Passing variable via form

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
tpra21
Forum Newbie
Posts: 22
Joined: Wed Jul 20, 2005 6:20 pm

Passing variable via form

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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']
tpra21
Forum Newbie
Posts: 22
Joined: Wed Jul 20, 2005 6:20 pm

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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>";
}
tpra21
Forum Newbie
Posts: 22
Joined: Wed Jul 20, 2005 6:20 pm

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