dani0157 Posted March 29, 2010 Share Posted March 29, 2010 Hello everyone, I have a simple problem. I am learning PHP and i am just playing around and testing some things. One of the things that i am trying to make is the following: The Code <form action="test.php" method="get"> Name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> <?php switch($_GET[fname] ) { case 'Dani'; echo "Welcome Home Dani"; break; default: echo "Please input a name"; break; } ?> How do i make the script wait for a input, before it displaying anything ? P.S : I also tried with if and elseif , but the script still display text , before an input is made. Quote Link to comment https://forums.phpfreaks.com/topic/196887-wait-for-input/ Share on other sites More sharing options...
Psycho Posted March 29, 2010 Share Posted March 29, 2010 Your use of default makes it so the error message is also used for the first time load of the page. Take a look at the code below. I'd suggest having a separate page to handle the success scenario. <?php $error = ''; $msg = ''; if(isset($_GET[fname])) { //User has submitted the form $name = trim($_GET[fname]); if(empty($name)) { //name was empty $error = "Please input a name"; } else { //Name was entered $msg = "Welcome Home {$name}"; include('welcome_page.php'); exit(); } } ?> <html> <head></head> <body> <?php echo $error; ?><br /> <form action="test.php" method="get"> Name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/196887-wait-for-input/#findComment-1033689 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.