Jump to content

check boxes and values


unistake

Recommended Posts

Hi all,

 

I am not sure of the best way to do this:

 

There will be about 20 checkboxes all with the same name however they will ofcourse have different values.

When the checkboxes are selected, I want them to go to a PHP page where

 

"if the checkbox is ticked, add the value to the database".

 

As a newbie to php I am not sure whether this is a good example of where to use an array?! If so could you show me a bit of code on how I would go about making it.

 

the idea code I have is below - if its any help!

 

Thanks

 

<?php

$value = $_POST['checkbox_value']
if ( $value > 0) {

$sql = "INSERT INTO table VALUES ('$value','date')";
$result = mysql_query($sql)
    or die ("cant insert values");

echo "you have added the values $value to your account";
}

else {
  echo "you have not selected any values";
}

Link to comment
https://forums.phpfreaks.com/topic/173885-check-boxes-and-values/
Share on other sites

Yeah an array is your best bet, something like...

 

HTML side of things:

<input type="checkbox" name="name_here[]" value="checkbox value here" />
<input type="checkbox" name="name_here[]" value="another value here" />

 

PHP side:

foreach ($_POST['name_here'] as $checkbox_value)
{
    // insert $checkbox_value into DB
}

-----------

 

Not sure if this is the direction you're wanting to go in, but you could extend this to add in a unique name for each check box too, like this:

 

<input type="checkbox" name="name_here[name1]" value="name 1 value" />
<input type="checkbox" name="name_here[name2]" value="name 2 value" />

 

foreach ($_POST['name_here'] as $checkbox_name => $checkbox_value)
{
    // insert $checkbox_name and $checkbox_value into DB
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.