0

public/quote.php

           <?php

// configuration
require("../includes/config.php"); 
 if ($_SERVER["REQUEST_METHOD"] == "GET")
{
    // else render form
    render("../views/quote.php", ["title" => "Quote"]);
}

// else if user reached page via POST (as by submitting a form via POST)
else if ($_SERVER["REQUEST_METHOD"] == "POST")
{
  $stock=lookup($_POST["stockSymbol"]);
      if($stock==false)
   apologize("Invalid Stock Symbol :( !!");
  else
  { $newarray=[];
  $newarray=[
      "name"=>$stock["name"],
      "symbol"=>$stock["symbol"],
      "price"=>$stock["price"],

      ];


      render("../views/quote.php",[ "newarray"=>$newarray,title=>"Quote"]);
    }
 }

?>

views/quote.php

       <?php
        print"A share of".$newarray["name"]."(".$newarray["symbol"].")     costs : $".number_format($newarray["price"],2);

    ?> 

[Undefined variable message]1

1
  • I don't think you're supposed to just declare an array without assigning a value like you would in C. Maybe take that line out. $newarray=[];
    – Evan
    Jul 18, 2016 at 22:37

1 Answer 1

1
  1. Declaring an array variable in php may not be necessary but it won't hurt.
  2. If you look at the error message you're getting, you'll see the error is occurring on line 8 of quote.php. That's where you render the view quote.php (btw please rename your files, having the same name for different files, even if in different folders, is quite confusing). Your view/quote.php has a $newarray variable but you only define it further down the code.
  3. Related to 2, what is the purpose of

    if ($_SERVER["REQUEST_METHOD"] == "GET") { // else render form render("../views/quote.php", ["title" => "Quote"]); }

Are you disallowing the use of the GET method and only allowing POST? If so, it's not needed, you should only be specifying POST method. Just get rid of it. What you probably want is something like:

if ($_SERVER["REQUEST_METHOD"] == "POST")
    //do something

else
   render("../views/quote.php",[ "newarray"=>$newarray,title=>"Quote"]); 

This renders your views/quote.php until user posts $stocksymbol.

You must log in to answer this question.

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