PHP Full Stack development (vi): PHP interacts with HTML pages

Source: Internet
Author: User
Tags html form php and mysql

Previously, we created a form under an HTML page in the HTML form learning article.

This form is used by users to enter data

The specific code is as follows

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"></Head><Body><H1>User Login</H1><formname= "Input"Action= "action.php"Method= "POST">Account Number:<inputtype= "text"name= "User"><BR><BR>Password:<inputtype= "Password"name= "pwd"><BR><BR>Gender:<inputtype= "Radio"name= "Sex"value= "Man">male<inputtype= "Radio"name= "Sex"value= "Woman">female<BR><BR>Hobbies:<inputtype= "checkbox"name= "hobby[]"value= "Basketball">Basketball<inputtype= "checkbox"name= "hobby[]"value= "Football">Football<inputtype= "checkbox"name= "hobby[]"value= "Badminton">Badminton<BR><BR>       <inputtype= "Submit"value= "Submit">       </form></Body></HTML>

We see that there are two properties in the form label, one of which is action and the other is method

When we click the Submit button, the content is transferred to the PHP file specified by the action.

And in PHP there is a super global variable $_post

This super global variable, $_post, is used by PHP to receive the form data, but it does not implement the receive effect until you specify post in the form properties method.

Then we give the contents of the action.php file:

<?PHP$user=$_post[' User ']; $pwd=$_post[' pwd '];$sex=$_post[' Sex '];$hobby=isset($_post[' hobby '])?$_post[' Hobby ']: "";Echo"Your account number is:",$user, "<br>"; Echo"Your password is:",$pwd, "<br>"; Echo"Your sex is:",$sex, "<br>"; Echo"Your hobby is:";if(Is_array($hobby)){    foreach($hobby  as $x){        Echo $x. " "; }}
?>

By clicking the Submit button, the data is then transferred to the action.php page, and our browser jumps to this page.

We can see that the variable that we use to receive the data is $_post, which puts the name value of the form element, which will be transferred to these variables of PHP.

It is worth noting that we are in the HTML page, the name of this check box is hobby[], because the check box to transfer multiple arrays, so its name should be an array of form.

Otherwise, only one value is passed to the variable, and we need a set of values for the check box.

Of course, although the check box in the HTML page name is hobby[]

But we are still $_post[' hobby ' when we receive it, and this stuff is actually an array.

This time we need to do a protective measure, in order to prevent sometimes our check box is nothing to choose the null value, rather than an array, this time we do a test

$hobby=isset($_post[' hobby '])? $_post [' Hobby ']: "";

The isset () function is used to detect if a variable is set and is not NULL.

If it is empty, then $hobby also give null value, if not empty, then $hobby this variable is the check box of the entire array of contents.

Then we use a foreach loop to output this array and show it.

if (is_array($hobby)) {    foreach($hobbyas$x) {        echo$x . " ";    }}

The is_array () function is used to detect if a variable is an array, and if it is an array, it is displayed, if it is not an array, do not do any action.

Line breaks in PHP can be replaced with php_eol to improve code-level portability

Well, here we have the first HTML and PHP interactive case has been done.

This is also an interactive case of our first front end and back end.

HTML is the front-end, as the customer's browsing interface, PHP is executed on the server side of the program, is the backend, this through the case to deepen our understanding of the front-end and back-end, browser-side and server-side.

And then after this interaction, the next thing we do is the interaction between PHP and MySQL, that is, to save the PHP value to the server-side database based on this case.

You know, if we do not save, then each time we get the data will disappear, and only save it, the data will not disappear, and every time we open a page, we will show all of our previous content

OK, we'll start with the next essay on PHP and MySQL interaction.

PHP Full Stack development (vi): PHP interacts with HTML pages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.