【问题标题】:AJAX Post form Variables to PHP PageAJAX 将表单变量发布到 PHP 页面
【发布时间】:2014-01-09 11:33:19
【问题描述】:

我有一个执行 AJAX 函数以将表单值提交到 PHP 页面的表单。 PHP 页面只是通过回显 DIV 中的变量来响应。

它适用于 GET 方法,但我无法让它适用于 POST。

    <html>
    <body>
    <script language="javascript" type="text/javascript">
    <!-- 
    //Browser Support Code
    function ajaxFunction(){
        var ajaxRequest;

        try{
            // Opera 8.0+, Firefox, Safari
            ajaxRequest = new XMLHttpRequest();
        } catch (e){
            // Internet Explorer Browsers
            try{
                ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try{
                    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e){
                    // Something went wrong
                    alert("Browser Not Supported");
                    return false;
                }
            }
        }
        // Get Response
        ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById("response").innerHTML = ajaxRequest.responseText;
            }
        }
        var name=document.getElementById("name").value
        var email=document.getElementById("email").value
        ajaxRequest.open("POST", "process.php?name="+name+"&email="+email, true);
        ajaxRequest.send(null); 
    }
    //-->
    </script>
    <form name='Form1'>
      Name:  <input type='text'  name='name' id="name"/> <br />
      Email: <input type='text'  name='email' id="email"/> <br />
    <input type="button" value="Submit" onClick="ajaxFunction();">
    </form>
    <div id="response">
    </div>
    </body>
    </html>

还有php页面,process.php

    <?php
    echo date("H:i:s"); 
    echo "<br/>";
    // echo "Post Variables: ".$_POST['name']." ".$POST['email']; old code
            echo "Post Variables: ".$_POST['name']." ".$_POST['email'];
    echo "<br/>";
    echo "Get Variables: ".$_GET['name']." ".$_GET['email'];
    ?>

我得到的回应是:

11:32:05 发布变量: 获取变量:输入的姓名 输入的电子邮件

所以我很确定这与从 PHP 传递到 Javascript 的变量有关。

非常感谢。

【问题讨论】:

  • @MazIqbal 如果您使用的是 AJAX,则表单方法无关紧要。
  • 默认情况下,将信息传递给 php 脚本的方法是 Get。您必须在 ajax 中指定通过 POST 传递信息。
  • 是的,添加方法“post”没有区别。 get 仅在我将值附加到 URL 时才起作用,因此 PHP 页面可以请求 GET 或 POST ???
  • 一方面,这个$POST['email']; 应该读作$_POST['email']; 你忘记了下划线。 @保罗
  • 一件小事:在第 4 行的 process.php 中,您写了 $POST['email'] - 缺少下划线。但这不应该是重点......你可以尝试做一个print_r($_REQUEST);吗?

标签: javascript php ajax


【解决方案1】:
ajaxRequest.open("POST", "process.php?name="+name+"&email="+email, true);
ajaxRequest.send(null);

这不是发布变量。在这里,您将它们作为 GET 参数发送,POST 请求的正文为空。相反,使用

ajaxRequest.open("POST", "process.php", true);
ajaxRequest.send("name="+name+"&email="+email);

甚至更好

ajaxRequest.open("POST", "process.php", true);
ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxRequest.send("name="+encodeURIComponent(name)+"&email="+encodeURIComponent(email));

【讨论】:

  • 修改:ajaxRequest.send("name="+name+"&email="+email);没有区别,甚至尝试过: ajaxRequest.send(name) 只是为了确定,但没有传递任何值。
  • 谢谢,刚刚看到更新。这有点工作:) ajaxRequest.send("name="+encodeURIComponent(name)+"&email="+encodeURIComponent(email));我只得到名称而不是电子邮件,我猜这与变量的串联有关,你会在 POST 中使用 &email 吗????我的错误只是发现了错误:$POST['email'] 应该是 $_POST['email']。都在工作。 :)
  • @Paul: 是的,如果你使用application/x-www-form-urlencoded :-) 似乎是PHP 的错字,请使用$_POST 而不是$POST 作为电子邮件
猜你喜欢
  • 2016-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
  • 2011-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多