【问题标题】:How to pass querystring with JQuery?如何使用 JQuery 传递查询字符串?
【发布时间】:2011-10-31 01:50:27
【问题描述】:

早上好,我目前正在做一个简单的 php 项目,应用程序的目标是读取 Oracle 数据并显示结果,我已经完成了这部分。我对如何将查询字符串与 JQuery 一起传递有点疑问,请指教。非常感谢。

index.php - 这是我传递查询字符串的地方 index.php?campus=abc&floor=xyz

<html>
<head>`enter code here`
    <title>Olympia College :: Kiosk</title>
    <link rel="Stylesheet" type="text/css" href="assets/css/kiosk.css" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
// $(document).ready(function()
// {
//   $("#responsecontainer").load("contents.php");
//   var refreshId = setInterval(function()
//   {
//       $("#responsecontainer").load('contents.php?randval='+ Math.random());
//   }, 1000);
//   $.ajaxSetup({ cache: false });
// });

function update() {
  $("#notice_div").html('<img src="indicator.gif" alt="Synchronizing data, please wait a moment.."/>&nbsp;&nbsp;Synchronizing data, please wait a moment..');
  $.ajax({
    type: 'POST',
//    data: '{campus, floor}',
    url: 'timetable.php',
    timeout: 5000,

        success: function(data) {
          $("#some_div").html(data);
          $("#notice_div").html('');
          window.setTimeout(update, 10000);
        },

        error: function (XMLHttpRequest, textStatus, errorThrown) {
          $("#notice_div").html('Timeout contacting server..');
          window.setTimeout(update, 60000);
        }
    });
}
$(document).ready(function() {
    update();
});

</script>
</head>
<body>
    <!--<div id="responsecontainer">
    <?php //echo date("l, F d, Y h:i:s" ,time());?>
    </div>-->
    <div id="main">
        <div id="left"><img src="assets/images/oc_logo.png" alt="Olympia College Malaysia"></img></div>
        <div id="right"><div id="notice_div"></div><div id="fright"><a href="setting.php" title="Change settings">Setting</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="bug.php" title="Send bug report">Bug Report</a></div></div>    
    </div>
    <div id="line"></div>

    <br />
    <div id="some_div"></div>
</body>

时间表.php

<?php
$datasource = "******";
$hostname = "******";
$username = "*****";
$password = "*****";
$database = "*****";
$campus = $_REQUEST['campus'];
$floor = $_REQUEST['floor'];

//KL CAMPUS
if ($campus == 'ockl')
    $campus = 'KL CAMPUS';
elseif ($campus == 'ocpj')
    $campus = 'PJ CAMPUS';
elseif ($campus == 'ocpg')
    $campus = 'PENANG CAMPUS';
elseif ($campus == 'ocip')
    $campus = 'IPOH CAMPUS';
elseif ($campus == 'ockt')
    $campus = 'KUANTAN CAMPUS';
elseif ($campus == 'ocjb')
    $campus = 'JB CAMPUS';

//echo $_REQUEST['campus'];

$connect = odbc_connect($datasource, $username, $password);

    if(!$connect){
        echo "Unable to connect!<br /><br />";
    }
    else {
        //echo "Successfully connected!<br /><br />";
    };

$query = "SELECT DISTINCT 
          RESOURCE_DAY_TIME_SETUP.DAY, 
          RESOURCE_DAY_TIME_SETUP.FLOOR_CODE,
          RESOURCE_DAY_TIME_SETUP.RESOURCES_CODE,
          RESOURCE_DAY_TIME_SETUP.CAMPUS 
          FROM RESOURCE_DAY_TIME_SETUP WHERE CAMPUS LIKE '" . $campus . "'";
$result = odbc_exec($connect, $query);

echo "<table border='1'>";
echo "<tr> <th>Day</th> <th>Floor</th> <th>Room</th> <th>Duration</th> <th>Start</th> <th>End</th> <th>Lecturer</th> <th>Status</th> <th>Campus</th> </tr>";

while (odbc_fetch_row($result)){
    $day = odbc_result($result,1);
        $floor_code = odbc_result($result, 2);
    $resources_code = odbc_result($result,3);      
    $campus = odbc_result($result,4);
        echo "<tr>";
        echo "<td>$day</td>";
        echo "<td>$floor_code</td>";
        echo "<td>$resources_code</td>";
        echo "<td>$resources_code</td>";
        echo "<td>$resources_code</td>";
        echo "<td>$resources_code</td>";
        echo "<td>$resources_code</td>";
        echo "<td>$resources_code</td>";
        echo "<td>$campus</td>";
        echo "</tr>";
}

odbc_close($connect);

echo "</table>";
?>

希望这里有人能给我指出正确的方向,非常感谢。

【问题讨论】:

    标签: php jquery oracle query-string


    【解决方案1】:

    由于您没有发布除查询字符串以外的任何数据,我认为您希望使用 GET 而不是 POST,但由于您的 php 文件使用$_REQUEST,两者都可以工作。只需像这样格式化您的数据:

    type: 'GET',
    data: {campus: 'abc', floor: 'xyz'},
    url: 'timetable.php',
    

    【讨论】:

    • +1 - 此外,如果 OP 使用 GET,他可能希望将 cache 设置为 false,因为众所周知,许多浏览器会缓存结果。
    • 嗨,保罗,感谢您的回复。我需要将数据作为查询字符串传递,即 index.php?campus=ockl&floor=1。原因是这个简单的应用程序必须按楼层显示正在进行的课程的详细信息,我不希望用户设置查询字符串参数的值,我正在考虑使用预设的查询字符串值启动 index.php 页面地址栏本身。
    • @Sashi jQuery 会在你将类型更改为 'GET' 时为你做到这一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 2020-11-23
    相关资源
    最近更新 更多