Jump to content

Simple php checkbox problem


Rifts

Recommended Posts

hey guys

 

I think I have a pretty simple problem I just cant seem to figure out.

 

I have an html form with 10 checkboxes

 

when I submit the form it echos all the inputs (name, address etc.)

 

the problem is I get "Notice: Undefined index: chx1" for each checkbox if it it not checked

but if the box is checked there is no error.

 

what do I need to do so the checkboxes are not required?

 

thanks

 

oh btw the checkboxes are NOT dynamic

Link to comment
https://forums.phpfreaks.com/topic/206550-simple-php-checkbox-problem/
Share on other sites

You should check if the variables exist before referencing them otherwise. Do this with the isset method.

 

isset($_POST["variables"]);

 

Yes, and you can also create an array of checkboxes so that you only operate on the ones that are set:

<input type='checkbox" name="check[1]" value="something">
<input type='checkbox" name="check[2]" value="something else">

 

Then:

foreach($_POST['check'] as $key => $value) {
   //do stuff with $key and/or $value
}

humm sorry im pretty new to writing php maybe you could give me some more help

 

I tried to use the isset but its still not working and i dont understand what  "=>" is.

 

here is my code maybe it'll help ill just post the php because the html form is very standard and simple

 

thanks

        $fname = $_POST['first'];
$lname= $_POST['last'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$phone1 = $_POST['phone1'];
$phone2 = $_POST['phone2'];
$phone3 = $_POST['phone3'];
$chx1 = $_POST['chx1'];
$chx2 = $_POST['chx2'];
$chx3 = $_POST['chx3'];
$chx4 = $_POST['chx4'];
$chx5 = $_POST['chx5'];
$chx6 = $_POST['chx6'];
$chx7 = $_POST['chx7'];
$chx8 = $_POST['chx8'];
$chx9 = $_POST['chx9'];
$chx10 = $_POST['chx10'];
$comment = $_POST['comment'];

$fname = mysql_real_escape_string($fname);
$lname= mysql_real_escape_string($lname);
$address = mysql_real_escape_string($address);
$city = mysql_real_escape_string($city);
$state = mysql_real_escape_string($state);
$zip = mysql_real_escape_string($zip);
$country = mysql_real_escape_string($country);
$phone1 = mysql_real_escape_string($phone1);
$phone2 = mysql_real_escape_string($phone2);
$phone3 = mysql_real_escape_string($phone3);
$comment = mysql_real_escape_string($comment);  

echo $fname;
echo $lname;
echo $address;
echo $city;
echo $state;
echo $zip;
echo $country;
echo $phone1;
echo $phone2;
echo $phone3;
echo $chx1;
echo $chx2;
echo $chx3;
echo $chx4;
echo $chx5;
echo $chx6;
echo $chx7;
echo $chx8;
echo $chx9;
echo $chx10;
echo $comment;

 

 

thanks

 

 

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.