About Wamp's HTML, PHP, MySQL operations and connections-value between HTML and PHP (ii)

Source: Internet
Author: User

In the previous chapter, the simple application of PHP to the value of HTML is explained in this chapter, which explains the two types of HTML to PHP, that is, the two cases mentioned in the previous chapter.

  1. Submission of the form

   Open e:/work/phptest/, you can create a new two files form.html and form.php

Form.html:

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0">    <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge">    <title>Document</title></Head><Body>    <!--action points to the address where the form data is received, method is the way it is sent -    <formAction= "./form.php"Method= "POST">UserName:<inputtype= "text"name= "Name"><BR/>PassWord:<inputtype= "Password"name= "Password"> <BR/>        <!--Multiple selection dropdown box multiple options, name corresponds to an array multiple is to indicate that you can select multiple -        <!--City : <select name= "city[", "id=" City "multiple=" multiple "> -City :<Selectname= "City"ID= "City">            <optionvalue= "0">Please select</option>            <optionvalue= "Shenzhen">Shenzhen</option>            <optionvalue= "Beijing">Beijing</option>            <optionvalue= "Shanghai">Shanghai</option>            <optionvalue= "Guangzhou">Guangzhou</option>        </Select>
<br/>
        <inputtype= "File"name= "File"ID= "File"><BR/>        <BR/>Like :<inputtype= "Radio"name= "like"value= "Apple">Apple<inputtype= "Radio"name= "like"value= "Banana">Banane<inputtype= "Radio"name= "like"value= "Orange">Orange<inputtype= "Radio"name= "like"value= "Tomatoes">Tomatoes<BR/>        <!--multiple options, name corresponds to an array -owner:<inputtype= "checkbox"name= "owner[]"Vaule= "BMW">BMW<inputtype= "checkbox"name= "owner[]"value= "House">Villa<inputtype= "checkbox"name= "owner[]"value= "Money">Dollar<inputtype= "checkbox"name= "owner[]"value= "Gender">female<BR/>        <inputtype= "Submit"value= "Submit">    </form></Body></HTML>

  Note: When multiple options are selected, name = arr[] (array).

form.php:

  

<?PHPHeader("content-type:text/html; Charset=utf-8 ");//prevent Chinese garbled//define variables to receive the number passed over, form using POST delivery, receive using $_post[]    $userName=$_post["Name"]; $password=$_post["Password"]; $city=$_post["City"]; $like=$_post["Like"]; $owner=$_post[' Owner ']; $file=$_post[' File ']; //put the data in the array    $arr=Array("UserName" =$userName, "Password" =$password, "City" =$city, "like" = =$like, "Owner" =$owner, "File" =$file); //Print    EchoJson_encode ($arr);?>

Open the browser input: myserver/phptest/form.html , fill out the form, and then submit, the page will be transferred to form.php, this is the PHP you see has been printed in the transmission of data.

  

  This indicates that the HTML was successfully delivered to PHP.

  2, the HTML in the specific data transfer to PHP, using Ajax

  

<!  DOCTYPE html>            $("#btn"). Click (function(){                //This data is simulated data, can be combined into objects according to the actual situation                varobj = {"id": 1, "item": {"name": "Ject", "Age": "25"}}; //send the collected data obj$.post ("./test.php", obj,function(data) {//Receive return value                    varres = JSON.parse (data); //You can print it out and take a look.Console.Log(RES); })            })        </script> </body>

form.php

<? PHP     // 1, sequential single variable receive    //$id = $_post["id"];    $name = $_post["name"];    echo Json_encode ($id);    echo Json_encode ($name);    2, directly stored in the array    $arrarrays$_post$_post["Item"]);     // The accepted value can be passed to the front end, this can be based on the actual situation, in PHP data processing, the front-end needs to pass through    echo json_encode ($arr);? >

  Open Browser input: myserver/phptest/form.html, refresh, click Submit, open the console, you can see the data returned:

  

  This loop shows that the data is sent to form.php and then back to form.html, and the HTML is passed to PHP in two directions.

Add: The data is processed in form.php as follows:

To change the form.php:

<?PHP//directly stored in the array    $arr=Array("id" =$_post["id"], "item" + =$_post["Item"]); //The connection symbol in PHP is the "." Dot number    $str= "I am".$arr["id"]. ", my name is".$arr[' Item '] [' name ']. ",".$arr[' Item '] [' Age ']. Years old. "; //The accepted value can be passed to the front end, this can be based on the actual situation, in PHP data processing, the front-end needs to pass through    EchoJson_encode ($str);?>

  Refresh the page, click Submit, open the console, you can see the data returned:

  

  This is sufficient to indicate that the data has been form.php processed and returned to form.html.

  The next chapter describes MySQL content in Wamp.

    

About Wamp's HTML, PHP, MySQL operations and connections-value between HTML and PHP (ii)

Related Article

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.