简体   繁体   English

PHP JSON自动完成功能$ _POST到另一个页面

[英]php JSON autocomplete $_POST to another page

I have here a select_course.php with AJAX autocomplete. 我这里有一个带有AJAX自动完成功能的select_course.php。 what I am trying to do is pass the array variable pulled in get_course.php 我想做的是传递get_course.php中提取的数组变量

<script type="text/javascript">
    $(document).ready(function(){
        $("#course_no").autocomplete({
            source:'get_course.php',
            remoteDataType: 'json',
            type: 'POST',
            minLength:1
        })
    });

and the form with it 和它的形式

<form method="post" action="select_course_add.php" name="selectcourse">
<input size="35" type="text" id="course_no" name="course_no" autofocus /> 
<input type="submit" value=" Add " />
</form>  

get_course.php code get_course.php代码

$term=$_GET["term"];

$query=mysql_query("
SELECT    
    course_tbl.course_id
    , course_tbl.course_code
    , course_tbl.course_num
    , course_tbl.description
FROM
    cis_db.course_tbl
where course_num LIKE '".$term."%' OR course_code LIKE '%".$term."%'
ORDER BY course_tbl.course_num ASC, course_tbl.course_code ASC
");
$json=array();
    while($student=mysql_fetch_array($query)){
             $json[]=array(
            'value'=>$student["course_num"],
        'code'=>$student["course_code"], 
            'label'=>$student["course_description"]
                            );
        }
echo json_encode($json);
flush();

please help, my problem is, how can i convert and use this 3 variable to $_POST 'value'=>$student["course_num"] , 'code'=>$student["course_code"], and 'label'=>$student["course_description"] 请帮助,我的问题是,如何将这3个变量转换为$ _POST'value'=> $ student [“ course_num”]'code'=> $ student [“ course_code”]'label'= > $ student [“ course_description”]

You don't need this two lines: 您不需要这两行:

remoteDataType: 'json'
type: 'POST'

Your requisition must be GET, and by default the ajax call knows that the response type will be Json, so your response must be an array like this: 您的请求必须为GET,并且默认情况下,ajax调用知道响应类型为Json,因此您的响应必须为如下数组:

echo json_encode(array('Value 1', 'Value 2', 'Value 3'));

If you really need to send more values on the array you can use the select method of autocomplete, to treat as you want this values. 如果确实需要在数组上发送更多值,则可以使用自动完成功能的select方法,将其视为您需要的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM