Rifts Posted July 2, 2010 Share Posted July 2, 2010 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 Quote Link to comment Share on other sites More sharing options...
beta0x64 Posted July 2, 2010 Share Posted July 2, 2010 You should check if the variables exist before referencing them otherwise. Do this with the isset method. isset($_POST["variables"]); Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 2, 2010 Share Posted July 2, 2010 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 } Quote Link to comment Share on other sites More sharing options...
Rifts Posted July 2, 2010 Author Share Posted July 2, 2010 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 Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 2, 2010 Share Posted July 2, 2010 I you do your inputs the way I showed, then you would remove all the $chxN = $_POST['chxN'] and replace all of your echo $chxN with: foreach($_POST['check'] as $chx) { echo $chx; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.