What is JotForm?
JotForm is a free online form builder which helps you create online forms without writing a single line of code. No sign-up required.

At JotForm, we want to make sure that you’re getting the online form builder help that you need. Our friendly customer support team is available 24/7.

We believe that if one user has a question, there could be more users who may have the same question. This is why many of our support forum threads are public and available to be searched and viewed. If you’d like help immediately, feel free to search for a similar question, or submit your question or concern.


  • Profile Image

    What is the appropriate way to handle checkboxes in mySQL?

    Asked by jamesforpm on September 30, 2014 at 06:31 AM

    What is the appropriate way to handle checkboxes in mySQL? I've created a column in my db that maps to a checkbox field but when I submit data to it, it shows as Array rather than what has been selected. Thanks.



    This is a re-post of a comment on How to send Submissions to Your MySQL Database Using PHP

  • Profile Image

    Answered by charmiequino on September 30, 2014 at 09:08 AM

    Hi jamesforpm,

    Thank you for posting your concern in the forum. From what I understand, you're submitting a data using a custom php code and inserting the values of multiple checkboxes to your database in one of your columns. Did I understand it correctly?

    Here’s the possible solution that I can think of.

    1. Here is a sample form I made, Sample Form. Take note of the indexes or values you have, you can use this tutorial "Show Post Data" to view the post data submission.

    2. Here’s the sample php code I have in getting the values and inserting it into my database.

    $answers = $_POST;
    $firstname = array("first" => $answers[firstname]); //first name field
    $checkbox = array("pet" => $answers[pet]); //creates an array for the selected checkboxes

    //Default code in connecting mysql as posted in W3schools
    //Modified for the submission of our form.
    $con=mysqli_connect("host_name","db_username","password","db_table_name");
    // Check connection
    if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }

    // get values and prepare SQL
    $pets = implode(',', $checkbox['pet']); //using php function implode to separate selected answers
    $first = $firstname['first']; //get value firstname
    $sql="INSERT INTO table1 VALUES ('$first', '$pets')"; //sql query to insert to database

    //Run sql with security checking
    if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } echo "1 record added";
    mysqli_close($con);

    Using the tutorial above, it will show you the correct keys that you need in using your php script. In my form you can check the screenshot below.

    3. The above code simply gets the value of the field of First name field and the selected options in ‘Pet’ checkboxes.

    4. Submit the form and the values are inserted in the database as shown below.

     

     

    Using the right keys and indexes and using the PHP function implode, the values selected in the checkboxes will show in the 'option' column separated by commas.

    I hope this helps you. If you have any questions or concern about this, let us know.

    Regards,

    Charmie

  • Profile Image

    Answered by jamesforpm on September 30, 2014 at 02:10 PM

    Thanks - this solved my problem. 

  • Profile Image
    JotForm Support

    Answered by KadeJM on September 30, 2014 at 04:02 PM

    Glad to see that charmiequino's answer was able to help solve your issue. 

    Additionally, I would like to point out that we also have documentation on Our JotForm API such as for PHP usage in the event that you might ever want to take a look at it for future reference.

    Feel free to open up a new thread to contact us should you need more help again.

  • Profile Image

    Answered by smit  on November 07, 2015 at 01:56 AM

    how to view multiple checkbox values in multiple column in database using php

    this is my code

    <?php

    include('config.php');

     

    //echo '<pre>';print_r($_POST);

     

    //echo sizeof($_POST['size_name']);

     

    if(isset($_POST['submit']))

    $data[]=array("id"=>0,"application_name"=>"","range_name"=>"","size_name"=>"","image"=>"");

     

    if(isset($_POST['size_name']))

    {   

    $val=$_POST['size_name'];

    for($i="0";$i<sizeof($val);$i++)

    {

    $seldata=mysql_query("select * from tiles where size_name='".$val[$i]."'")or die(mysql_error());

    while($data1=mysql_fetch_assoc($seldata))

    {

    $data[]=$data1;

    }

    }

    }

     

    if(isset($_POST['application_name']))

    {   

    $val=$_POST['application_name'];

    for($i="0";$i<sizeof($val);$i++)

    {

    $seldata=mysql_query("select * from tiles where application_name='".$val[$i]."'")or die(mysql_error());

    while($data2=mysql_fetch_assoc($seldata))

    $nm=$i+1;

    if(($data2['id']!=$data[$nm]['id']) && ($data[$nm]!="0"))

    {

    $data[]=$data2; 

    }

    }

    }

     

    }

    if(isset($_POST['range_name']))

    {   

    $val=$_POST['range_name'];

    for($i="0";$i<sizeof($val);$i++)

    {

    $seldata=mysql_query("select * from tiles where range_name='".$val[$i]."'")or die(mysql_error());

    while($data3=mysql_fetch_assoc($seldata))

    {

    $nm=$i+1;

    if(($data3['id']!=$data[$nm]['id'])  && ($data[$nm]!="0"))

    {

    $data[]=$data3; 

    }

     

    }

     

    }

    }

     

     

    unset($data["0"]);

    echo '<pre>';print_r($data);

     

  • Profile Image
    JotForm Support

    Answered by Jan on November 07, 2015 at 09:00 AM

    @smit

    This is an old thread. In order to assist you properly, I moved your inquiry on another thread. Here's the URL: http://www.jotform.com/answers/699932

    Thank you.