Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

PHP Forms Ex. #1: Simple Form with PHP Response

In the next PHP exercise, you will request input from the user, then move the user's response from one file to another and do something with it.

Create two separate files. The first will contain a form with one input field asking for the user's favorite city. Use the post method for the form. Although this file contains no PHP code, on my localhost, it needs the .php extension to successfully call the second file.

The second file will contain PHP code to process the user's response. (In this case, something very simple.) After the user clicks the submit button, echo back Your favorite city is $city., where $city is the input from the form.

Hint: the variable that contains the user's input is an array. Arrays will be addressed in future exercises, but this particular array needs to come into play here. The array variable is $_POST['name'], where 'name' is the name of your input field.

 

Comments

WORKS PERFECTLY. This site really is top notch!!!

LOL - I couldn't make it work last night - 4:00am ran out of brain fuel [Coffee]. Works perfect now of course.

So far it's been a very good to share opinions with other people from different countries

im so thankful for this site developers .. hope you still Continuing update this site . .

I saved these two files on the same directory.

main.php

  1. <form action="response.php" method="post">
  2.   Enter City Name:
  3.   <input name="cityname" type="text" maxlength="100" />
  4.   <input type="submit" value="Check it!">
  5. </form>

response.php

  1. echo "Your favorite city is ".$_POST["cityname"].".";