Page 1 of 1

checkbox:error

Posted: Wed Sep 17, 2008 11:49 pm
by swetha

Code: Select all

 
<?php
if (isset($_POST['submit']))
{
    for ($i=0;$i<count($state);$i++)
    {
            $temp .= $count[$i];
        echo "value of temp:". $temp;
             }
}
?>
<form action="" method="post">
<input type="checkbox" name="state[]" value="NE">First 
<input type="checkbox" name="state[]" value="IA">Second 
<input type="checkbox" name="state[]" value="MO">Third
<input type="submit"  value="submitted "  name="submit">
</form>
im getting the following error
Undefined variable: state in E:\Work\Tools for download\checkdemo.php on line 4 .

Why am i getting the above error?

Re: checkbox:error

Posted: Thu Sep 18, 2008 12:16 am
by pcoder
I haven't seen in your code where the $state variable comes from. :)

Re: checkbox:error

Posted: Thu Sep 18, 2008 1:10 am
by swetha
$state is the name of the checkbox.thanks.pls help

Re: checkbox:error

Posted: Thu Sep 18, 2008 1:25 am
by pcoder
In your code, state is the POST value, how can you use that directly as $state.
Umm.. tell me first what you are trying to do.

Re: checkbox:error

Posted: Thu Sep 18, 2008 1:35 am
by pcoder

Code: Select all

 
You have to use count($_POST['state']) instead of count($state);
 
Better to look at the post value. I mean to say

Code: Select all

 
print_r($_POST);
 
Then you will be more clear.

Re: checkbox:error

Posted: Thu Sep 18, 2008 2:53 am
by swetha

Code: Select all

 
<?php
if (isset($_POST['submit']))
{
   $temp=" ";
   $count=count($_POST['city']);
   for($i=0;$i<$count;$i++)
   {
      $temp="$temp$city[$i],";
   }
   echo "value of temp:$temp"; 
} 
?>
 
<form action="" method="post">
<input type="checkbox" name="city[]" value="NE"
<?php if ((isset($_POST['city[0]'])) && ($_POST['city[0]']=="NE")) echo "checked='checked'" ?>/>First 
<input type="checkbox" name="city[]" value="IA"
<?php if ((isset($_POST['city[1]'])) && ($_POST['city[1]']=="IA")) echo "checked='checked'"?>/>Second 
<input type="checkbox" name="city[]" value="MO"
<?php if ((isset($_POST['city[2]'])) && ($_POST['city[2]']=="MO")) echo "checked='checked'"?>/>Third
<input type="submit" name="submit" value="submit"/>
</form>
 

please check this code..."Im getting the error undefined variable:city"
Also i need to ensure that the checkbox is enabled when i select a checkbox and click on "submit" . Also i need to store the values of city in temp array.

Re: checkbox:error

Posted: Thu Sep 18, 2008 3:49 am
by pcoder
You have an array like this:

Code: Select all

$_POST['city'][0],$_POST['city'][1] and so on.
You cannot use directly as

Code: Select all

$city[$i];
Replace

Code: Select all

$city[$i] with $_POST['city'][$i]
. It will work.

Re: checkbox:error

Posted: Thu Sep 18, 2008 4:50 am
by swetha

Code: Select all

 
<form action="" method="post">
<input type="checkbox" name="city[]" value="NE" <?php if ((isset($_POST['city'][0])) && ($_POST['city'][0]=="NE")) echo " checked"?>/>First 
<input type="checkbox" name="city[]" value="IA" <?php if ((isset($_POST['city'][1])) && ($_POST['city'][1]=="IA")) echo " checked" ?>/>Second 
<input type="checkbox" name="city[]" value="MO" <?php if ((isset($_POST['city'][2])) && ($_POST['city'][2]=="MO")) echo " checked"?> />Third
</form>
 


thanks it worked,but this portion of code didnt work,ie checkbox does not remain checked in some cases.

Re: checkbox:error

Posted: Thu Sep 18, 2008 5:10 am
by pcoder
In your code,

Code: Select all

$_POST['city'][0]
is not always "NE".
When you check the last one and submit the form.

Code: Select all

$_POST['city'][0]
becomes "MO";
It depends upon which you check at first. So you have to change the logic to solve it.