Jump to content

[SOLVED] Form post var not registering


Asheeown

Recommended Posts

This is a search for users to search through their own logs from different "sources" each source is a checkbox
code:
[code]
foreach($Source as $s) {
if($_POST['Source[$i]'] == $s) {
echo ("$s: <input name=\"Source[$i]\" type=\"checkbox\" id=\"Source[$i]\" value=\"$Source[$i]\" checked=\"checked\"/>&nbsp;");
} else {
echo ("$s: <input name=\"Source[$i]\" type=\"checkbox\" id=\"Source[$i]\" value=\"$Source[$i]\" />&nbsp;");
}
$i++;
}
[/code]

Now whats wrong with it is the checkboxes don't get checked...but I even printed the $_POST vars out before it and they say for $_POST['Source']:
[quote][Source] => Array ( [0] => 000500 )[/quote]

Any Ideas why they arent being checked?
Link to comment
https://forums.phpfreaks.com/topic/35904-solved-form-post-var-not-registering/
Share on other sites

OK, I think I get it... Can you confirm the following for me.

You display a form to the users that has a list of sources selected from the database, these are displayed as checkboxes on the page.  How do we know if it should be checked or not, is there a value for that in the database?

Regards
Huggie
Your code is wrong. Try this:
[code]<?php
foreach($Source as $s) {
$chkd = ($_POST['Source'][$i] == $s) ?' checked="checked"':'';
        echo $s . ': <input name="Source[' . $i . ']" type="checkbox" id="Source_' . $i . '" value="' . $Source[$i] . '"' . $chkd . '>&nbsp;';
        $i++;
}?>[/code]

BTW, id values can not be arrays AFAIK.

I also tightened the code somewhat...

Ken

Archived

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

×
×
  • Create New...

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.