checkbox:error

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
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

checkbox:error

Post 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?
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: checkbox:error

Post by pcoder »

I haven't seen in your code where the $state variable comes from. :)
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: checkbox:error

Post by swetha »

$state is the name of the checkbox.thanks.pls help
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: checkbox:error

Post 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.
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: checkbox:error

Post 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.
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: checkbox:error

Post 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.
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: checkbox:error

Post 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.
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: checkbox:error

Post 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.
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: checkbox:error

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