2

I am new to Php so i basically dont know much just started studying from a book php and mysql doing the same as the book said but my code doesnt fetch the data if i use Post method but does work if i use Get method. the book has used post so kind of confused here :/ this is the html part:

<!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<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 colspan="2" align="center"><input type="submit" value="SUBMIT ORDER" /></td>
        </tr>
    </table>
</form>
</body>
</html>

And the php code is this

<html>
<?php
// create short variable names
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
?>
<head>
    <title>Bob’s Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob’s Auto Parts</h1>
<h2>Order Results</h2>

<?php
echo "<p>Order processed at " .date('H:i, jS F Y')."</p>";
echo '<p>Your order is as follows: </p>';
echo $tireqty.' tires<br />';
echo $oilqty.' bottles of oil<br />';
echo $sparkqty.' spark plugs<br />';
?>

</body>
</html>

If i use the _GEt method the values of this variables are shown but with post method nothing is retrieved.

3 Answers3

1

If you used form attribute method is POST definitely you will get the data using $_POST in the php file might be there is caching issue or instead of $_POST you can use it $_REQUEST but i would suggest you clear your cache first then try again with $_POST

Also I do not think this is a code issue, rather it is a configuration issue with either apache or php. and you should only go with $_POST is more secure!

Thanks

Vinod Patidar
  • 685
  • 4
  • 17
1

Well, Are you sure post not working, Please check post method contains data or not by using.

var_dump($_POST); and var_dump($_REQUEST);

if you don't found anything in post then it will be in request if it is then your request is submitting by get method.

if it display an array with array entered in post then your post method is working and problem is with your file and your file was unable to open due to some security problem/incorrect path or may be due to file permission. So check and let me know.

Tayyab Kazmi
  • 376
  • 4
  • 19
0
<?php
    if (isset($_POST['tireqty'])) {
        $tireqty = $_POST['tireqty'];
        $oilqty = $_POST['oilqty'];
        $sparkqty = $_POST['sparkqty'];
    }
    ?>
  • 1
    Please do not post code without any supporting text. Explain what this snippet does and how it would help the OP solve their problem. Help others to understand. – APC Dec 25 '15 at 12:27
  • While this code may answer the question, it is better to explain what it does and add some references to it. – Hamid Pourjam Dec 25 '15 at 13:05