【问题标题】:posting to same page with ajax and php not working [duplicate]使用ajax和php发布到同一页面不起作用[重复]
【发布时间】:2015-04-24 03:14:28
【问题描述】:

我有一个应该发布到同一页面的表单,但它没有做任何事情。我正在使用我在另一个页面中使用的相同 ajax 脚本和 php,唯一的区别是这个有一个表单,但除此之外一切正常。但是在这个表单页面中它不起作用。输出应显示在页面末尾;但是,我在控制台中没有收到任何错误。 html页面

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Contact Us</title>

    <!-- Bootstrap -->
    <link href="bootstrap.min.css" rel="stylesheet">
    <!-- stylesheet for this form -->
    <link href="contact-stylesheet.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script type="text/javascript">
        function validateForm()
        {
            var params = "name=jorge&email=jorge@gmail.com";

            //send data to php form--------------------->
            var xmlhttp;
            if (window.XMLHttpRequest)  {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

            }

            xmlhttp.open("POST", "contact.php", true);
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {

                    document.getElementById("outputtext").innerHTML=xmlhttp.responseText;
                }
            }
            xmlhttp.send(params);



        }       

    </script>
  </head>
  <body>

      <div class="row">
          <div class="hero-unit" style="padding:20px 100px">
            <h1>Contact Us</h1>
            <p>aldkfjasdkfjaskdfasdfkasdkfjadsfjsdkfjaskfjasdkfjasjfaskdfjsdkfjsksdsdkjsd</p>       
        </div>
          <div class="col-sm-6">
            <div class="my-form">
                <form class="form-horizontal" name="myForm"  >
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">Name:</label>
            <div class="col-sm-8">
              <input type="name" name="name" class="form-control" id="inputEmail3" placeholder="Name">
            </div>
          </div>
          <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">Email:</label>
            <div class="col-sm-8">
              <input type="email" name="email" class="form-control" id="inputPassword3" placeholder="Email">
            </div>
          </div>
          <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">Subject:</label>
            <div class="col-sm-8">
              <input type="text" name="subject" class="form-control" placeholder="Subject">
            </div>
          </div>

          <div class="form-group">
              <label for="inputPassword3" class="col-sm-2 control-label">Text:</label>
              <div class="col-sm-8">
                <textarea name="text" class="form-control" rows="7" placeholder="Text"></textarea>
              </div>    
          </div>
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
              <button type="submit" class="btn btn-default" onclick="validateForm()">Send</button>
            </div>
          </div>    
            </div> 
        </form>
          </div>
          <div class="col-sm-6">
              <div style="width:500px;heigth:350px;border:solid 1px brown">
                <h1>GOOGLE MAP HERE!</h1>
              </div>
             <!-- <img sytle="padding:0px 20px" src="https://maps.googleapis.com/maps/api/staticmap?center=Miami+Downtown,Miami,FL&zoom=13&size=500x350&maptype=roadmap&markers=color:red%7CMiami+Downtown,Miami,FL">                     -->
          </div>      
      </div>

        <div id="outputtext">
            <p>output text here!</p>
        </div>  
            <!-- display form result message here! -->

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>

  </body>
</html>

php 脚本

<?php

    echo "name: " . $_POST['name'] . " email: " . $_POST['email'];

?>

【问题讨论】:

    标签: php ajax forms


    【解决方案1】:

    您通过GET提交:

                <form class="form-horizontal" name="myForm"  >
    

    没有method="POST",因此浏览器默认为GET请求,使得$_POST完全为空。而且您的 ajax 代码也不会中断标准提交行为,因此它永远不会在页面关闭之前发送自己的 POST 请求。

    【讨论】:

    • 当 JS 发出请求时,这有关系吗?
    • 没有。 http是http。只要 js 复制了表单会做的事情,浏览器就无法判断它是 ajax 标头(有一个用于 ajax 的 X-标头,但由于它是 http,因此很容易伪造/省略)。
    • 我的表单中有一个 method="post" ,我删除了它 b/c 它没有任何区别。我仍然没有得到任何回报或没有它
    猜你喜欢
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 2017-06-25
    • 1970-01-01
    • 2016-04-29
    • 2018-07-04
    • 2022-01-01
    相关资源
    最近更新 更多