简体   繁体   English

数据如何从 java 脚本传递到 php?

[英]How is the data being passed from java script to php?

Okay, so I've been trying to learn web programming for a project that I need to submit soon.好的,所以我一直在努力学习 web 编程,因为我需要尽快提交一个项目。 Anyways, I've stumbled on a tutorial that I cannot, for the love of me, understand how does the data from his javascript is passed to the php script that he wrote.无论如何,我偶然发现了一个教程,出于对我的爱,我无法理解他的 javascript 中的数据如何传递到他编写的 php 脚本。

Here's the snippet.这是片段。

Javascript: Javascript:

        $("form").submit(function(event) {
            event.preventDefault();
            var name = $("#mail-name").val(); 
            var email = $("#mail-email").val(); 
            var gender = $("#mail-gender").val(); 
            var massage = $("#mail-massage").val(); 
            var submit = $("#mail-submit").val();  
            $(".form-message").load("mail.php",{
                name: name,
                email: email,
                gender: gender,
                massage: massage,
                submit: submit
            });
        })

    })

PHP: PHP:

    $name = $_POST['name'];
    $email = $_POST['email'];
    $gender = $_POST['gender'];
    $massage = $_POST['massage'];

    $errorEmpty = false;
    $errorEmail = false;

    if(empty($name) || empty($email) || empty($massage)) {
        echo "<span class='from-error'> Fill in all fields!</span>";
        $errorEmpty = true; 
    }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        echo "<span class='from-error'> Write a valid e-mail address!</span>";
        $errorEmail = true; 
    }else{
        echo "<span class='form-success'> E-mall has been sent! </span>";
    }

how is the php here able to receive the data from the "form"?这里的 php 如何能够从“表单”接收数据? Also, on the $("form") should this be either an ID or a CLASS set for the form that will contain the information (that wasn't mentioned in the tutorial).此外,在 $("form") 上,这应该是一个 ID 或 CLASS 集,用于将包含信息的表单(本教程中未提及)。

Lastly, is the.load("mail.php") the one responsible for sending the data to php?最后,负责将数据发送到 php 的是 the.load("mail.php") 吗?

If so, why did he add $(".form-message") which is ap tag.如果是这样,他为什么要添加 $(".form-message") 这是 ap 标签。

First of all, the $("form") reffer to the <form> html tag in your page.首先, $("form")引用页面中的<form> html 标记。

So basicaly, $("form").submit reffer to an event form click(submit of the form).所以基本上, $("form").submit reffer 事件表单点击(提交表单)。

The .form-message is a class of a div or p... which will contain the response of the mail.php .In the example here it could be on of those lines: .form-message是 div 或 p... 的 class,它将包含mail.php的响应。在此处的示例中,它可能位于以下行中:

<span class='from-error'> Fill in all fields!</span>
<span class='from-error'> Write a valid e-mail address!</span>
<span class='form-success'> E-mall has been sent! </span>

The load jquery function here is going to make an HTTP Request sending parameters to the mail.php file.这里的load jquery function 会做一个HTTP请求发送参数到mail.php文件。

{
   name: name,
   email: email,
   gender: gender,
   massage: massage,
   submit: submit
} 

The php file in going to map the given parameter as: php 文件中的 map 给定参数为:

 $name = $_POST['name'];
 $email = $_POST['email'];
 $gender = $_POST['gender'];
 $massage = $_POST['massage'];

Remember that parameters name should be kept the same.请记住,参数名称应保持不变。

Check out here some useful links about:在这里查看一些有用的链接:

JQuery load function JQuery 负载 function

PHP: variables from external sources PHP:来自外部源的变量

When you are submitting data from a form, with either GET or POST, it goes to the action parameter in that form which can be a php script.The.load() function gets the mail.php file with the parameters which you set in your javascript, and returns some html code which gets appended in your p tag ( the.form-message ).当您使用 GET 或 POST 从表单提交数据时,它会转到该表单中的操作参数,该参数可以是 php 脚本。The.load() function 获取 mail.php 文件,其中包含您在您的 javascript,并返回一些 html 代码,该代码附加在您的 p 标签 ( the.form-message ) 中。

So when you load it, and set those parameters, that's when the js is sending the data to the PHP.因此,当您加载它并设置这些参数时,就是 js 将数据发送到 PHP 的时候。

$("form") means any form on the page. $("form") 表示页面上的任何表单。 $("#form") is selecting the element with the ID form, and $(".form") is selecting the element with the class form. $("#form") 正在选择具有 ID 表单的元素,而 $(".form") 正在选择具有 class 表单的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM