mardi 25 avril 2017

Submit multiple checkboxes with multiple values into database

I am making a web questionnaire containing 20 pictures and 20 checkboxes. May I know how to add the values of each checkboxes into the database using PHP ? My sample code is as below.

HTML:

    <br> <br>
        <h1> <font size="5" face="verdana">Image #1 </font> </h1>
    <br>
        <img src="image/img1.png" alt="HTML5 Icon" width="500" align="absmiddle"> <br> <br>
        <input  type="checkbox" name="ans_image1[]" value="ans1"> Relevant Context</option> <br /> 
        <input type="checkbox"  name="ans_image1[]" value="ans2"> Familiarity Brand</option> <br />
        <input type="checkbox"  name="ans_image1[]" value="ans3"> Colors Eye Catching</option> <br />
        <input type="checkbox"  name="ans_image1[]" value="ans4"> Text Fonts Sizes</option> <br />
        <input type="checkbox"  name="ans_image1[]" value="ans5"> Create Awareness</option> <br /> <br />

    <br>
        <h1> <font size="5" face="verdana">Image #2 </font> </h1>
    <br />
        <img src="image/img2.PNG" alt="HTML5 Icon" width="500" align="middle"> <br> <br>
        <input type="checkbox" name="ans_image2[]" value="ans1"> Relevant Context</option> <br /> 
        <input type="checkbox" name="ans_image2[]"  value="ans2"> Familiarity Brand</option> <br />
        <input type="checkbox" name="ans_image2[]"  value="ans3"> Colors Eye Catching</option> <br />
        <input type="checkbox" name="ans_image2[]"  value="ans4"> Text Fonts Sizes</option> <br />
        <input type="checkbox" name="ans_image2[]" value="ans5"> Create Awareness</option> <br /> <br />


    <br>                
        <h1> <font size="5" face="verdana"> Image #3 </font></h1>
    <br />
        <img src="image/img3.png" alt="HTML5 Icon" width="500" align="middle"> <br> <br>
        <input type="checkbox" name="ans_image3[]" value="ans1"> Relevant Context</option> <br /> 
        <input type="checkbox"  name="ans_image3[]" value="ans2"> Familiarity Brand</option> <br />
        <input type="checkbox" name="ans_image3[]"  value="ans3"> Colors Eye Catching</option> <br />
         <input type="checkbox"  name="ans_image3[]" value="ans4"> Text Fonts Sizes</option> <br />
         <input type="checkbox"  name="ans_image3[]" value="ans5"> Create Awareness</option> <br /> <br />

PHP:

    if(isset($_POST["submit"]))
    {
    $age = $_POST['age'];
    $gender = $_POST['gender'];
    $image1 = $_POST['ans_image1'];
    $image2 = $_POST['ans_image2'];
    $image3 = $_POST['ans_image3'];


    $insert = "INSERT INTO tbsoalan (age, gender, image1, image2, image3) 
    VALUES ('$age', '$gender', '$image1', '$image2', '$image3')";

I have come across multiple solutions in the web but mostly are for only one checkbox and I do not know how to implement implode, explode or delimiter into the coding. May I know how ?

Thank youu.




Aucun commentaire:

Enregistrer un commentaire