Forum Moderators: coopster

Message Too Old, No Replies

Select multiple checkboxes

php multiple checkboxes

         

jcolgate

1:22 am on Jan 13, 2007 (gmt 0)

10+ Year Member



I'm working on a project and have a simple issue that is eluding me.

I want to pass the values of multiple checkboxes to a table in the database. But of course how I am doing it now, only one value is passed and not the array.

Any help to get me past this mental block would be appreciated. Thanks.

Here is some sample code: (Please understand that this is only part of a larger file.
-----------------------------------------------
SECTION ONE:
-----------------------------------------------
// ********************* //
// BEGIN PRODUCT SECTION //
// ********************* //

$verify_products = "SELECT * FROM news_product order by product_rank DESC";
$verify_products_res = mysql_query($verify_products, $conn)
or die(mysql_error());
if (mysql_num_rows($verify_products_res) < 1) {

// *********************************** //
// NO PRODUCT EXISTS - DISPLAY MESSAGE //
// *********************************** //

$display .="<p align=\"center\">There are no products in the database.<br><a href=\"product_add.php\">Click to Add a Product.</a></p>";

// ****************** //
// END VERIFY PRODUCT //
// ****************** //
}else
//LIST PRODUCT(s)
{

// PULL PRODUCT DATA FOR LIST //
while ($newArray = mysql_fetch_array ($verify_products_res)){
$product_id = $newArray['product_id'];
$product_name = $newArray['product_name'];
$product_rank = $newArray['product_rank'];

//STRIPSLASHES
$product_name = stripslashes($product_name);
$product_rank = stripslashes($product_rank);
//DISPLAY LIST

$display .="<input type=\"checkbox\" name=\"product_id[]\" value=\"$product_id\">$product_name<br>";

//END DATA PULL FOR LIST
}
//END PRODUCT LIST
}


--------------------------------------------------------
SECTION TWO:
--------------------------------------------------------
<td colspan=\"2\"><input type=\"submit\" value=\"Submit\" name=\"submit_button\">
<input type=\"reset\" value=\"Reset Form\" name=\"reset_button\">
<a href=\"index.php\">Cancel Button Here</a>
</td>
</tr>
";

}
else {

// PROCESS FORM

// CLEAN DATA
$testimonial_title = mysql_real_escape_string($_POST['testimonial_title']);
$testimonial_hyperlink = mysql_real_escape_string($_POST['testimonial_hyperlink']);

// SEND DATA
$sql = "INSERT news_testimonial VALUES ('', '$customer_id', '$product_id', '$testimonial_title', '$testimonial_hyperlink', '$testimonial_rank')";

if (mysql_query($sql)) {
$display = "<center><br><br>Database Updated</center>";
$display .="<meta http-equiv=refresh content='5;URL=index.php'>";
} else {
die(mysql_error());
}

eelixduppy

3:45 am on Jan 13, 2007 (gmt 0)



Are
$_POST['testimonial_title']
and
$_POST['testimonial_hyperlink']
suppose to be arrays from checkboxes? If so you are going about the procedure wrong. You are going to have to loop through the array to add them to the database, or maybe even implode [php.net] them.

and Welcome to WebmasterWorld! :)

jcolgate

3:02 pm on Jan 14, 2007 (gmt 0)

10+ Year Member



Thanks for the response and the welcome.

I'm looking to pull product_id and product_name from a table, then let the visitor select one or more products using the check box system. I was hoping to display the product name, but only use the product id in the new table called news_testimonials. This way, if the admin changes how the products are spelled, all associated testimonials are updated as well.

I agree implode sounds like the best option. I'm not sure how to make that work in this situation since I'm pulling the data from one table, to another (many to one).

Thanks again for your time!

JC

jcolgate

4:48 am on Jan 18, 2007 (gmt 0)

10+ Year Member



Can anyone help out on how to pull data from a table, use check boxes to select multiple items, and store that into a new record? I believe implode would be the best option, but I'm not sure how to implement that.

Thanks,

JC

eelixduppy

4:57 am on Jan 18, 2007 (gmt 0)



