Receiving User Input PHP

Manipulating variables that you set within your script is somewhat interesting, but hardly anything to rave about. Scripts become much more useful when they use input from the user. When you call PHP from an HTML form, the form fields are turned into variables.

HTML Form for Lunch Information

HTML Form for Lunch Information

If you have dealt at all with CGIs, it will look familiar. There are three form fields that match up with the variables from our previous example. Instead of simply putting data into the variables, we will provide a form and use the information the user types. When the user presses the submit button, the script named in the ACTION attribute will receive the three form fields and PHP will convert them into variables.

Computing the Cost of Lunch from a Form print

Computing the Cost of Lunch from a Form print

Notice that in the first segment of the PHP script, I have eliminated the lines setting the variables, except for today's date. The rest of the script is unchanged. The script assumes there will be data in the variables. Try experimenting with the scripts by entering nonsense in the form fields.

One thing you should notice is that if you put words where the script expects numbers, PHP seems to just assign them values of zero. The variables are set with a text string, and when the script tries to treat it as a number, PHP does its best to convert the information.Entering 10 Little Indians for the cost of lunch will be interpreted as 10.

Conditional Daily Message

Conditional Daily MessageConditional Daily Message


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics