0

I have a pretty simple form with some text fields and 2 check boxes. The form works except if both checkboxes are checked, only the first one will add to the database.

I have this so far:

<?php require_once('connectionsettings.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
. . .

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
    # ------------ edit begins here ------------ #
    if(count($articletags) > 0)
    {
        $articletags_string = implode(",", $articletags);
    }
    # ------------ edit ends here ------------ #

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
    $insertSQL = sprintf("INSERT INTO articles (articletitle, articledescription, articletags) VALUES (%s, %s, %s)",
                   GetSQLValueString($_POST['articletitle'], "text"),
                   GetSQLValueString($_POST['articledescription'], "text"),
                   GetSQLValueString($_POST['articletags[]'], "text")); # --Edited here also

 mysql_select_db($database_MySQL_CSFTDB, $MySQL_CSFTDB);
 $Result1 = mysql_query($insertSQL, $MySQL_CSFTDB) or die(mysql_error());
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
. . .
  <form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">
  . . .
       <tr>
        <td align="right">&nbsp;</td>
       <td align="left"><p>
          <label> # -- added []s to article name --
          <input type="checkbox" name="articletags[]" value="checkbox" id="articletags_0" />
          Checkbox</label>
        <br />
        <label>
          <input type="checkbox" name="articletags[]" value="checkbox 2" id="articletags_1" />
          Checkbox</label>
         <br />
         <br />
        </p></td>
     </tr>
      . . .
</body>
</html>

5 Answers 5

2
  Use checkbox as array so it can store multiple value.

<input type="checkbox" name="articletags[]" value="checkbox" id="articletags_0" />
<input type="checkbox" name="articletags[]" value="checkbox 2" id="articletags_1" />



 $articletags = $_POST['articletags'];

Convert the Array into string before insert the value to db.

if(count($articletags) > 0)
{
 $articletags_string = implode(",", $articletags);
}

Insert new value in db ...

2
  • Pramond - I've attempted to make the edits you suggest, but now I receive null entries into the article tags column. Which part did I mess up? I added comments to show where I made additions or edits.
    – Zrb0529
    Jun 7, 2012 at 20:28
  • Really very short and sweete method then implement serialization Mar 4, 2013 at 6:36
2

This is beacuse you have used same name for both check boxes.

You can either provide different names or use an array name

<input type="checkbox" name="articletags[]" value="checkbox" id="articletags_0" />
<input type="checkbox" name="articletags[]" value="checkbox 2" id="articletags_1" />

Which will be send as an array to the server.

$articletags=$_POST['articletags']
print_r($articletags)
1

When inputs share a name, PHP will throw away all but one of them unless that name ends in the characters [].

Change to name="articletags[]" (or bypass $_POST and access the raw form data).

You'll also need to change your logic for building the SQL so that you loop over the values in the array instead of just checking to see if the articletags has been defined.

0

You have the same name in both checkboxes, try change it and put the name equal to the id so they can be distinguish in the php.

Something like this :

<label>
           <input type="checkbox" name="articletags_0" value="checkbox" id="articletags_0" />
           Checkbox</label>
          <br />
         <label>
             <input type="checkbox" name="articletags_1" value="checkbox 2" id="articletags_1" />
             Checkbox</label>
0

In Checkboxes and Radio Buttons, we use the same name if we need a single value. To your question, you want both the values which means you are supposedly use different name or an array.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.