Getting value from drop down menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Getting value from drop down menu

Post 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.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post 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?
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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.
kenrbnsn
Forum Newbie
Posts: 13
Joined: Tue Jul 01, 2003 3:34 pm
Location: Hillsborough, NJ USA

Post 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
Post Reply