1

I have a form with multiple fields: Textinputs, Checkboxes, Radios.. and I want to submit it to a MySQL database. When I comment the checkboxes HTML, and the corresponding php code, everything is working fine, and everything is submitted and saved in the DB. If I try to submit the checkbox-form and I uncomment it, nothing get submitted, and clicking on the submit-button doesn't make any effect.

How can I submit the value of the checkbox-field to the MySQL-Database as a string, with the values separated with a semi-colon? For ex. if the checkbox fields are: Ab, Cd, De, Fg - and "Ab" and "De" are checked, the following string gets submitted: "Ab;Cd"

Here is a part of my HTML-form:

<div class="row">
    <div class="col-sm-4">
        <fieldset class="form-group">
            <label for="plattform">Platform</label>
         <form id="formId">


                <input type="checkbox" name="check_list[]" value="Android">Android
                <input type="checkbox" name="check_list[]" value="iPhone">iPhone
                <input type="checkbox" name="check_list[]" value="iPad">iPad
                <input type="checkbox" name="check_list[]" value="Windows Phone">Windows Phone

            </form> 

            <!-- <input type="checkbox" name="check_list[]" value="Android">
            <input type="checkbox" name="check_list[]" value="iPhone">
            <input type="checkbox" name="check_list[]" value="iPad">
            <input type="checkbox" name="check_list[]" value="Windows Phone"> --> 

        </fieldset>
    </div>
    <div class="col-sm-4">
        <fieldset class="form-group">
            <label for="featured">Featured</label>
                <div>
                        <input type="radio" name="featured" required>True</input>
                    </div>
                    <div>
                    <input type="radio" name="featured" required checked>False</input> 
                </div>
        </fieldset>
    </div>

here is a sample of my php-file:

<?php /* Attempt MySQL server connection. Assuming you are running
MySQL server with default setting (user 'root' with no password) */

/* Database connection start */ 
$servername = "localhost"; 
$username = "serverName_Here"; 
$password = "password_Here"; 
$dbname = "dbName_Here";

$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());

// Check connection 
if($conn === false){
    die("ERROR: Could not connect. " . mysqli_connect_error()); 
}   
// Escape user inputs for security 
$stName = mysqli_real_escape_string($conn, $_POST['sName']); 
$lgName = mysqli_real_escape_string($conn, $_POST['lName']);
$desc = mysqli_real_escape_string($conn, $_POST['desc']);

$Platform = ''; 
if(!empty($_POST['check_list'])) {  
    $counter = 0;
    foreach($_POST['check_list'] as $check) {
        if ($counter < 1) {             
            $Platform = $check;         
        } else {                
            $Platform = $excludePlatform + ';' + $check;        
        }       
        counter++;
    } 
} 
$Platform = mysqli_real_escape_string($conn, $_POST['check_list']);


$sql = "INSERT INTO tableName_Here (stName, lgName, details_description, Platform) VALUES ('$stName', '$lgName', '$desc', '$Platform')"; 

if(mysqli_query($conn, $sql)){
    echo "Records added successfully."; 
} else {
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn); 
}   // close connection 
mysqli_close($conn); ?>
1
  • The PHP file code isn't visible. Commented Jul 1, 2016 at 9:33

3 Answers 3

1

You need to have the checkboxes within the form tags.

The easiest way is to put the form opening and closing tabs above and below the rest of the field code. The submit buttons should be within the form as well.

On the backend checkbox values will come through as an array. You can then do something like this if you want to save them comma separated.

$values = implode(', ', $_POST['check_list']);
Sign up to request clarification or add additional context in comments.

4 Comments

but I have a big form, which contains all the fields.. Textinput, Radios and checkboxes.. Should I add another form in that big page form, which is just for the checkbox?
I can see a <form id="formId"> with fields outside it - all the fields should be inside. If you have an additional form tag that is not included in the code above that wraps everything you can delete the nested form tag. So you just need one form tag that includes all the fields.
oh no.. there is a bigger form, which contains the whole fields of the page. The form you re seeing is just for the checkbox.. should I delete if?
Yeah you don't need a form for the checkboxes within the larger form.
0

First of all,Why you haven't used POST as method in form tag also you haven't used submit button.In that way you can access value of checkboxes in php like

$value=$_POST['check_list[]'];

Comments

0

Try this whole example,

Table Structure

CREATE TABLE IF NOT EXISTS `games` (
  `id` int(12) NOT NULL AUTO_INCREMENT,
  `game_name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;


<?php
include_once("yourconfig.php"); //include your db config file
extract($_POST);
$check_exist_qry="select * from games";
$run_qry=mysql_query($check_exist_qry);
$total_found=mysql_num_rows($run_qry);
if($total_found >0)
{
    $my_value=mysql_fetch_assoc($run_qry);
    $my_stored_game=explode(',',$my_value['game_name']);
}

if(isset($submit))
{
    $all_game_value = implode(",",$_POST['games']);
    if($total_found >0)
    {
        //update
        $upd_qry="UPDATE games SET game_name='".$all_game_value."'";
        mysql_query($upd_qry);

    }
    else
    {
        //insert
        $ins_qry="INSERT INTO games(game_name) VALUES('".$all_game_value."')";
        mysql_query($ins_qry);
    }
}

?>
<form method="post" action="">
Games You Like: <br/>
    <input type="checkbox" name="games[]" value="1" <?php if(in_array(1,$my_stored_game)){echo "checked";}?>><label>Football</label><br>
    <input type="checkbox" name="games[]" value="2" <?php if(in_array(2,$my_stored_game)){echo "checked";}?>><label>Basket Ball</label><br>
    <input type="checkbox" name="games[]" value="3" <?php if(in_array(3,$my_stored_game)){echo "checked";}?>><label>Pool</label><br>
    <input type="checkbox" name="games[]" value="4" <?php if(in_array(4,$my_stored_game)){echo "checked";}?>><label>Rugby</label><br>
    <input type="checkbox" name="games[]" value="5" <?php if(in_array(5,$my_stored_game)){echo "checked";}?>><label>Tennis</label><br>
    <input type="checkbox" name="games[]" value="6" <?php if(in_array(6,$my_stored_game)){echo "checked";}?>><label>Cricket</label><br>
    <input type="checkbox" name="games[]" value="7" <?php if(in_array(7,$my_stored_game)){echo "checked";}?>><label>Table Tennis</label><br>
    <input type="checkbox" name="games[]" value="8" <?php if(in_array(8,$my_stored_game)){echo "checked";}?>><label>Hockey</label><br>
    <input type="submit" name="submit" value="submit">
</form>

this is just basic example and query i have added in this example, you can learn from this basic example and i think this is very useful for you... if useful than give correct answer for this solution

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.