Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 验证表单并将数据发送到php文件_Javascript_Php_Jquery_Html - Fatal编程技术网

Javascript 验证表单并将数据发送到php文件

Javascript 验证表单并将数据发送到php文件,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我用Javascript开发了一个验证表单,所有人都认为是正确的 但是我想当所有人都被接受时,将信息发送到php文件 我怎么能做到 HTML代码: 接触 + = 发送 编辑:如果成功,则需要在php中回显数字1,如果失败,则需要回显数字2 PHP $message=$\u GET['comment']; $email=$_GET['commentMail']; $name=$_GET['commentName']; $to=“$email”; $subject='newmes

我用Javascript开发了一个验证表单,所有人都认为是正确的

但是我想当所有人都被接受时,将信息发送到php文件

我怎么能做到

HTML代码:


接触




+ =
发送
编辑:如果成功,则需要在php中回显数字1,如果失败,则需要回显数字2

PHP

$message=$\u GET['comment'];
$email=$_GET['commentMail'];
$name=$_GET['commentName'];
$to=“$email”;
$subject='newmessage';
$message=“lenom:”.$name.“

”$message.

电子邮件:“.$Email; $header=“$email”; if(邮件($to、$subject、$message、$header)){ 回音“消息发送”; } 否则{ 呼应“严重错误”; }
编辑:如果成功,则需要在php中回显数字1,如果失败,则需要回显数字2

PHP

$message=$\u GET['comment'];
$email=$_GET['commentMail'];
$name=$_GET['commentName'];
$to=“$email”;
$subject='newmessage';
$message=“lenom:”.$name.“

”$message.

电子邮件:“.$Email; $header=“$email”; if(邮件($to、$subject、$message、$header)){ 回音“消息发送”; } 否则{ 呼应“严重错误”; }
因此,AJAX旨在通过允许网页在用户工作时透明地对服务器进行异步调用来创建更通用、更具交互性的web应用程序。AJAX是一种工具,web开发人员可以使用它来创建更智能的web应用程序,在与人交互时,它的性能优于传统web应用程序

AJAX所采用的技术已经在所有现代web浏览器中实现,如Mozilla Firefox、Internet Explorer或Opera,因此客户端不需要安装任何额外的模块来运行AJAX网站。AJAX由以下部分组成:

  • JavaScript是AJAX的重要组成部分,它允许您 构建客户端功能。在JavaScript函数中 您将大量使用文档对象模型(DOM)来 操纵HTML页面的某些部分
  • XMLHttpRequest对象使JavaScript能够访问服务器 异步,以便用户可以在 功能在后台执行。访问服务器 简单地说就是对文件或脚本进行简单的HTTP请求 位于服务器上。HTTP请求很容易发出,并且不会导致 任何与防火墙相关的问题
  • 需要服务器端技术来处理出现的请求 来自JavaScript客户端。在本书中,我们将使用PHP来执行 作业的服务器端部分
对于客户机-服务器通信,部件需要一种传递数据和理解数据的方法。传递数据是最简单的部分。访问服务器的客户端脚本(使用XMLHttpRequest对象)可以使用GET或POST发送名称-值对。使用任何服务器脚本读取这些值都非常简单。 服务器脚本只是通过HTTP发回响应,但与通常的网站不同,响应的格式可以简单地由客户端上的JavaScript代码解析

建议的格式是XML,它具有被广泛支持的优点,并且有许多库使操作XML文档变得容易。但如果需要,您可以选择另一种格式(甚至可以发送纯文本),JavaScript对象表示法(JSON)是XML的一种流行替代方法

老派风格的简单示例:

HTML文件

<html>
  <head>
    <title>AJAX with PHP: Quickstart</title>
  </head>
  <body onload='process()'>
    Server wants to know your name:
    <input type="text" id="myName" />
    <div id="divMessage"></div>
  </body>
</html>

AJAX与PHP:Quickstart
服务器想知道您的姓名:
魔术师

// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();
// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() {
    // will store the reference to the XMLHttpRequest object
    var xmlHttp;
    // if running Internet Explorer
    if (window.ActiveXObject) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            xmlHttp = false;
        }
    }
// if running Mozilla or other browsers
    else {
        try {
            xmlHttp = new XMLHttpRequest();
        }
        catch (e) {
            xmlHttp = false;
        }
    }
// return the created object or display an error message
    if (!xmlHttp)
        alert("Error creating the XMLHttpRequest object.");
    else
        return xmlHttp;
}
// make asynchronous HTTP request using the XMLHttpRequest object
function process() {
// proceed only if the xmlHttp object isn't busy
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
// retrieve the name typed by the user on the form
        name = encodeURIComponent(document.getElementById("myName").value);
// execute the quickstart.php page from the server
        xmlHttp.open("GET", "**yourPHPfiletoretrievedata**.php?name=" + name, true);
// define the method to handle server responses
        xmlHttp.onreadystatechange = handleServerResponse;
// make the server request
        xmlHttp.send(null);
    }
    else
