Click to See Complete Forum and Search --> : inserting checkbox ID's into a lookup table?


kmiec
06-30-2003, 12:48 AM
I am creating a form that allows the user to Submit a property with it's address and ammenities. I have the ammenities listed in a form table with check boxes. I want the user to be able to check all the different ammenities that aply to the house. I'll show a an example of the table.

tr>
<td width="4%"><input type="checkbox" name="Furnished" value="1"></td>
<td width="12%">Furnished</td>
<td width="4%"><input type="checkbox" name="Unfurnished" value="2"></td>
<td width="15%">Unfurnished</td>
<td width="4%"><input type="checkbox" name="Capreted" value="3"></td>
<td width="15%">Capreted</td>
<td width="4%"><input type="checkbox" name="HFloors" value="4"></td>
<td width="17%">Hardwood Floors</td>
<td width="2%"><input type="checkbox" name="Stove" value="5"></td>
<td width="23%">Stove</td>
</tr>

What I want is to have the value of the checked boxes (the Amenities ID) insterted in a lookup table (AID / PID) with the Property ID of the users Property.

I'm sure I'll have to do some manual coding but I've been trying and can't get it to work. Does anyone have any Ideas how to get this done in Dreamweaver.

Thanks

ahundiak
06-30-2003, 08:57 AM
Actually, you can (and should) have a value for check box elements. It's the value that ends up being submitted when the box is checked.

kmiec
06-30-2003, 12:39 PM
I'm sure that you can have a value for the checkbox. And it is the value of the Amen's ID that I would like to insert to the Lookup Table. Can anyone actually explain what I can do to have that value inserted to the lookup table with the Property ID? ie.

PID / AID Where Property ID = 3
------------
3 1
3 3
3 5

Thanks

goa103
07-01-2003, 04:52 AM
Originally posted by kmiec
I'm sure that you can have a value for the checkbox. And it is the value of the Amen's ID that I would like to insert to the Lookup Table. Can anyone actually explain what I can do to have that value inserted to the lookup table with the Property ID? ie.

PID / AID Where Property ID = 3
------------
3 1
3 3
3 5

Thanks

You don't need a value for the checkbox. Its value is 1 or 0 (TRUE or FALSE).
Update by goa103 : I can't believe i wrote that :). Set the VALUE of the checkbox to an id (user id for a user selection...), once submitted a PHP variable is created for all checked checkboxes : $_POST ['my_checkbox'] returns the VALUE of the checkbox.

Use a hidden field to store your PID or AID :

<input type="hidden" name="pid" value="<? $pid ?>">
...

JM

ahundiak
07-01-2003, 09:04 AM
goa103,
I don't think your post is entirely accurate. An unchecked checkbox does not post at all. It's simply skipped and it does not get a value of 0. A posted checkbox will have either a value of 'on' or whatever the value attribute has.

<form method="post" action="test.php">
<input type="checkbox" name="box">Check Me</input><br />
<input type="checkbox" name="boxes[]" value="24">Check Me</input><br />
<input type="checkbox" name="boxes[]" value="42">Check Me</input><br />
<input type="submit" name="submit" value="Submit">
</form>
<?php
error_reporting(E_ALL);
if (isset($_POST['submit']))
{
if (isset($_POST['box']))
{
echo 'BOX ' . $_POST['box'] . '<br />';
}
else echo 'BOX NOT SET <br />';

if (isset($_POST['boxes']))
{
$boxes = $_POST['boxes'];
foreach ($boxes as $box)
{
echo 'BOXES ' . $box . '<br />';
}
}
else echo 'BOXES NOT SET <br />';
}
?>

Try submiting with and without the 'box' element being checked. See the difference?

Where it becomes more challenging is when you have an array of check boxes. Unchecked boxes don't even post so the only way of knowing which boxes were checked is being giving them a unique value.

kmiec
07-01-2003, 11:09 PM
ahundiak

Your post actually makes a lot of sense and I see the difference between the box checked and not checked. Now, what would the code look like to insert the value into a lookup table if the values of the checkboxes were to be inserted into the AID and the value of a hidden field entered into the PID?

Thanks

goa103
07-01-2003, 11:45 PM
Originally posted by ahundiak
goa103,
I don't think your post is entirely accurate. An unchecked checkbox does not post at all. It's simply skipped and it does not get a value of 0. A posted checkbox will have either a value of 'on' or whatever the value attribute has.

Well actually I think my answer doesn't make sense at all :). I can't believe I wrote that so I updated my post. Probably because I'm jumping between PHP and a lot of other technologies.

So the checkbox VALUE matters. Read my update for more info.

Sorry again for the weird reply, I think I lost it :)
JM

ahundiak
07-01-2003, 11:58 PM
function db_query($sql)
{
$mysql_query($sql);
if (mysql_errno() == 0) return;
die(htmlspecialchars(mysql_error()) . '<br />' . htmlspecialchars($sql));
}

$pid = $_POST['pid'];
$sql = "DELETE FROM pid_aid WHERE pid=$pid";
db_query($sql);

if (isset($_POST['aids'])) $aids = $_POST['aids'];
else $aids = array();

foreach($aids as $aid)
{
$sql = "INSERT INTO pid_aid VALUES($pid,$aid)";
db_query($sql);
}

That should get your started.
Make sure the user actually pressed the submit button. And create a unique index on just to make sure you never get duplicates.