Page 1 of 1

Sticky Checkboxes array

Posted: Mon Sep 19, 2011 6:32 pm
by malakiahs
I need help making an array of checkboxes sticky. This is what I am trying to use.

Code: Select all

<?php			  
			  

          
            <input type="checkbox" name="submissions[]" value="Email" id="submit_email"' . ((isset($_POST['submissions']) == 'Email' ) ? ' checked="checked"' : '' ) . '  />
            Submit via Email
            <br />

            <input type="checkbox" name="submissions[]" value="Person" id="in_person"' . ((isset($_POST['submissions']) == 'Person' ) ? ' checked="checked"' : '' ) . ' />
            Submit Resume in Person
			<br />

            <input type="checkbox" name="submissions[]" value="Pick up App." id="pick_up"' . ((isset($_POST['submissions']) == 'Pick up App.' ) ? ' checked="checked"' : '' ) . '   />
            Pick-up application
			<br />

            <input type="checkbox" name="submissions[]" value="Visit Web" id="visit_web"' . ((isset($_POST['submissions']) == 'Visit Web' ) ? ' checked="checked"' : '' ) . '  />
            Visit Company Website
			<br />

            <input type="checkbox" name="submissions[]" value="" id="otherbox" />
            Other: <input type="text" name="othertext" id="other" value="" size="20" maxlength="20" />
			
           <br /> ';
		
?>	
But this does only seems to work when it is not an array, all boxes get checked when the submit button is clicked. I would like to use an array because of validations purposes.

Any help is appreciated. Thank you in advance.

Re: Sticky Checkboxes array

Posted: Mon Sep 19, 2011 7:44 pm
by twinedev
Well, to determine if you should says it is checked, you are doing the following:

isset($_POST['submissions']) == 'Visit Web'.

Well as long as one checkmart is placed isset($_POST['submissions']) will be equal to true
When it comes to comparing, you are comparing a Boolean (TRUE from the isset) to a string. So PHP evaluates each string to be a boolean value, and since they are not blank, that ends up being TRUE.

so you are saying IF (TRUE == TRUE) set the checkbox to be selected.

(isset($_POST['submissions]) && in_array('Visit Web',$_POST['submissions])) is something you are more likely looking for.

-Greg

Re: Sticky Checkboxes array

Posted: Mon Sep 19, 2011 8:04 pm
by malakiahs
Thank you so much Greg, I now see where my mistake was!!
I fixed it and it worked flawlessly!
I looked everywhere before posting here and did not find anything. I am now in debt with you, sir.

For anyone having the same problem, this is the complete code that I am using and its working perfectly (thanks to Greg)

((isset($_POST['submissions']) && (in_array('Visit Web',$_POST['submissions']) == Visit Web') ) ? ' checked="checked"' : '' )

Re: Sticky Checkboxes array

Posted: Tue Sep 20, 2011 1:56 am
by twinedev
you don't need the == 'Visit Web' in there because it will always evaluate as TRUE since it is being compared against a boolean. It is just wasted code not needed.

-Greg

Re: Sticky Checkboxes array

Posted: Tue Sep 20, 2011 8:20 am
by malakiahs
Yes, you are right. It was wasted code since I tested the form without the =='Visit Web' and it works perfectly also.
I must admit, I am not very familiar with the function in_array() yet.
I will have to do more reading on that.

I greatly appreciate your help. And for this I thank you again.