unistake Posted September 11, 2009 Share Posted September 11, 2009 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 More sharing options...
Adam Posted September 11, 2009 Share Posted September 11, 2009 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 } Link to comment https://forums.phpfreaks.com/topic/173885-check-boxes-and-values/#findComment-916624 Share on other sites More sharing options...
unistake Posted September 11, 2009 Author Share Posted September 11, 2009 hi adam, yeah that looks like it, ill give it a try and put a post up as to how I get on! Cheers Link to comment https://forums.phpfreaks.com/topic/173885-check-boxes-and-values/#findComment-916625 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.