Jump to content

[SOLVED] need refresher on form checkboxes with value going to mysql db


simcoweb

Recommended Posts

I have a rather simple form that has several fields using checkboxes. I've set the code up in 'groups' with the field names set up as arrays like this:

 

<table border="0" cellpadding="0" style="border-collapse: collapse" width="100%" id="table3">
        <tr>
          <td class="revform" style="border-right:1px dotted #A1634A; border-bottom:1px dotted #A1634A; padding:3px; " valign="top">
          <input type="checkbox" name="time_horizon[]" value="1 to 3 years">1 to 3 years</td>
          <td class="revform" style="border-right:1px dotted #A1634A; border-bottom:1px dotted #A1634A; padding:3px; " valign="top">
          <input type="checkbox" name="time_horizon[]" value="3 to 5 years">3 to 5 years</td>
          <td class="revform" style="border-right:1px dotted #A1634A; border-bottom:1px dotted #A1634A; padding:3px; " valign="top">
          <input type="checkbox" name="time_horizon[]" value="5 to 10 years">5 to 10 years</td>
          <td class="revform" style="border-bottom:1px dotted #A1634A; padding:3px; " valign="top">
          <input type="checkbox" name="time_horizon[]" value="10 years or more">10 years or more</td>
        </tr>
      </table>

 

My recollection tells me to name the fields common names and use the [] array attribute at the end of them which i've done. Then i'm using regular variable settings to retrieve the data like this:

 

$objective = $_POST['name'];
  $time_horizon = $_POST['time_horizon'];
  $expected_return = $_POST['expected_return'];
  $pre_tax = $_POST['pre_tax'];
  $willingness = $_POST['willingness'];
  $performance = $_POST['performance'];
  $situations = $_POST['situations'];

 

Then inserting the results into a mysql database. But, all I get inserted is the word 'Array' instead of the value checked. Anyone?

Link to comment
Share on other sites

My recollection tells me to name the fields common names and use the [] array attribute at the end of them which i've done. Then i'm using regular variable settings to retrieve the data like this:

 

Your recollection would be correct if you wanted the person to select 1 or more checkboxes. It seems to be as though you only want them to select one. As metrostars pointed out rename it to time_horizon and it should work. Also you may want to witch to radio buttons if that is the case, I am not sure if renaming the fields all the same will allow for multiple checks or not, but I know with radio buttons naming them all the same will allow the user to only choose one.

 

Link to comment
Share on other sites

Hmmm... well, that worked. And, i've done tons of forms without the brackets before. But, if you Google a search for a checkbox tutorial you see they all use the array brackets. It makes sense to use 'em but obviously the results were not coming through properly.

 

frost, I may go ahead and do that... switch to radio buttons...to avoid confusion. quick fix so no problems on that. Thanks!

 

ok, i'll consider this one solved :)

Link to comment
Share on other sites

  • 1 month later...

<?php
foreach ($_POST['PICTURES'] as $row=>$PICTURES)
{
      $pictures = mysql_real_escape_string($row);
      $persons = $_POST['persons'];
      mysql_query('INSERT INTO relations (relPics, person) VALUES ($pictures, $persons)') or die(mysql_error());

}
?>

 

basically what i have is a list of pictures with check boxes, I want those check boxes if selected to insert into a table with the "persons" variable. the code above is what i basically have where the check box is the PICTURES. the form names the variable as PICTURES and the value should be passed if it's been checked and only if it's been checked correct? this then forms an array, i thought this handled the array.

Link to comment
Share on other sites

the form generates it's values from two tables:

 

persons are generated here:

 

$result = mysql_query("SELECT * FROM persons");
$output="<tr>";
while($row = mysql_fetch_array($result))
{
        $counter = $counter + 1;
        $output .="<!-- START ROW FOR Radio " .$row['name']."--> ";
        $output .="        <td width=10 id=left10 align=left valign=top >".$counter.". ";
        $output .="        </td>";
        $output .="        <td width=120 id=middle align=left valign=top >";
        $output .="        <input type=radio name=PERSONS value=".$row['name']." >".$row['name'];
        $output .="        </td>";
        $output .="        <td width=10 id=right10 valign=top ></td>";
        $output .="<!-- END ROW FOR Radio " .$row['Name']."--> ";
              if ( $counter % 1 == 0 )
              {
                     $output .= "</tr><tr>";
              }
}
$output .="</tr>";
// open the file
$fhandle = fopen( "FormParts/personsBullets.php", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );

 

