2

I have set up a template form that sends the symbol selected by the user to the controller.

I cannot get the form to show when I request the page.

Here is my code for the form:

<form action="sell.php" method="post">
<fieldset>
    <div class="form-group">
        <select class="form-control" name="symbol">
                <option value=""> </option>
                <?php$rows=query("SELECT * FROM portfolio WHERE id=?" , $SESSION["id"]);?>
                <?php foreach $rows as $row:?>
                    print("<option value=\"{$row["symbol"]}\">{$row["symbol"]}</option>");
                <?php end foreach ?>
        </select>
    </div>
    <div class="form-group">
        <button type="sell" class="btn btn-default">Sell</button>
    </div>
</fieldset> 
</form>

This is my code for the controller:

<?php

//Configure page
require("..//includes/config.php");   

if ($_SERVER["REQUEST_METHOD"] == "POST")
{    
     //check if the field was filled    
    if(empty($_POST["symbol"]))
    {
        apologize("You must submit a symbol");
    }

    else

    {
      //code to deal with successful post
    }

else
{
    apologize("There was an issue confirming your sale.");
}    
?> 

When I request the page in my browser is takes me straight to the last line indicating that the post was not successful but I cannot decipher why that is.

0

1 Answer 1

4

The request method is GET by default. When the user visits http://pset7/sell.php, she is requesting the page (i.e., sell.php in this case) using GET. Therefore, the condition

$_SERVER["REQUEST_METHOD] == "POST"

is initially false and the else branch is executed as a result.

To fix this, you need to render the template if the requesting method is not POST (i.e., GET) rather than apologizing.

4
  • Thanks @Kareem! It renders the sell form now. One last problem: it provides the following error now--> Parse error: syntax error, unexpected '$rows' (T_VARIABLE), expecting '(' in /home/jharvard/vhosts/pset7/templates/sell_form.php on line 7
    – ascar
    Dec 23, 2014 at 9:57
  • @ascar check the syntax of the foreach loop because you're missing something!
    – kzidane
    Dec 23, 2014 at 10:03
  • Ah, solved! I've seen a couple of your answers and you have been really helpful. Thanks a lot!
    – ascar
    Dec 23, 2014 at 10:08
  • @ascar you may up-vote this answer if it was helpful and accept it to mark the question as solved. Thank you!
    – kzidane
    Dec 23, 2014 at 10:10

You must log in to answer this question.

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