【问题标题】:Send date and retrieve information using PHP and AJAX in form使用 PHP 和 AJAX 在表单中发送日期和检索信息
【发布时间】:2014-02-16 15:31:27
【问题描述】:

我是 AJAX 和 PHP 编程的新手。 我相信这很简单,但我很难。

我需要的是用户选择一个日期(使用 jquery 日期选择器)并单击“确定”按钮。

表单将调用页面 query.php(通过 Ajax)。 query.php 将检查日期。 如果日期在数据库中有注册表,则 query.php 将返回为 index.php 填充的表。

我的脚本基于这个例子:http://www.w3schools.com/php/php_ajax_database.asp

但在该示例中,触发器是事件 (onchange="showUser(this.value)")。 我需要在提交表单后调用 AJAX 函数。

我使用 () 完成了该操作,但是当我尝试使用 $q = $_GET['q']; 获取日期时该值为空白。

Index.php(相关信息):

<!DOCTYPE html>

<html lang='pt-BR'>
<head>
    <script>
    function showUser(str)
    {
    if (str=="")
      {
      document.getElementById("txtHint").innerHTML="";
      return;
      } 
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","checar_data.php?q="+str,true);
    xmlhttp.send();
    }
    </script>
</head>
<body>

    <form id="dateForm" onsubmit="showUser(); return false;" method="GET">
        Data:
        <input name="datepicker" id="datepicker" type="text" size="10" maxlength="8" />
        <input name="OK" value="OK" type="submit" >
    </form>        
    <div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

查询.php:

<?php
$q = intval($_GET['q']);

echo "$q -> bla bla";


?> 

我该如何解决这个问题?

【问题讨论】:

    标签: php jquery ajax forms xmlhttprequest


    【解决方案1】:

    查询.php

    改成

    <?php
    $q = $_GET['q'];
    echo "$q";
    ?>
    

    发送数据页面不正确

    xmlhttp.open("GET","checar_data.php?q="+str,true);
    

    改成

    xmlhttp.open("GET","query.php?q="+str,true);
    

    你的函数只有一个参数并且留空 启动时表单声明,

    改变

    <form id="dateForm" onsubmit="showUser(); return false;" method="GET">
    

    <form id="dateForm" onsubmit="showUser(this.datepicker.value); return false;" method="GET">
    

    【讨论】:

    • 我更正了表单提交方法,效果很好。为什么旧方法行不通? tks
    • 我不知道旧方法是什么:)。我也是新手,但“showUser()”有一个参数发送到 query.php 页面,而在旧代码中,您不向该参数发送任何内容。
    • 啊,现在我明白了 showUser(this.datepicker.value),我需要具体说明哪个字段会将数据发送到 query.php。 Tks很多人:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    相关资源
    最近更新 更多