Sticky Checkboxes array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
malakiahs
Forum Newbie
Posts: 13
Joined: Tue Sep 13, 2011 11:15 am

Sticky Checkboxes array

Post 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.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Sticky Checkboxes array

Post 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
malakiahs
Forum Newbie
Posts: 13
Joined: Tue Sep 13, 2011 11:15 am

Re: Sticky Checkboxes array

Post 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"' : '' )
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Sticky Checkboxes array

Post 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
malakiahs
Forum Newbie
Posts: 13
Joined: Tue Sep 13, 2011 11:15 am

Re: Sticky Checkboxes array

Post 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.
Post Reply