Here's a quick example for implode:

<form method="post">
<input type="checkbox" value="one" name="value[]" />One<br/>
<input type="checkbox" value="two" name="value[]" />Two<br/>
<input type="checkbox" value="three" name="value[]" />Three<br/>
<input type="checkbox" value="four" name="value[]" />Four<br/>
<input type="submit" value="Click me!" />
</form>

<?php
if(isset($_POST['value'])) {
$checkboxes = $_POST['value'];
$string = implode($checkboxes,",");
echo $string;
}
?>

Once you get the $string value, you add that string to the database like you would any other string. To retrieve the information, you do that normally, but then you explode [us3.php.net] the data back into an array for processing.

Try this method out first, and see where you get. Get back to use if you need any further assistance.

Good luck! :)

jcolgate

5:30 am on Jan 18, 2007 (gmt 0)

10+ Year Member



Hmm, it works as written, but when I try to pull the data for the "Value[]" from a table and insert it I only end up with the last check box value.

Would it help if I sent you the code? Do you prefer it posted here, or sent in an email.

Thank you very much for the assistance!

JC

eelixduppy

12:12 pm on Jan 18, 2007 (gmt 0)



If you would like to post your code in the forum, that would be fine, however please only post relevant code to make it easiest to debug. If you need further clarification on this, refer to our forum charter [webmasterworld.com].

jcolgate

3:50 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



No problem...

I'll try to sum up the code:

I created a form, query data for a list of customers - make a combobox, query a list of products - make check boxes, and allow the admin to select one customer, one or multiple products, and post a short testimonial to display on the website.

The combobox works fine, the check boxes are queried and displayed assigning appropriate values to each. But when the testimonial is submitted to the database only the last check box value is stored. I can't seen to find a way to use the implode when there aren't specific set values for each check box. I kinda think I need a way to store the array upon submitting the query.

Here is a little of the code:

$display .="<input type=\"checkbox\" name=\"product_id[]\" value=\"$product_id\">$product_name<br>";

..........skip to processing the form..............

if(isset($_POST['product_id'])) {
$checkboxes = $_POST['product_id'];
$string = implode($checkboxes,",");
echo $string;
}

// SEND DATA
$sql = "INSERT news_testimonial VALUES ('', '$customer_id', '$string', '$testimonial_title', '$testimonial_hyperlink', '$testimonial_rank')";

I'm sure it is a simple step I keep missing.

Thank you again for your time and help!

JC

jcolgate

4:57 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



First, thank you very much for your help! I've solved the problem. Too many late nights coding and my mind started to melt.

Second, all I needed was:
$checkboxes = $_POST['product_id'];
$string = implode($checkboxes,",");

And, to update the database field from INT to VARCHAR. My original thought was to only have a number, not realizing the , needed the different type of field.

All that work for a simple issue.

Thank you again!

JC

jcolgate

1:11 am on Jan 19, 2007 (gmt 0)

10+ Year Member



Here I go again...

Now I have the values in the table (example: 2,3,9).

How can I build a query to explode or preg_split() the values and list the names associated with them?

Table 1 has Product_ID with Product_Name. Table 2 has a record with several values to Product_ID (2,3,9).

Here is a bit of code to help:
/$verify_product = "SELECT * FROM news_product WHERE product_id='$product_string'";
while ($newArray = mysql_fetch_array ($verify_product_res)){
$product_id = $newArray['product_id'];
$product_name = $newArray['product_name'];

echo "$product_id is $product_name";

The product id is stored as 2,3,9 and I want to list the names associated with each ID. But all I get is the first value.

jcolgate

4:51 am on Jan 19, 2007 (gmt 0)

10+ Year Member



Another nap, and I'm better.

I did explode, with foreach and it works fine.

Sorry to bug you all with the simple stuff. But, I'm sure you have all been there... late nights, lots of work to do, and you start chasing shadows.

JC

eelixduppy

3:23 pm on Jan 19, 2007 (gmt 0)



Yup, we've all been there...multiple times too!

Glad you got everything sorted! :)