1

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.

3
  • Change your name into array, then you will get all check boxes:<input class="checkboxarna" name="locationID[]" style= "z-index: 1; position: relative;" type="checkbox" value="1"><input class="checkboxarna" name="locationID[]" style= "z-index: 1; position: relative;" type="checkbox" value="2"> Commented Feb 22, 2013 at 9:41
  • I tried that now. But then it only insert the text "Array" in my db instead of a number. Commented Feb 22, 2013 at 9:49
  • you need to run a for loop to check the value for each checkbox. If you want to save only checked on , then check the value with "on", if you want to save all, just run a for loop and retrieve individual check box value and then run the query. Commented Feb 22, 2013 at 9:52

3 Answers 3

3
<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

Sign up to request clarification or add additional context in comments.

Comments

2

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'
           )
         ");
       }
     }

6 Comments

Hi! I guess it my fault, but i cant get it to work. I have set [] on my checkboxes in the code correctly. Then i inserted your loop you gave me in my code, unsure if i doing that right, so will give you a copy of my code, look here: pastebin.com/pAjTkGPv Its only inserts the text "Array" insted of a number, guess that mena ssomething with the loop is wrong. And its still only inputs 1row only, guess it doess that because the loop is wrong. Thanks for help!
Yes, that is what your code will do. You are inserting into the database first, then running the loop. You need to insert into the database inside the loop (to get multiple inserts), see where I have commented "ideally insert into DB here"
Also your inserting "$_POST['locationID']" into the database which we know is an Array, thats why its inserting the word Array. You need to insert $loc[$i].
Changed my code to contain your SQL query from the pastebin code. ps $_POST['namn']? isn't that meant to be $_POST['name']?
for( $i = 0; $i < count($loc); $i++ ) ... bad idea to put counr($loc) in for loop. put $cnt_loc = count($loc); and then for( $i = 0; $i < $cnt_loc; $i++ )
|
0
$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.