// if the connection is busy, try again after one second
        setTimeout('process()', 1000);
}
// executed automatically when a message is received from the server
function handleServerResponse() {
// move forward only if the transaction has completed
    if (xmlHttp.readyState == 4) {
// status of 200 indicates the transaction completed successfully
        if (xmlHttp.status == 200) {
// extract the XML retrieved from the server
            xmlResponse        = xmlHttp.responseXML;
// obtain the document element (the root element) of the XML structure
            xmlDocumentElement = xmlResponse.documentElement;
// get the text message, which is in the first child of
// the the document element
            helloMessage = xmlDocumentElement.firstChild.data;
// update the client display using the data received from the server
            document.getElementById("divMessage").innerHTML =
                '<i>' + helloMessage + '</i>';
// restart sequence
            setTimeout('process()', 1000);
        }
// a HTTP status different than 200 signals an error
        else {
            alert("There was a problem accessing the server: " + xmlHttp.statusText);
        }
    }
}
//存储对XMLHttpRequest对象的引用
var xmlHttp=createXmlHttpRequestObject();
//检索XMLHttpRequest对象
函数createXmlHttpRequestObject(){
//将存储对XMLHttpRequest对象的引用
var-xmlHttp;
//如果运行Internet Explorer
if(window.ActiveXObject){
试一试{
xmlHttp=新的ActiveXObject(“Microsoft.xmlHttp”);
}捕获(e){
xmlHttp=false;
}
}
//如果运行Mozilla或其他浏览器
否则{
试一试{
xmlHttp=新的XMLHttpRequest();
}
捕获(e){
xmlHttp=false;
}
}
//返回创建的对象或显示错误消息
如果(!xmlHttp)
警报(“创建XMLHttpRequest对象时出错。”);
其他的
返回xmlHttp;
}
//使用XMLHttpRequest对象发出异步HTTP请求
函数过程(){
//仅当xmlHttp对象不忙时才继续
if(xmlHttp.readyState==4 | | xmlHttp.readyState==0){
//检索用户在表单上键入的名称
name=encodeURIComponent(document.getElementById(“myName”).value);
//从服务器执行quickstart.php页面
xmlHttp.open(“GET”、“**yourpfiletoretrievedata**.php?name=“+name,true”);
//定义处理服务器响应的方法
xmlHttp.onreadystatechange=handleServerResponse;
//发出服务器请求
xmlHttp.send(空);
}
其他的
//如果连接忙,请在一秒钟后重试
setTimeout('process()',1000);
}
//从服务器收到消息时自动执行
函数handleServerResponse(){
//仅当事务已完成时才向前移动
if(xmlHttp.readyState==4){
//状态为200表示事务已成功完成
if(xmlHttp.status==200){
//提取从服务器检索到的XML
xmlResponse=xmlHttp.responseXML;
//获取XML结构的文档元素(根元素)
xmlDocumentElement=xmlResponse.documentElement;
//获取文本消息,该消息位于
//文档元素的
helloMessage=xmlDocumentElement.firstChild.data;
//使用从服务器接收的数据更新客户端显示
<html>
  <head>
    <title>AJAX with PHP: Quickstart</title>
  </head>
  <body onload='process()'>
    Server wants to know your name:
    <input type="text" id="myName" />
    <div id="divMessage"></div>
  </body>
</html>
// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();
// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() {
    // will store the reference to the XMLHttpRequest object
    var xmlHttp;
    // if running Internet Explorer
    if (window.ActiveXObject) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            xmlHttp = false;
        }
    }
// if running Mozilla or other browsers
    else {
        try {
            xmlHttp = new XMLHttpRequest();
        }
        catch (e) {
            xmlHttp = false;
        }
    }
// return the created object or display an error message
    if (!xmlHttp)
        alert("Error creating the XMLHttpRequest object.");
    else
        return xmlHttp;
}
// make asynchronous HTTP request using the XMLHttpRequest object
function process() {
// proceed only if the xmlHttp object isn't busy
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
// retrieve the name typed by the user on the form
        name = encodeURIComponent(document.getElementById("myName").value);
// execute the quickstart.php page from the server
        xmlHttp.open("GET", "**yourPHPfiletoretrievedata**.php?name=" + name, true);
// define the method to handle server responses
        xmlHttp.onreadystatechange = handleServerResponse;
// make the server request
        xmlHttp.send(null);
    }
    else
// if the connection is busy, try again after one second
        setTimeout('process()', 1000);
}
// executed automatically when a message is received from the server
function handleServerResponse() {
// move forward only if the transaction has completed
    if (xmlHttp.readyState == 4) {
// status of 200 indicates the transaction completed successfully
        if (xmlHttp.status == 200) {
// extract the XML retrieved from the server
            xmlResponse        = xmlHttp.responseXML;
// obtain the document element (the root element) of the XML structure
            xmlDocumentElement = xmlResponse.documentElement;
// get the text message, which is in the first child of
// the the document element
            helloMessage = xmlDocumentElement.firstChild.data;
// update the client display using the data received from the server
            document.getElementById("divMessage").innerHTML =
                '<i>' + helloMessage + '</i>';
// restart sequence
            setTimeout('process()', 1000);
        }
// a HTTP status different than 200 signals an error
        else {
            alert("There was a problem accessing the server: " + xmlHttp.statusText);
        }
    }
}