Forum Moderators: coopster
I am a brand new member of this forum, and I am also new to PHP. I have a moderate amount of experience working with C++, and so am able to pretty well grasp most PHP concepts without issue. Yet, I am having a bit of difficulty with the server-side scripting aspect of PHP. In particular, I am still learning how to retrieve information once it has been submitted to the server for processing.
In this instance, I have created an xhtml 1.1 form that includes some simple PHP validation procedures on the fields (I know JavaScript might be a better choice for client-side validation), and I would like the form to retain all information that was filled in by the user to remain filled in once the error page, which is also the form page, is returned with a list of errors for the user to read. I believe the term for this type of form is "Sticky Form".
I have managed to do this successfully with text fields, text areas, and select menues, but cannot figure how to do this with checkboxes. I have stored all of the values that are selected inside an array, like this:
<label for="mail_contact"><input type="checkbox" name="contactmethod[]" id="mail_contact" value="Mail" />Mail</label>
<label for="email_contact"><input type="checkbox" name="contactmethod[]" id="email_contact" value="Email" />Email</label>
<label for="phone_contact"><input type="checkbox" name="contactmethod[]" id="phone_contact" value="Phone" />Phone</label>
Thank you in advance.
The best solution for something like this is to utilize the ternary operator [us3.php.net].
The example would then look something like this"
<input type="checkbox" name="contactmethod[]" id="mail_contact" value="Mail" [b]<?php echo ([url=http://us2.php.net/manual/en/function.isset.php]isset[/url]($_POST['contactmethod']) && [url=http://us2.php.net/in-array]in_array[/url]('Mail',$_POST['contactmethod']))? 'checked':'';?>[/b] />Mail
<input type="checkbox" name="contactmethod[]" id="email_contact" value="Email" [b]<?php echo (isset($_POST['contactmethod']) && in_array('Email',$_POST['contactmethod']))? 'checked':'';?>[/b] />Email
<input type="checkbox" name="contactmethod[]" id="phone_contact" value="Phone" [b]<?php echo (isset($_POST['contactmethod']) && in_array('Phone',$_POST['contactmethod']))? 'checked':'';?>[/b] />Phone
This should work as intended, however, it is untested. Good luck! :)
Thank you for the warm welcome :)
Okay, I hope you don't mind if I try to break this down, in order to fully understand it...Please let me know if I've misunderstood you.
<input type="checkbox" name="contactmethod[]" id="mail_contact" value="Mail" <?php echo (isset($_POST['contactmethod']) && in_array('Mail',$_POST['contactmethod']))? 'checked':'';?> />Mail The ternary operator is exp1? exp2 : exp3, where expression 1 is the condition, expression 2 will occur if expression 1 is TRUE, and expression 3 will occur if expression 1 is FALSE. I've actually used this before in C++, though I'd not thought to use it here. It's amazing how moving from one environment to another can be disorienting, but anyway...
So, it seems like you have used 2 different conditionals:
Conditional 1...
isset($_POST['contactmethod'])
Conditional 2...
in_array('Mail',$_POST['contactmethod'])
...and you have used && to ensure that both of these conditions must test TRUE in order for 'checked' to be added to the input tag.
isset() is used to see if the current checkbox option was previously checked, while the ternary statement is checking to see if Mail exist in the array contactmethod[]. Did I interpret that correctly?
Please forgive my naiveté, but I'm just new to this. I really don't mean to sound ungrateful or rude, I'm just trying to fully understand things...
Isn't it redundant to use both the isset() function and the in_array() ternary statement together with the && operator? If I understand correctly (and I probably don't), a checkbox will only be in the contactmethod[] array if it was checked. So, in that case, isset() would return TRUE if the checkbox was previously checked and the in_array() ternary statement would also only resolve TRUE if the box was previously checked. Is there a circumstance in which one could be true while the other is not true?
Sorry to be so difficult, but I really do appreciate your help.
Sorry to be so difficult
Don't worry; you are not being difficult at all. :)
Your interpretations are correct for everything.
Now, onto why I put an
isset in there. Let me explain using a simpler example. Consider the following code:
$array = array("one","two","three","four","five");
if(in_array("one",$array)) {
echo 'found!';
} else {
echo 'Not found';
}
Now, the obvious output is Found and you should clearly see why: because
one is an element inside the $array array. Now let's change this around a little bit. Let's say, for some reason, the array isn't defined:
if(in_array("one",$array)) {
echo 'found!';
} else {
echo 'Not found';
}
If you run this code, you'll find that you'll get two errors. The first is that the variable
$array does not exist. This is obvious behavior because we did not define it. You will also get a warning that tells you that the second parameter for the in_array function is the wrong datatype. Now let's see what we can do with an isset:
if(isset($array) && in_array("one",$array)) {
echo 'found!';
} else {
echo 'Not found';
}
Here, if you run this, you won't get any errors, and it will print Not Found as it should. What happens is the
isset returns FALSE, already causing the if argument to be false not even bothering with the in_array (as my experiments have shown). One interesting thing to note here is that if you were to write it the other way around:
if(in_array("one",$array) && isset($array)) {
You will get those same two errors again.
So how does this apply to what you are doing? Well, all I'm doing by placing the
isset in there is test for the possibility that the $_POST variable does not exist, or at least the checkbox element of the POST array doesn't. So with the isset, if this is the case, no errors will be thrown. I hope this makes it a little clearer. If you are still having trouble understanding this, why don't you try out the example I've given you.
Good luck! :)
Actually I did try your example in the first reply right after I read it and your solution worked perfectly. I was just trying to understand how it worked. I appologize for not having mentioned that earlier, and thank you immensely for your helpfullness.
Now that I've read your example, I understand perfectly why you used the isset() && in_array() setup. Thank you :)
Though, I admit the reaction of the in_array() statement in your second example is a bit confusing to me.
if(in_array("one",$array)) {
echo 'found!';
} else {
echo 'Not found';
}If you run this code, you'll find that you'll get two errors. The first is that the variable $array does not exist. This is obvious behavior because we did not define it. You will also get a warning that tells you that the second parameter for the in_array function is the wrong datatype.
It seems to me that since you are using the in_array() function, you should just get 'found!' or 'Not found'. I can see how your isset() techniques fixes this problem, but why is it a problem in the first place? Is it just a quirk of this particular function, or was that response actually intended?
I was just trying to understand how it worked
You've described it quite nicely in your second post. I was agreeing with you in my last post, that you are correct. But here's a quick explanation:
<input type="checkbox" name="whatever[]" value="foo" <?php echo (check to see if the value of the checkbox exists in the post array)?(if it does, echo 'checked'):(otherwise echo nothing);?> />
It seems to me that since you are using the in_array() function, you should just get 'found!' or 'Not found'.
Well, it is not because you are using that particular function, but rather that you are running a condition based on what that function returns and echoing different messages accordingly.
I can see how your isset() techniques fixes this problem, but why is it a problem in the first place? Is it just a quirk of this particular function, or was that response actually intended?
Well, no, it's not just with this function. I just hate to see errors, or warnings, or notices, or whatever, come from my code when you can write it so that it handles it properly. You can call it overkill if you want, however, if you are dealing with variables that may or may not be defined, then you want to handle it properly when they may not be defined by using isset, or empty, etc...
In your case, I wasn't sure if there was a possibility that the post variables didn't exist, so I added an isset in to account for both possible situations.