使用AJAX将表单输入发布到PHP以用于多个自动完成查询条件

时间:2013-02-14 23:40:23

标签: php jquery ajax autocomplete

我在输入表单'city'上使用jquery自动完成功能,但我希望我的'autocity.php'文件中的查询仅建议预选国家/地区的城市WHERE City LIKE '%$term%'" AND CountryID = '%$country%'。表单操作submit使用单独的PHP文件(create-business.php)将表单数据插入数据库,因此通常的$ _POST ['Countries_CountryId']在autocity.php中不起作用。这就是我现在使用AJAX将'country'发布到autocity.php的原因。还有一种方法可以从autocity.php文件中回显/ alert / print_r,这样我就可以确认ajax帖子中的$ _POST ['$ country']到达autocity.php文件。

我有两个输入框

<pre>`
<form id="input" action="php/create-business.php" method="post">
<select name="Countries_CountryId" id="country">
<input type="text" id="city" name="City">`
</pre>

以下是表单

中的脚本
<script>
$(function () {
var country = $("#country").val();
$.ajax({
type:"POST", url:"autocomplete/autocity.php", data:"country",         
beforeSend:function () {
// alert(country);
}, complete:function () {  // is there any need for this?
}, success:function (html) {  // is there any need for this too?
}
});

$("#city").autocomplete(
             {
             source:'autocomplete/autocity.php'
             })
    });
</script>

这是autocity.php

`
//database connection works fine and autocomplete 
//suggestion works without the AND CountryID = '%$country%' part.  

    $country = "";
    if (isset($_POST['country'])) {
    $country = trim($_POST['country']);}
    echo "window.alert($country);"; //This did nothing no alert

    $term = $_REQUEST['term'];
    $req = "SELECT City
            FROM cities
            WHERE City LIKE '%$term%' AND CountryID = '%$country%'";

    $query = mysql_query($req);
    while ($row = mysql_fetch_array($query)) {
        $results[] = array('label' => $row['City']);
    }
    echo json_encode($results);
    ?>`
    

所以问题基本上是:

1 - 如何在.php文件中使用AJAX使用表单中的文本输入来查询不是提交表单操作.php文件的MySQL数据库

2 - 当ajax用于显示php文件收到我的ajax帖子时,如何从PHP文件中提醒post变量。在我的简短经验中,当网页发生变化时,echo和print_r仅适用于表单提交,表明我的表单结果在表单操作上提交。

3-我的语法怎么样?

非常感谢您提前帮助这位新手:D


好的,这是我尝试过的最新动态。我想我很亲密。我正在使用Jquery UI - //ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js

这是脚本方法1:

   $(document).ready(function () {
    var country = $('#country').value();
    alert(country + " complete");
    $("#city").autocomplete(
          {
            source:'autocomplete/autocity.php?country='+country,
            minLength:1
           });
        });

这是脚本方法2:

 $(document).ready(function () {

    $('#city').autocomplete({
        // source: function() { return "GetState.php?country=" + $('#Country').val();},
        source:function (request, response) {
            $.ajax({
                url:"autocomplete/autocity.php",
                //dataType:"json",
                data:{
                    term:request.term,
                    country:$('#country').val()
                },
                success:function (data) {
                    response(data);
                }
            });
        },
        minLength:2
    });
  });

我更喜欢方法2,因为它允许我添加多个参数。

最后这是我最新的autocity.php代码

 <?php
    $term = $_REQUEST['term'];
    $country = $_REQUEST['country'];
    $req = "SELECT City
    FROM cities
    WHERE City LIKE '%$term%' AND CountryID = '%$country%' ";

    $query = mysql_query($req);

    while ($row = mysql_fetch_array($query)) {
    $results[] = array('label' => $row['City']);
    }

    echo json_encode($results);
     ?>

我仍然完全陷入困境。任何人都可以看到代码的问题?我在网上到处寻找正确的语法。再次感谢

1 个答案:

答案 0 :(得分:0)

对于第一个问题,您的方法基本上是正确的。您可以绑定到特定字段的blur事件,并使用您的函数获取该字段的值并以您正在执行的方式提交到php脚本。 $ .blur()正是您要找的。

对于第二个问题,error_log函数会将内容写入php的错误日志。如果使用print_r将变量转储到此日志,请确保将print_r的第二个参数设置为true,以将结果作为返回值输出。