Jump to content

wait for input


dani0157

Recommended Posts

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. 

Link to comment
https://forums.phpfreaks.com/topic/196887-wait-for-input/
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/196887-wait-for-input/#findComment-1033689
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.