I have some checkboxes, with values from 1-10, the value is placed under a column named "locationID". The problem is that if I tick in for instance 2 boxes, it only chooses to input the highest value of the two boxes, I want a new row for each box that I ticked.
My database structure: Tablename: event
Columns: column1, column2.., locationID
Example of a checkbox php code:
<input class="checkboxarna" name="locationID" style="z-index: 1;position: relative;" type="checkbox" value="1">
MySQL query in PHP: http://pastebin.com/ri7LCib2
I have searched on Google and here but, because I'm not very good at PHP for the moment, so is it really hard for me to "understand someone elses code and transform it to work for me", hope you accept that.
<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="checkbox" name="check_list[]" value="value 2">
<input type="checkbox" name="check_list[]" value="value 3">
<input type="checkbox" name="check_list[]" value="value 4">
<input type="checkbox" name="check_list[]" value="value 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check;
// Run Your Insert query here.
}
}
Hope this Helps
You need to change your form to be like this:
<input type="checkbox" name="locationID[]" value="1" />A<br />
<input type="checkbox" name="locationID[]" value="2" />B<br />
<input type="checkbox" name="locationID[]" value="3" />C<br />
Then your php needs a loop to go over the values:
$loc= $_POST['locationID'];
if ( empty( $loc ) ) {
echo("You didn't select any checkboxes");
} else {
for( $i = 0; $i < count($loc); $i++ ) {
//echo( $loc[$i] . " " ); //ideally insert into DB here
mysql_query("INSERT INTO event
(EventName, EventStart, EventEnd, FromDate, ToDate, locationID, EventValue, isActive, photo)
VALUES
('$_POST[namn]',
'$_POST[eventstart]',
'$_POST[eventend]',
'$_POST[startdate]',
'$_POST[enddate]',
'$loc[$i]',
'$AddValue',
'$isActive',
'$pic'
)
");
}
}
$location_ids = $_POST['location_id'];
if(!empty($location_ids)){
foreach($location_ids as $location_id)
{
if($location_id == 'on' || $location_id)
{
mysql_query("INSERT INTO event (EventName, EventStart, EventEnd, FromDate,
ToDate, locationID, EventValue, isActive, photo)
VALUES
('$_POST[namn]','$_POST[eventstart]','$_POST[eventend]','$_POST[startdate]',
'$_POST[enddate]', '$_POST[locationID]','$AddValue','$isActive','$pic')");
/* Sending locationID to jointable */
mysql_query("INSERT INTO `jointable` VALUES('','".$_POST["locationID"]."')");
}
}
mysql_close($con);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.