Forum Moderators: coopster

Message Too Old, No Replies

Using array for multiple form values

         

jspeed

10:50 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



I'm not sure if im going about this the right way. Im using an array to populate a drop down box of what is selected in the database, along with the other choices. That is working fine. What I need is depending on what the user selects, it also submits a number value to a different column.

here is my code so far:

--------------------------
<?php

$query = mysql_query("SELECT * FROM $dbtable WHERE status='$status'") or die(mysql_error());

$info = array('Open','Leased','Traded','Partial','Competition','Contacted');

while ($row = mysql_fetch_assoc($query))
{

echo '<select name="status" class="dropdown">';

foreach ($info as $data)
{
echo '<option'.($row['status']==$data? ' selected' : '').'>'.$data.'</option>';
}

echo '</select>';

}

?>
--------------------------------

As in:
Open = 0
Leased = 1

etc

Any pointers would be much appreciated!

cameraman

5:16 pm on Mar 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure I understand what you mean by 'different column' - is this maybe what you're looking for?

foreach ($info as $key => $data)
{
echo '<option value="' . $key . '"' .($row['status']==$data? ' selected' : '').'>'.$data.'</option>';
}

jspeed

5:48 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



I meant when the form is submitted, depending on what the user chose, e.g. Leased, it also submits a number value to a different column in the database. So you would end up with:

Status, StatusNumber
Leased, 1

cameraman

8:27 pm on Mar 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OIC. In the script that posts the data to the table, reuse the array you posted. If you use the numbers for the option values like I posted above, it's easier (in my opinion), and it's easier to sanitize since you can use intval():
$statusnumber = intval($_POST['status']);
if(($statusnumber < 0) ¦¦ ($statusnumber > count($info)) {
// Someone's monkeying with the form
}
else {
$status = $info[$statusnumber];
// do the insert/update
}

jspeed

8:38 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



Thank you for the help, I will see if i can implement your code