0

Here is my buy form

<html>
<form action="buy.php" method="POST">
    <fieldset>
        <div class="form-group">
            <input  autofocus name="symbol" placeholder="Symbol" type="text" size="20"/>
        </div>
        <div class="form-group">
            <input name="nbrshares" placeholder="Number of shares" type="number" size="20"/>
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-default">Buy</button>
            </br> 
        </div>
    </fieldset>
</form>
</html>

Here is the start of the Post section of buy.php

     // else if user reached page via POST (as by submitting a form via POST)
     else if ($_SERVER["REQUEST_METHOD"] == "POST")
     {
        echo "<pre>";
            print_r($_POST["symbol"]);
            echo  "</pre>"; 
        echo "<pre>";
            print_r($_POST["nbrshares"]);
            echo  "</pre>";     

        // if no stock entered
        if (empty($_POST["symbol"]))
        {
            apologize("Please enter a stock symbol.");
        }
        // if no shares entered
        else if (empty($_POST["nbrshares"]))
        {
            apologize("Please enter a stock quantity to buy.");
        }
1
  • Shouldn't you be checking if the fields are empty before you try to print their arrays? Jun 18, 2015 at 19:34

2 Answers 2

0

It could be that your form is not submitting. You can check by naming your submit button to submit and
if(isset($_POST["submit"]) { //echo something }

1
  • Thanks for the help. Very much appreciated. I'm inspired to check for empty going fwd. It turns out the problem was an issue not shown. I accidentally had 2 if statements for the GET part and those were the culprits. The disadvantage of trying to be precision in excerpting code :)
    – yorroy
    Jun 19, 2015 at 13:17
0
else if ($_SERVER["REQUEST_METHOD"] == "POST")
 {
    echo "<pre>";
        print_r($_POST["symbol"]);
        echo  "</pre>"; 
    echo "<pre>";
        print_r($_POST["nbrshares"]);
        echo  "</pre>";

Here, you have to print the value of a key(symbol) from the array $_POST What you're doing is printing an array

instead of

print_r($_POST["symbol"]);

use

echo $_POST["symbol"]   // $_POST["symbol"] is a value, not an array

or

print_r($_POST); //$_POST is an array

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .