预输入输入字段和查询传递给PHP和AJAX [英] Typeahead input field and query passed to PHP with AJAX

查看:149
本文介绍了预输入输入字段和查询传递给PHP和AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Twitter引导为事先键入的内容自动完成域中。

I am using Twitter Bootstrap Typeahead for an autocomplete field.

末状态的​​目标:用户首次进入细节到现场1.当他们进入现场2的详细信息,阿贾克斯通过在此基础上查询的数据库,因为它是写一个PHP文件查询什么也进入现场1。

End state goal: The user first enters details into field 1. When they enter details in field 2, ajax passes the query as it is written to a PHP file which queries a database based on what was also entered into field 1.

我如何通过这两个从外地2查询和现场1到PHP文件的内容,并对其进行访问。

How do I pass both the query from field 2 and the contents of field 1 to the PHP file and access them.

下面是我到目前为止有:

Here is what I have so far:

HTML文件

<div class="field1">
    <input type="text" id="field1" data-provide="typeahead" name="field1">
</div>
<div class="field2">
    <input type="text" id="field2" data-provide="typeahead">
</div>

<script src="js/jquery-1.9.1.min.js"></script>
    <script src="js/bootstrap.js"></script>
    <script>
$(function() {
            $("#field2").typeahead({
                source: function(query, process) {
        var textVal=$("#field1").val();
                    $.ajax({
                        url: 'field2.php',
                        type: 'POST',
                        data: 'query=' + query,
                        dataType: 'JSON',
                        async: true,
                        success: function(data) {
                            process(data);
            console.log(textVal);
                        }
                    });
                }
            });
        });
    </script>

PHP文件:

if (isset($_POST['query'])) {
$db_server = mysql_connect("localhost", "root", "root");
mysql_select_db("db_test");

$query = $_POST['query'];
$other = '**This needs to be field 1**';

$sql = mysql_query("SELECT * FROM table WHERE row1 LIKE '%{$query}%' AND row2 = '$other'");
$array = array();

while ($row = mysql_fetch_assoc($sql)) {
    $array[] = $row['row1'];
}

echo json_encode($array);}

目前,查询部分完美的作品,并将结果返回(控制台也从字段1显示的数值。只需要得到该值将在同一时间PHP文件!

At the moment, the query part works perfectly and the results are returned (the console also displays the value from 'Field1'. Just need to get that value into the php file at the same time!

任何帮助将是巨大的。

推荐答案

如果我正确地理解这一点,要分析现场1和2的两个值相同的AJAX调用。这是你如何做到这一点。

If I understood this correctly, you want to parse both the values of field 1 and 2 to the same AJAX call. This is how you do it.

<script>
$(function() {
        $("#field2").typeahead({
            source: function(query, process) {
    var textVal=$("#field1").val();
                $.ajax({
                    url: 'field2.php',
                    type: 'POST',
                    data: 'query=' + query + '&field1=' + textVal,
                    dataType: 'JSON',
                    async: true,
                    success: function(data) {
                        process(data);
        console.log(textVal);
                    }
                });
            }
        });
    });
</script>

现在你只要把另外$ _ POST ['字段1'在你的PHP文件。

Now you just make another $_POST['field1'] in your PHP file.

这篇关于预输入输入字段和查询传递给PHP和AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