I have a text box in which the user can start typing the name of a company and it will start listing the companies to choose from. The problem I am having is that if I click ENTER with empty box it gives the message "Please enter a stock" but then when the user starts typing the previous message stays there and the suggestions are also shown. How do I remove the previous message? This is the page
if($_SERVER["REQUEST_METHOD"] == "POST"){
$stock = lookup(strtoupper($_POST["symbol"]));
if(empty($_POST["symbol"])){
echo"You must enter a stock symbol";
exit;
}else if($_POST["symbol"]){
$price = number_format($stock['price'], 2);
echo "A share of {$stock['name']} costs $price";
}
}
else{
// render portfolio
render("stock_search.php", ["title" => "Get Quote"]);
}
You should hide or clear the suggestions with JavaScript on (re-)start of typing.
you suould redirect to page so that the page refreshes and message removes
if($_SERVER["REQUEST_METHOD"] == "POST"){
$stock = lookup(strtoupper($_POST["symbol"]));
if(empty($_POST["symbol"])){
echo"You must enter a stock symbol";
//use redirect method to move page or refresh the same page not use exit;
}else if($_POST["symbol"]){
$price = number_format($stock['price'], 2);
echo "A share of {$stock['name']} costs $price";
}
}
else{
// render portfolio
render("stock_search.php", ["title" => "Get Quote"]);
}