Jump to content


Photo

_POST w/hidden fields within _GET page


  • Please log in to reply
5 replies to this topic

#1 Skor

Skor
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 16 October 2006 - 02:34 AM

???
--------------------------------------------------------------------------------

Am trying to do two things on a page, retrieve db info and display it through a $_GET which I'm functionally able to do on a product details page (details.php?id=7). I'm also trying to populate hidden fields in the Paypal hidden form. Through error messages I know I'm not declaring the variables correctly. I've done searches but have been unable to find what I'm doing wrong in this scenario. Can someone please assist.

Strangely, I was able to get the page without error, but not with the results I wanted. All I'm looking to do is have variables (Price and Name) from a query populate a form's hidden fields. Here are some of the changes I made to get it to not error.

Here's what I'm getting on the processed code:

<input type="hidden" name="item_number" value="2">
<input type="hidden" name="item_name" value="">
<input type="hidden" name="amount" value="">


Thanks in advance.

if(isset($_GET['Prodid']) && !empty($_GET['Prodid'])){
$Prodid = $_GET['Prodid'];
$Name = isset($_POST['Name']) ? $_POST['Name'] : "";
$Price = isset($_POST['Price']) ? $_POST['Price'] : "";
//$Name = $_POST['Name'];
//$Price = $_POST['Price'];
$query = "SELECT Prodid, Name, Category, Size, Description, Price
FROM testdb
WHERE Prodid = '".$_GET['Prodid']."'
AND Status = 'A'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
} else {
echo "Cannot find the product ID or product ID is empty.";
}




?>
<table border ="2">

<?php


echo '<tr><td>Prodid</td><td>Name</td><td>Description</td><td>Size</td><td>Category</td><td>Price</td></tr>';

$cols = mysql_num_fields( $result );

while ($row = mysql_fetch_array($result))

{
echo "<tr>";
echo "<td>".$row['Prodid']."</td>";
echo "<td>".$row['Name']."</td>";
echo "<td>".$row['Description']."</td>";
echo "<td>".$row['Size']."</td>";
echo "<td>".$row['Category']."</td>";
echo "<td>".$row['Price']."</td>";
echo "</tr>";
}

?>
</table>


<form target="paypal" action="https://www.paypal...." method="post">
<table>
<tr>

<input type="hidden" name="item_number" value="<?php Print $Prodid;?>">
<input type="hidden" name="item_name" value="<?php Print $Name;?>">
<input type="hidden" name="amount" value="<?php Print $Price;?>">
</form>

#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 16 October 2006 - 02:56 AM

There's a misconception underlying your code which is probably causing much trouble.

Any request to a PHP script will be either a GET or a POST.  It can't be both.  If it's a GET, then $_GET will be populated.  If it's a POST, then $_POST will be populated.  Never will you have variables in both $_GET and $_POST.

If you want to set hidden fields in that post form, you should not be using $_POST at all.  $_POST is for requests to your script, but that paypal form goes to someone else's script (it goes to paypal).  So, $_POST should never be mentioned in your code (unless you have another post form somewhere).

I think an essential thing to understand is that only one form is ever submitted in any request.  Only data in that form is available to your script.  The data in the post form is never available to you, because it goes to another script.  If you want access to that data, you must add it to your other form as well.

Good luck :)

#3 Skor

Skor
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 16 October 2006 - 03:06 AM

Shall I just use basic variables to retrieve data values from the db query.  Can I do that on a _GET page?

Something like this: $Prodid = $row[0].  If that is the case what is the syntax for the hidden fields, $variable?

As a newbie, looks like I took the path that led to a dead end.  Would love to sort this out tonite.

#4 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 16 October 2006 - 03:26 AM

Try using $row['Name'], $row['Prodid'], $row['Price'] inside the paypal form.

And delete any reference you have to $_POST.  Those lines have no effect, because $_POST is never set, only $_GET.

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 16 October 2006 - 03:33 AM

To btherl: you are mistaken. You can have both GET and POSTed variables passed to a PHP script, but it's not good programming.

Consider the following script:
<?php
echo '<pre>$_GET:' . print_r($_GET,true) . '</pre>';
if (isset($_POST['submit'])) echo '<pre>$_POST:' . print_r($_POST,true) . '</pre>';
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?p[]=1&p[]=2">
Posted Param1: <input type="text" name="posted_p[]"><br>
Posted Param2: <input type="text" name="posted_p[]"><br>
<input name="submit" value="Do Test" type="submit">
</form>

If you run this script, you will see both the $_GET array and the $_POST array populated after submitting the form.

Ken

#6 Skor

Skor
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 16 October 2006 - 03:37 AM

Was just trying that as your reply came in.  

This is what the results still look like:

<input type="hidden" name="item_name" value="">
<input type="hidden" name="amount" value="">

Here's what I put in.  Am I just missing something obvious here?

<input type="hidden" name="item_name" value="<?php Print $row['Name'];?>">
<input type="hidden" name="amount" value="<?php Print $row['Price'];?>">

Have subsequently tried the following:

<input type="hidden" name="item_name" value="<?php echo $result[1]; ?>">
<input type="hidden" name="amount" value="<?php echo $row->Price; ?>">

which both generate an empty hidden field tag:  ""
Any other avenues to try??

Thanks.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users