-2

我有

enter code here

function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!

try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
} catch (e){
    // Internet Explorer Browsers
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            // Something went wrong
            alert("Your browser is too old to run me!");
            return false;
        }
    }
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
    $.post('userfind.php', function(data) {


    document.getElementById("myTable").style.display = "block"; 
    var x=document.getElementById("myTable");
    for (var i = 0; i < data.length; i++) { 

    var row=x.insertRow(-1);
    var cell1=row.insertCell(-1);
    var cell2=row.insertCell(-1);
    ...
    ...

并在 php

enter code here

<?php 

session_start();
$username = "XXXXXXXX";
$password = "XXXXXXXX";
$database = "XXXXXXXX";
$link = mysql_connect("localhost", "$username", "$password");
if(!$link) {echo("Failed to establish connection to mysql server");
         exit();}

$status = mysql_select_db($database);


$oId = mysql_real_escape_string($_POST["order_IDsearch"]);


if (isset($order_IDsearch)){
$result = mysql_query ("SELECT * FROM personal_info WHERE  order_id= '".$oId."' ");
$myjsons = array();
while($row = mysql_fetch_assoc($result)){ 
$myjsons[] = $row;
}
echo json_encode($myjsons);
}

?>

如果我删除 SQL 条件、matrk if 并标记 $_post,javascript 将显示表格

如果我离开 php 如上所示,它不会显示表格,

请问php页面有什么问题


这是整个 javascript ajax 函数,

enter code here

function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!

try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
} catch (e){
    // Internet Explorer Browsers
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            // Something went wrong
            alert("Your browser is too old to run me!");
            return false;
        }
    }
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){




    $.post('userfind.php', {orderId:"order_IDsearch"}, function(data) {
    var obj = $("#myTable").show();
    var x = obj.get(0);
    for (var i = 0; i < data.length; i++) { 

    var row=x.insertRow(-1);
    var cell1=row.insertCell(-1);
    ...
    ...



   cell1.innerHTML = "<b><input name='edit' type='button' onClick='editRow(this)' value='Edit' />    <input name='del' type='button' onClick='delRow(this)' value='Del' /></b>";
   cell2.innerHTML =  data[i].user_id;
   cell3.innerHTML =  data[i].first_name ;
   ....
   ....  

 }},'json');}
    }
   ajaxRequest.open("POST", "userfind.php", true);
   ajaxRequest.send(null); 
    }

太困惑了,请帮我修改代码,我应该重新编写代码吗?可以修改此代码吗?

4

2 回答 2

6

您没有将任何内容从$.post...传递给您的 php 文件

让它像

$.post('userfind.php', {order_IDsearch: "your data"}, function(data){
// your implementation
});

这应该工作...

如果您的 order_IDsearch 是动态的,那么就这样做

$.post('userfind.php', {yourData:order_IDsearch}, function(data){
    // your implementation
    });

在 PHP 方面,您必须在

$_POST['yourData'];

发送多个值

$.post('userfind.php', {key1:value1,key2:value2,...}, function(data){
        // your implementation
        });
于 2012-07-27T12:23:52.473 回答
3

Aditya Parab确实回答了您的问题,但我必须插入一些您需要了解的 jQuery 基础知识。

您可以(并且应该)大幅简化您的功能。所有这些ajaxRequest东西,前 20 行左右的代码,都是在您不使用 jQuery Ajax 时使用的。这些东西是内置在 jQuery 中的,你不必这样做。

此外,在 jQuery 中,您可以通过声明var object = $('#objectId').get(0). 你不需要document.getElementsById()


简化功能


function ajaxFunction(){
    $.post('userfind.php', {var1: "value 1"}, function(data) {
        var obj = $("#myTable").show();
        var x = obj.get(0); //Get the JS object from the jQuery Object
        for (var i = 0; i < data.length; i++) {
            var row=x.insertRow(-1);
            var cell1=row.insertCell(-1);
            var cell2=row.insertCell(-1);
        }
        ...
        ...
    });
}
于 2012-07-27T12:37:43.970 回答