Page 1 of 1

Passing variable via form

Posted: Wed Jul 27, 2005 3:56 pm
by tpra21
This is kind of confusing, but I'll try my best to explain. I allow a user to view thier posted housing ads at the same time (say they have two ads). At the bottom of each ad, there are two links that say "EDIT AD DELETE AD."

For example, if the user clicks on the "EDIT" link of the first ad, that PHP file (view_ads.php) sends a "file id" of 1 through the link to the edit.php file. Now, we are at the actual file where you make changes to your ad and then click submit. My problem, after I click "submit" button on my form (using PHP SELF to redirect back to a specific block of code still within the edit.php file), I lose the "file id" that I need.

How can I pass a variable within the "action" command of the form to the new block of code within that same file?

Thanks for any help, I can post my code if needed.

Posted: Wed Jul 27, 2005 4:10 pm
by John Cartwright
What I usually end up doing is

1) Store the file id in a session (requires variable cleanup after)
2) Store the file id in a hidden input field

If you choose #2 your going to have to look out for what method you are accessing the variable.
The easiest way is to simply change $_GET['file_id'] to $_REQUEST['file_id']

Posted: Wed Jul 27, 2005 8:14 pm
by tpra21
I tried what you mentioned, and still can't get the value (which is 1) to transfer.

Here is what I am using in the form, with the code in bold the main focus:

Code: Select all

<p><center>
<form action = "<? echo $HTTP_SERVER_VARS['PHP_SELF']?>" method="post">
<table><tr><td>
<p align="justify"><font size="2" style="Trebuchet MS">Click the button below to delete your ad. This process cannot be reversed, so please make sure that you wish to delete this ad.</font></p></td></tr><tr><td colspan="2" align="center">

<input type="submit" name="delete" value="Delete Ad">
<? echo "<INPUT TYPE= \"hidden\" VALUE=\"$id\"NAME= \"id\">"; ?> 
</td></tr></table>
</form> 
<BR><BR><br><br><BR><BR><BR><BR><BR><br><br><BR></center></p>
I then retrieve the variable by the following, only it is not correctly retrieving it:

Code: Select all

<?
if(isset($_POST['delete'])){

  $id=$_REQUEST['id'];

  echo $id;
}
I should be echoing a value of 1, but I am echoing nothing.

Thanks for your help, Adam

Posted: Wed Jul 27, 2005 8:33 pm
by timvw
Are you sure you are echoing?

Code: Select all

if(isset($_POST['delete'])){
  $id=$_POST['id'];
  echo "the id: $id <br>";
}

if (isset($HTTP_POST_VARS['delete']))
{
  $id=$HTTP_POST_VARS['id'];
  echo "the other id:$id <br>";
}

Posted: Wed Jul 27, 2005 8:38 pm
by tpra21
Yeah, I tried your code and I get the following written to the screen:

id is:

It is just left blank from where the variable is not going. It is really blowing my mind. As always, thanks for the continued help guys/gals!

Adam