Jump to content

Archived

This topic is now archived and is closed to further replies.

benluke

validating arrays from a form

Recommended Posts

Hi,

I would like to learn how to validate arrays. I need to make sure that at least one check box has been selected from each set.

I have the following html within a form
[code]
<input type="checkbox"  name="knowledge1[]" value="A">
<input type="checkbox"  name="knowledge1[]" value="B">
<input type="checkbox"  name="knowledge1[]" value="C">
<input type="checkbox"  name="knowledge1[]" value="D" >

<input type="checkbox"  name="knowledge2[]" value="A">
<input type="checkbox"  name="knowledge2[]" value="B">
<input type="checkbox"  name="knowledge2[]" value="C">
<input type="checkbox"  name="knowledge2[]" value="D" >

<input type="checkbox"  name="knowledge3[]" value="A">
<input type="checkbox"  name="knowledge3[]" value="B">
<input type="checkbox"  name="knowledge3[]" value="C">
<input type="checkbox"  name="knowledge3[]" value="D" >
[/code]

I then pass it and serialize it

[code]
$pc1=serialize($_POST['knowledge1']); //grab the array data and seaialize it to store in db
$pc2=serialize($_POST['knowledge2']);
$pc3=serialize($_POST['knowledge3']);
[/code]

Should i validate it before serializing? What is the best way to do this?

Benluke

Share this post


Link to post
Share on other sites
If I can remember correctly, if a checkbox is not checked its name is not passed as a key in the $_POST array, so it would be as easy as just checking if the array key is set.

Share this post


Link to post
Share on other sites
Hi,

ok so how would i go about doing that. Would i be looking at using the isset function?

benluke

Share this post


Link to post
Share on other sites
[!--quoteo(post=385997:date=Jun 20 2006, 02:08 PM:name=benluke)--][div class=\'quotetop\']QUOTE(benluke @ Jun 20 2006, 02:08 PM) [snapback]385997[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi,

ok so how would i go about doing that. Would i be looking at using the isset function?

benluke
[/quote]
Pretty much, yeah.
[code]if(!isset($_POST['knowledge1'])) echo "No checkboxes were selected for Knowledge 1";[/code]

Share this post


Link to post
Share on other sites
No problem :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.