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:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.      "">
  4. <html xmlns=""  xml:lang="en" lang="en">
  5. <head>
  6. <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
  7. <title>Form Response with If-Else Statement</title>
  8. </head>
  10. <body>
  11. <h2>Favorite City</h2>
  13. <?php
  14. //If form not submitted, display form.
  15. if (!isset($_POST['submit'])){
  16. ?>
  18. <form method="post" action="yourfile.php">
  19. <!--Make sure you have entered the name you gave the file as the action.-->
  20. Please enter your favorite city:  <br />
  21. <input type="text" name="city" />
  22. <p />
  23. <input type="submit" name="submit" value="Go" />
  24. </form>
  26. <?php
  27. //If form submitted, process input.
  28. }else{
  30. //Retrieve string from form submission.
  31. $city = $_POST['city'];
  32. echo "Your favorite city is $city.";
  34. }
  35. ?>
  37. </body>
  38. </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.


Another way, but giving the user an option to resend the form in the result page.

//my version

<form method="post" action="4.php">
	<label for="city">City: </label>
	<input type="text" name="city" placeholder="city" id="city" />
	<input type="submit" value="Send" />

	$city = $_POST['city'];
	if (isset($city)) {
		echo '<p>Your city is '.$city.'.</p>';
	} else {
		echo '<p>Type your city name above!<p>';

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.

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.

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.

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 != empty

A way to only submit the form if the city value has been filled out. The code is below:

  1. <h1>Simple Form with PHP Response</h1>
  3. <?php
  4. if (isset($_POST['submit']) == TRUE && (($_POST['f_city']) != "")) {
  6. 	$f_city = $_POST['f_city'];
  7. 	echo "<p>Your favorite city is " . $f_city . "." . "</p>";
  9. } else {
  11. 	?>
  12. 	<h2>Favorite City</h2>
  13. 	<p>What city is your favorite?</p>
  14. <form action="" method="post"/>
  15. 	<input type="text" name="f_city" />
  16. 	<input type="submit" name="submit"/>
  17. </form>
  19. <?php 
  21. } 	 
  24. ?>

it's unnecessary to write this

  1. isset($_POST['submit']) == TRUE


  1. isset($_POST['submit'])

does the same thing. isset() returns a boolean response already.

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:

  1. if(!isset($_POST['submit'])){


  1. if(!isset($_POST['submit']) || $_POST['city'] == ""){

to get the same result.

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()?

  1. <DOCTYPE html>
  2. <html>
  3. <body>
  5. <?php
  6. if (isset($_POST['city'])){
  7. $city = $_POST['city'];
  8. echo "Your favourite city is " . $city . ".";
  9. }
  10. ?>
  12. <form action="forms_ex_2.php" method="post">
  13. Enter your favourite city:<input type="text" name="city">
  14. <input type="submit" name="submit">
  15. </form>
  17. </body>
  18. </html>

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.


  1. <Html>
  2. <body>
  3. <form method='post' action="answer.php">
  4. What is your favorite city <br/>
  5. <input type="text" name="city" value="">
  6. <input type="submit" name="submit"/>
  7. </form>
  8. </body>
  9. </html>


  1. <html>
  2. <body>
  3. <?php
  4.     $city = $_POST["city"];
  6.     if (!isset($city)) {
  7.         echo '<p>Go back and enter city name</p>';
  8.     } else {
  9.         echo '<p>Your city is '.$city.'.<p>';
  10.     }
  11. ?>
  13. </body>
  14. </html>

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?

Here's my take on this (without looking at the solution script). It's short, clean, and easy to understand.

  1. <form action="" method="post">
  2.   Enter City Name:
  3.   <input name="cityname" type="text" maxlength="100" />
  4.   <input type="submit" value="Check it!">
  5. </form>
  7. <?php
  8.   if (isset($_POST["cityname"]) && !empty($_POST["cityname"]))
  9.     echo "Your favorite city is ".$_POST["cityname"].".";
  10.   else echo " ";	
  11. ?>