Page 1 of 1
Getting value from drop down menu
Posted: Tue Dec 21, 2004 2:17 pm
by Archy
OK, I am using a dynamicall generated drop down menu to execute a certain SQL function, however, I cant get the value of the object selected, and only the name.
Below is an example of the script that I am using:
Code: Select all
<select size='1' name='action".$row['id']."'>
<option value='accept".$row['id']."'>Accept</option>
<option value='decline".$row['id']."'>Decline</option>
<option value='void' selected>Do nothing</option>
</select>
The idea is that I can add/decline members easily based upon their ID number, and accept/decline unlimited amounts (within reason

) of applications simultaniously. However, as I have previously stated, I am finsind it difficult to get the actual value the drop down box gives because of its dynamically generated name.
Thanks, Archy.
Posted: Tue Dec 21, 2004 2:22 pm
by andre_c
if you have the $row['id'] on the name, why are you appending it to the value?
or, if you have it appended to the value, then you shouldn't need to have it in the name.
Posted: Tue Dec 21, 2004 2:24 pm
by kettle_drum
Take a look at the source code that it produces to see what the name of the form item actually is. If you name the form item action1, action2 etc, then you can give acccept, decline etc number values:
Code: Select all
<select size='1' name='action".$row['id']."'>
<option value='1'>Accept</option>
<option value='0'>Decline</option>
<option value='2' selected>Do nothing</option>
</select>
Then you check to see the value of $_POST['action1']; etc
Posted: Tue Dec 21, 2004 2:25 pm
by Archy
OK, a minor adjestment can be made for that.
Once I get the script working to a satisfactory level, I will change that.
Thanks fo pointing it out, do you have any idea how I can go about with my problem though?
Posted: Tue Dec 21, 2004 2:29 pm
by Archy
kettle_drum, thats my problem, I am unable to get the value from the object.
The "action1" could be "action12538542", and so checking each one would be very inneficiant, not to mention time consuming.
All I need is to get the value of what thet object is.
Posted: Tue Dec 21, 2004 2:33 pm
by kettle_drum
Well if you dont know what the values will be loop through the $_POST array and then do a substr on it, to check to see if the first 6 letters of the key name is 'action'. I was just assuming that if you know the values when you print the form, you can use the same info to check the values on submition.
Posted: Tue Dec 21, 2004 2:38 pm
by andre_c
like i said, take out the rowid out of the name, then you know that the name will be action, and you can grab the row id from the value with a simple string function.
Posted: Tue Dec 21, 2004 3:11 pm
by kenrbnsn
Instead of appending the id to the name, make the name an array with the id as the index:
Code: Select all
<select size='1' name='action[".$row['id']."]'>
<option value='accept'>Accept</option>
<option value='decline'>Decline</option>
<option value='void' selected>Do nothing</option>
</select>
Then in your processing program, do something like:
Code: Select all
$action = (isset($_POST['action'])?$_POST['action']:array();
foreach($action as $id => $act)
switch ($act) {
case 'accept':
// do work
break;
case 'decline':
// do work
break; }
Please note: This code is untested. I just created it.
Ken