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
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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.