PHP Forms Ex. #2: Interactive Form with If-Else Statement
One very useful thing you can do with PHP is include the request for user input and the response in the same file, using conditional statements to tell PHP which one to show. For this PHP exercise, rewrite the two files of the previous exercise into one file using an if-else conditional statement.
Hint: You'll need some way to tell if the form has been submitted. The function to determine if a variable has been set and is not null is isset().
 
 
Here's the script:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
<title>Form Response with If-Else Statement</title>
</head>
<body>
<h2>Favorite City</h2>
<?php
//If form not submitted, display form.
if (!isset($_POST['submit'])){
?>
<form method="post" action="yourfile.php">
<!--Make sure you have entered the name you gave the file as the action.-->
Please enter your favorite city: <br />
<input type="text" name="city" />
<p />
<input type="submit" name="submit" value="Go" />
</form>
<?php
//If form submitted, process input.
}else{
//Retrieve string from form submission.
$city = $_POST['city'];
echo "Your favorite city is $city.";
}
?>
</body>
</html>
See the output of the script in a separate window here. You can also view the output's HTML source from the new window, if you need to check that.
Comments
Another version
Another way, but giving the user an option to resend the form in the result page.
Thanks
Readers: In case you're not familiar with the syntax highlighter's functions, just click 'expand source' in leocg's post to see his solution.
Thanks for the alternative, leocg. We get an undefined index error for city when the page is loaded, but otherwise your code does work. And you did use the conditional if-else statement. We encourage readers to learn the original answer script's approach, since it is used many times in future exercises.
why is isset($_POST['submit']) checked?
Under what circumstances would !isset($_POST['submit']) return a true value?
I am beginning to learn coding for web sites and this code seems unnecessary to me.
PHP Forms Ex. #2: Interactive Form with If-Else Statement
Forgive me, I'm new to programming.
This statement
"if (!isset($_POST['submit'])){"
confused me. My question in looking at the code, I saw no field called "submit".
The "input type" and the "name" of the form were both given values of "sumbit". So I'm unsure what "submit" are we referring to? I can't see any field called "submit".Where is this (field called "submit") referred to in the code for this program?
Thank you for your time.
PHP Forms Ex. #2: Interactive Form with If-Else Statement
The form only gets displayed the first time. If I don't enter a city name, then submit I get : "Your favorite city is . "
Is this normal?
add something like $city !=
add something like $city != empty
Here's a way to keep the form from submitting if it's empty
A way to only submit the form if the city value has been filled out. The code is below:
<?php
?>
<?php
}
?>
== true unnecessary
it's unnecessary to write this
when
does the same thing. isset() returns a boolean response already.
This is kind of late, but
This is kind of late, but there is actually a way you can modify the code in the example even less to get the same result. By simply changing:
to
to get the same result.
Exercise #2
Awesome website, so glad you kept it going!
My solution is below. Couple of questions though: How do I stop it from re-displaying the form once I've submitted it? And is there a preference for checking "submit" or "(input field name)" with isset()?
<?php
}
?>
difference between isset and empty?
I tried the script with isset and wouldnt get an error message if the form wasnt filled out.
I then used the empty function and all worked...I cant figure out why it didnt work with isset.
question.php
answer.php
<?php
}
?>
$_POST["submit"] vs $_POST["city"]
Btw i tested both and they work either way for some reason. Any explanation for this? I also tested this with the empty function and it works. I just don't know why in the previous exercise we make two files and in this one we can complete everything in the same file like in the next exercise. Is it just two ways to do the same thing?
You might want to check this.
Here's my take on this (without looking at the solution script). It's short, clean, and easy to understand.
<?php
?>