Jump to content

Include Checkbox Value


dvent

Recommended Posts

Hi All,

 

I'm very new to PHP so please bear with me.

 

I have created a form with 3 fields. One field is a multi select / checkbox with the name 'tenCode'. How do I get all of the values selected from this box and add them to my database?

 

My submission code (submit.php)

 

<?php
$con = mysql_connect("localhost","dbname","dbpassword");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbname", $con);

$sql="INSERT INTO tablename (tenFirst,tenPhone,tenID)
VALUES
('$_POST[tenFirst]','$_POST[tenPhone]','$_POST[tenID]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
  
echo "Adding Ten. The page will refresh in 5 seconds.";
echo "<meta http-equiv='Refresh' content='5; URL=nextpage.php'/>";

mysql_close($con)
?>

 

Thanks

dvent

Link to comment
Share on other sites

If you name your checkboxes "tenCode[]" like an array, then $_POST['tenCode'] will also be an array containing the values of the checkboxes that were selected.

For example,

<input type="checkbox" name="tenCode[]" value="pare1">
<input type="checkbox" name="tenCode[]" value="pare2">
<input type="checkbox" name="tenCode[]" value="pare3">
<input type="checkbox" name="tenCode[]" value="pare4">
<input type="checkbox" name="tenCode[]" value="pare5">

results in

$_POST = Array
(
    [tenCode] => Array
        (
            [0] => pare1
            [1] => pare3
        )
)

 

How you choose to store that is up to you. You could for example store them separated by a comma or semicolon using implode.

$tenCode = implode(';', $_POST['tenCode']);

Link to comment
Share on other sites

Thanks for the replies.

 

I've added my example table here:

 

http://bit.ly/Hslw4X

 

You'll see the box asking for suitable postcodes. That is built using the following code:

 

<div class="mws-form-row">
                    				<label>Suitable Postcodes</label>
                    				<div class="mws-form-item large">
                                    	<div class="mws-dualbox clearfix">
                                        	<div class="mws-dualbox-col1">
                                            	<div class="mws-dualbox-filter clearfix">
                                                    <label for="box1Filter">Filter</label>
                                                    <input type="text" id="box1Filter" class="mws-textinput" />
                                                    <button type="button" id="box1Clear">CLEAR</button>
                                                </div>

                                                <select id="box1View" multiple="multiple" size="3">
                                                    <option value="1">Code 1</option>
                                                    <option value="2">Code 2</option>
                                                    <option value="3">Code 3</option>
                                                </select>
                                                <span id="box1Counter" class="countLabel"></span>
                                                <select id="box1Storage"></select>
                                            </div>
                                            <div class="mws-dualbox-col2">
                                                <button id="to2" type="button">></button>
                                                <button id="allTo2" type="button">>></button>
                                                <div class="clear"></div>
                                                <button id="allTo1" type="button"><<</button>
                                                <button id="to1" type="button"><</button>
                                            </div>
                                            <div class="mws-dualbox-col3">
                                            <div class="mws-dualbox-filter clearfix">
    	                                            <label for="box2Filter">Filter</label>
        	                                        <input type="text" id="box2Filter" class="mws-textinput" />
            	                                    <button type="button" id="box2Clear">CLEAR</button>
                                                </div>

                                                <select id="box2View" multiple="multiple" size="3" name="tenSuitablePostcodes"></select>
                                                <span id="box2Counter" class="countLabel"></span>
                                                <select id="box2Storage"></select>
                                            </div>
                                        </div>
                                    </div>
                    			</div>

 

My three values are defined in this bit:

 

<select id="box1View" multiple="multiple" size="3">
                                                    <option value="1">Code 1</option>
                                                    <option value="2">Code 2</option>
                                                    <option value="3">Code 3</option>
                                                </select>

 

How can I put these into your example and post them to the DB, separated by a comma?

 

Thanks

dvent

Link to comment
Share on other sites

I just realized what's going on with the two selects on that page haha. Let's say we were going to insert these values into the database. You would store all the values in variables and then insert it into the query which inserts a row in the database. The fields in your first code don't match up here so I'm making this up.

 

This goes in submit_add_tenant.php:

$forename = mysql_real_escape_string($_POST['tenForename']);
$surname = mysql_real_escape_string($_POST['tenSurname']);
$tenSuitablePostcodes = mysql_real_escape_string(implode(',', $_POST['tenSuitablePostcodes']));

$sql = "INSERT INTO table (`tenForename`, `tenSurname`, `tenSuitablePostcodes`) VALUES ('$forename', '$surname', '$tenSuitablePostcodes')";
$res = mysql_query($sql);

Your second select would need to be renamed into

<select id="box2View" multiple="multiple" size="3" name="tenSuitablePostcodes[]">

Link to comment
Share on other sites

Getting an error on submit:

 

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table (`tenForename`, `tenSurname`, `tenSuitablePostcodes`) VALUES ('1', '1111',' at line 1

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.