我们如何使用jquery在url中传递id?

时间:2017-01-19 12:36:35

标签: javascript php jquery html5 css3

代码:

 <script type = "text/javascript" >
  $(document).ready(function($) {
    $('#university_name').autocomplete({
      source: 'autocomplete.php',
      minLength: 2,
      select: function(event, ui) {
        var x = ui.item.value;

        x = encodeURIComponent(x);
        location.href = "university_page.php?university_name=" + x + "&university_id=" + x;
        return false;
      }
    });
  }); < /script>

autocomplete.php

<?php
    include('conn.php');
    $searchTerm = $_GET['term'];
    $query = $db->query("SELECT * FROM all_university WHERE university_name LIKE '%".$searchTerm."%' ORDER BY university_name ASC");
    while ($row = $query->fetch_assoc()) {
      $data[] = $row['university_name'];
    }
    echo json_encode($data);
?>

当我在文本框中搜索时,如何在网址中传递ID仍然有university_page.php?university_name=xyz

但我想要

university_page.php?university_name=xyz&university_id=1 ?

谢谢

1 个答案:

答案 0 :(得分:0)

对于Jquery请求尝试此

contentView

请求的文件是:autocomplete.php 你必须通过

处理uni name
var get_url = "<baseurl>/autocomplete.php?university_name="+x;
    $.get(get_url, function(data, status){
    alert("Data: " + data + "\nStatus: " + status);
});