Jump to content

Trouble passing form variables


desolati0n

Recommended Posts

Hello,

 

I hope I'm posting this in the correct place. Basically I'm trying to make a page that will let you change the order in which some items are displayed. The problem I'm having is passing back the id value of the item that you want to move up or down in the display order. Here is the code:

 

elseif($op=='editdisplayorder'){
	$button = $_POST['button'];
	$editid = $_POST['id'];
	if($button == 'Move Up'){
		//Move Position up a spot
	}
	if($button == 'Move Down'){
		//Move Position down a spot
	}
	$sql->Query("SELECT id, title, location, posid FROM careers ORDER BY posid");

		print "

		<center>

		<table border='1' width='60%'><form action='index.php?&op=editdisplayorder' method='post'>

		<tr bgcolor='#FFFFFF'><th colspan='4'>Open Positions</th></tr>
		<tr><td><center><b>Position</b></center></td>
		    <td><center><b>Location</b></center></td>
		    <td><center><b>Display Order</b></center></td></tr>";


		for($i=0; $i<$sql->rows; $i++){

			$sql->Fetch($i);

			$id = $sql->data[0];

			$title = $sql->data[1];

			$location = $sql->data[2];
			$posid = $sql->data[3];

			print "<input type='hidden' name='id' value='$id'>
				<tr><td><center>$title</center></td>					   
                                           <td><center>$location</center></td>						
				   <td><center><input type='submit' name='button' value='Move Up'>
					       <input type='submit' name='button' value='Move Down'></center></td></tr>";

		}

		print "<tr></tr>

		</form></table>

		<br><br><br>

		";

 

 

I'm trying to use a hidden input type to pass back the id value of the item (career position) when one of the buttons ('Move Up' or 'Move Down') are selected, but I want the hidden id value to be unique to each button. But instead right now the value of id that is passed back by each button is the same as the last value that id was set to inside the for loop. For example the table looks kind of like this:

 

    Career Position 1  [move up] [move down]

 

    Career Position 2  [move up] [move down]

 

    Career Position 3  [move up] [move down]

 

 

And I want the "move up" and "move down" buttons for 'Career Position 1' to pass back the id value of 'Career Position 1', but instead it passes the id value of 'Career Position 3'. Any help at all with this would be appreciated, and I hope I explained it clear enough.

 

Thanks,

 

desolati0n

Link to comment
https://forums.phpfreaks.com/topic/71986-trouble-passing-form-variables/
Share on other sites

Why dont you just use links rather than submit buttons? You can then pass the id in the URL.

 

The reason why you always seem to get the ID of the last item is that all of your hidden fields have the name 'id', hence when you retrieve the value from the GET/POST array, its always the last one.

 

Edit: If you particularly want to use submit buttons, i suggest you use a bit of javascript to update the value of a single hidden field prior to form submission.

Archived

This topic is now archived and is closed to further replies.

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