Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.