Hi to all, actually i have a simple form called "form2" in which, i have used some checkboxes to choose users several values of "Hobbies". So my problem arises that, if a person selects any one of the hobby , and submit form then the value of that single hobby is properly entered into the database, But if he selects more than one hobbies then also the values of any of them one single value is entered in to the database. rather than his selected all multiple values of hobbies. what should be done so that All the (multiple hobby) values selected by a person properly enter into the database? its validation is shown below:- <script type="text/javascript"> function validate_required(form2) { if (!form2.checkbox[0].checked && !form2.checkbox[1].checked && !form2.checkbox[2].checked ) { alert("Please Select any Hobby"); return false; } return true; } </script> & here its code is as follows:- <form name="form2" onSubmit="return validate_required(this)" action="submit2.php" method="post" > Hobbies : <input type = "checkbox" name="checkbox" id= "checkbox1" value="movies"> Movies. <input type = "checkbox" name="checkbox" id= "checkbox2" value="reading">Reading. <input type = "checkbox" name="checkbox" id= "checkbox3" value="sports"> Sports. please suggest me the right code to be used or any other suggessions regarding this. Thanks & Regards.
For a check box you have to use differetn name attributes for each check box or you can use any array to work in php. use some thing like <input type = "checkbox" name="checkbox1" id= "checkbox2" value="reading">Reading. <input type = "checkbox" name="checkbox2" id= "checkbox3" value="sports"> Sports.
Hello sir, first of all thanks for your precious suggession. please suggest me what changes wll be applied to my "submit2.php" form according to your code. "submit2.php" :- <?php $sql="INSERT INTO mydatabase.mytablename (Name, Age, Hobbies, ) VALUES ('$_POST[name]','$_POST[age]','$_POST[checkbox]')" ; ?> Thanks & Regards.
Its better to use an array for check box name. Ex: name="checkbox[0]" and so on once you had used any array then you can retrieve the array values as $_POST['checkbox[$i]'] using a loop. and insert it into your database
change checkboxes name checkbox to checkbox[]. you dont have to write keys. and print_r($_POST['checkbox']); PHP: