javascript $ .post将参数传递给外部PHP文件

时间:2012-07-27 12:19:42

标签: php javascript ajax json xmlhttprequest

我有

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条件,则javascript将显示该表,如果并且标记为$ _post

则为matrk

如果我按照上面的说法离开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); 
    }

如果我再次编写代码,请帮助我修改代码,这让我很困惑?可以修改此代码吗?

2 个答案:

答案 0 :(得分: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
        });

答案 1 :(得分:3)

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

您可以(而且应该)DRASTICALLY简化您的功能。所有这些ajaxRequest内容,前20行左右的代码,都是在你不使用jQuery Ajax时使用的。这些内容构建在jQuery中,你不必这样做。

此外,在jQuery中,您可以通过声明var object = $('#objectId').get(0)来获取其ID的对象。您不需要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);
        }
        ...
        ...
    });
}