Jump to content

Archived

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

kenek

Problem with $_POST[''] variables

Recommended Posts

I have just re-installed php (version 5.2.0-dev), and it's not working right. I think I have installed it properly, phpinfo() is working, but variables from a html form ($_POST['variablename'], for example) aren't passed on to PHP, it just doesn't reveive anything.. Before the re-install everything worked fine (...).

I am pretty clueless as to what might be causing this. Any help or thoughts would be appreciated.

Share this post


Link to post
Share on other sites
How do you know it isn't working correctly. Please post the test script you are using.

Ken

Share this post


Link to post
Share on other sites
I figured it wasn't working by trying to run the following example.

orderform.html:
[code]
<html>
<body>
<form action="processorder.php" method="post">
<table border="0">
<tr bgcolor="#cccccc">
  <td width="150">Item</td>
  <td width="15">Quantity</td>
</tr>
<tr>
  <td>Tires</td>
  <td align="center"><input type="text" name="tireqty" size="3"
    maxlength="3"></td>
</tr>
<tr>
  <td>Oil</td>
  <td align="center"><input type="text" name="oilqty" size="3" maxlength="3"></td>
</tr>
<tr>
  <td>Spark Plugs</td>
  <td align="center"><input type="text" name="sparkqty" size="3"
    maxlength="3"></td>
</tr>
<tr>
  <td>How did you find us?</td>
  <td><select name="find">
        <option value = "a">I'm a regular customer</option>
        <option value = "b">TV advertising</option>
        <option value = "c">Phone directory</option>
        <option value = "d">Word of mouth</option>
      </select>
  </td>
</tr>
<tr>
  <td colspan="2" align="center"><input type="submit" value="Submit Order"></td>
</tr>
</table>
</form>
</body>
</html>
[/code]

processorder.php:
[code]
<?php
  $tireqty = $_POST['tireqty'];
  $oilqty = $_POST['oilqty'];
  $sparkqty = $_POST['sparkqty'];
  $find = $_POST['find'];
?>
<html>
<head>
  <title>Order Results</title>
</head>
<body>
<?php
echo '<p>Order processed at ';
echo date('H:i, jS F');
echo '</p>';

echo '<p>Your order is as follows: </p>';

$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
echo 'Items ordered: '.$totalqty.'<br />';

if( $totalqty == 0)
{
  echo 'You did not order anything on the previous page!<br />';
}
else
{
  if ( $tireqty>0 )
    echo $tireqty.' tires<br />';
  if ( $oilqty>0 )
    echo $oilqty.' bottles of oil<br />';
  if ( $sparkqty>0 )
    echo $sparkqty.' spark plugs<br />';
}

$totalamount = 0.00;

define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);

$totalamount = $tireqty * TIREPRICE
            + $oilqty * OILPRICE
            + $sparkqty * SPARKPRICE;

echo 'Subtotal: $'.number_format($totalamount,2).'<br />';

$taxrate = 0.10;
$totalamount = $totalamount * (1 + $taxrate);
echo 'Total including tax: $'.number_format($totalamount,2).'<br />';


if($find == 'a')
  echo '<p>Regular customer.</p>';
elseif($find == 'b')
  echo '<p>Customer referred by TV advert.</p>';
elseif($find == 'c')
  echo '<p>Customer referred by phone directory.</p>';
elseif($find == 'd')
  echo '<p>Customer referred by word of mouth.</p>';
else
  echo '<p>We do not know how this customer found us.</p>';
?>
</body>
</html>
[/code]

Sorry for the long code, I picked this example because I know it was working before the re-install. No matter what I put in the orderform, I always get the same output:

"Order processed at 18:55, 22nd July

Your order is as follows:
Items ordered: 0
You did not order anything on the previous page!
Subtotal: $0.00
Total including tax: $0.00

We do not know how this customer found us."

That's why I thought the variables aren't passed on the PHP. I'm running Apache on Windows XP, by the way.

Share this post


Link to post
Share on other sites
name your form and use a if post statement ok.

try it's free lol............

if($_POST['submit'] ) {

code

}

And also in your form use a uper case letter POST

Share this post


Link to post
Share on other sites
You don't need a name in your form. Since the script that is being invoked by the form is not the same as the one that displays the form, you don't really to check if the submit button has been pressed. Upper or lowercase "post" in the method is fine.

At the top of your script put it this line:
[code]<?php echo '<pre>' . print_r($_POST,true) . '</pre>'; ?>[/code]
and see what is printed.

You can also try this test script:
[code]<?php
if (isset($_POST['submit']))phpinfo();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>test forms</title>
</head>
<body>
<form method="post">
<input type="hidden" name="test" value="testing post">
<input type="submit" name="submit" value="Send">
</form>
</body>
</html>[/code]
Which should display the phpinfo() information when you press the "Send" button. Look in there for what's in the "$_POST" array.

Ken

Share this post


Link to post
Share on other sites
Thanks Ken.

[code]
<?php echo '<pre>' . print_r($_POST,true) . '</pre>'; ?>
[/code]
prints out:

Array
(
)

Above the other output, which stays the same.

I also tried the test script, but that doesn't work either. When I press the "Send" button, the page 'refreshes' and nothing happens...

Share this post


Link to post
Share on other sites
Your code is fine, something is not right with your server if it isn't working for you.

Share this post


Link to post
Share on other sites
I already supposed there was nothing wrong with the code.. But I can't figure out what is causing the malfunction. I am on winxp and used these steps to install php: http://iamcode.net:64/projects/apache/php-win.awk - maybe something went wrong in that process?

I really hope someone might have some suggestions still, because I'm stuck right now.

Share this post


Link to post
Share on other sites
Without actually seeing the set-up on your machine in person, it's pretty hard to diagnose what when wrong with your installation remotely. You may want to try to install everything again and make note of any errors that might show. You also may want to consider installing a package like xampp http://www.apachefriends.org/en/xampp-windows.html

Ken

Share this post


Link to post
Share on other sites
Ok, thanks anyway. Would it be of any use if I post this on the PHP Freaks Apache forum, or are the posters there the same as here?

Otherwise I'll just try to install everything again. I want to install it manually, I did it before, and it worked fine..

Share this post


Link to post
Share on other sites
Any reason why you are using a dev version? Why not downgrade to the PHP5.1.x?

I have no idea why this is.

Share this post


Link to post
Share on other sites
Yes, I am planning to downgrade. I was using the dev version because I upgraded Apache to 2.2, and my php version couldn't handle that (see the link I posted earlier for details).

Share this post


Link to post
Share on other sites

×

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.