and pictures (the checkboxes) are generated here:

 

$result = mysql_query("SELECT * FROM pictures");
$output="<tr>";
while($row = mysql_fetch_array($result))
{
        $counter = $counter + 1;
        $output .="<!--START ROW FOR checkbox " .$row['fileName']." --> ";
        $output .="        <td width=10 id=left10 align=left ><!--".$counter."--><br></td>";
        $output .="        <td width=120 id=middle align=center valign=top >";
        $output .="        <a href=".$fileURL."".$row['fileName']." ><img src=".$fileURL."THUMBS/".$row['fileName']." ></a><br>";
        $output .="        <input type=checkbox name=PICTURES value=".$row['fileName']." />".$row[picTitle];
        $output .="        </td>";
        $output .="        <td width=10 id=right10  ><br></td>";
        $output .="<!-- END ROW FOR checkbox " .$row['fileName']." -->";
              if ( $counter % 4 == 0 )
              {
                     $output .= "</tr><tr>";
              }
}
$output .="</tr>";
// open the file
$fhandle = fopen( "FormParts/pictureCheckBoxes.php", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );
// close the file
fclose( $fhandle );
include('FormParts/pictureCheckBoxes.php');

 

Form is this simple:

 

<form id="makerelation" action="makerelation.php" method="POST">
<fieldset>
<legend>Relate pictures and person</legend>
<table id="makerelations" border="0" colspan="0" cellpadding="0" cellspacing="0" align="left">

<tr>
      <td align="left">
      Choose a person
      </td>
      <td width="5"><br>
      </td>
      <td align="left">
      Pick the Pictures that include the person you chose.
      </td>
</tr>
      <td align="left" valign="top">
            <table id="persons" width="140" border="0" colspan="0" cellpadding="0" cellspacing="0" align="left">
            <tbody valign="top">
                  <?php include('makerb4allpeople.php'); ?>
            </tbody>
            </table> <!-- persons -->
      </td>
      <td width="5"><br>
      </td>
      <td align="left">
            <table id="pictures" border="1" colspan="0" cellpadding="0" cellspacing="0" align="left">
                  <?php include('makecb4allpictures.php'); ?>
            </table> <!-- pictures -->
      </td>
</tr>
<tr>
      <td colspan="3" align="center">
           <input type="submit" value="submit" name="Submit" />
           <input type="reset" value="reset" name="Reset" />
      </td>
</tr>

 

 

Form handler

 

foreach ($_POST['PICTURES'] as $row=>$PICTURES)
{
      $pictures = mysql_real_escape_string($row);
      $persons = $_POST['persons'];
      mysql_query('INSERT INTO relations (relPics, person) VALUES ($pictures, $persons)') or die(mysql_error());
      echo $pictures." ".$persons;
}

Link to comment
Share on other sites

This line in your form output is missing a double quote and period to close out your value:

 

$output .="        <input type=checkbox name=PICTURES value=".$row['fileName']." />".$row[picTitle];

 

should be:

 

$output .="        <input type=checkbox name=PICTURES value=".$row['fileName']." />".$row[picTitle].";

Link to comment
Share on other sites

HMMM yeah that's not really a problem, the output from the form is fine, it's the processing of the insert that i'm having issues with. getting the variables from checkboxes into the database.

 

should have a listing like this

 

person a radio

person b radio

person c radio

...

 

pic A Checkbox title Value = file1.jpg

pic B Checkbox title Value = file2.jpg

pic C Checkbox title Value = file3.jpg

...

 

if i select pic B and pic C with radio a

 

radio a relates to pic B, pic C

 

it should be doing an insert for each B and C with person A

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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