单击时使用AJAX(jQuery、PHP、MySQL)

单击时使用AJAX(jQuery、PHP、MySQL),php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,在我的下一个项目中开始使用AJAX,我有点不知所措! 我有一个订单页面,页面上大约有50个用户,每个用户都有大量的数据。直接显示所有信息是愚蠢的,因为加载时间等。 因此,只需显示此人的摘要,单击时,使用AJAX即可完成订单 到目前为止,我的例子是: <div class="row bottom-buffer"> <div class="col-sm-2"> $date

在我的下一个项目中开始使用AJAX,我有点不知所措! 我有一个订单页面,页面上大约有50个用户,每个用户都有大量的数据。直接显示所有信息是愚蠢的,因为加载时间等。 因此,只需显示此人的摘要,单击时,使用AJAX即可完成订单

到目前为止,我的例子是:

<div class="row bottom-buffer">
                    <div class="col-sm-2">
                            $date
                    </div>
                    <div class="col-sm-3">
                        <a href="#" class="person" name="order_no">$name</a>
                    </div>
                    <div class="col-sm-2">
                        $order_status
                    </div>
                    <div class="col-sm-3">
                        $order_date
                    </div>
                    <div class="col-sm-2 text-center">
                        <A href="#" class="person" name="order_no">$order</A>
                    </div>
                </div>
<div class="contactInfo ">
    <form action="#" method="post">
        //Big a$$ order I want pulled from the DB such as name, order number, order status, notes, payment etc
    </form>
</div>

我已经准备好了SQL,我只是不知道如何使用AJAX调用我的PHP文件。我是否在.click函数中执行AJAX调用?任何帮助都将不胜感激

您还没有真正在这里搜索过。 但既然你努力提出了一个复杂的问题,我就给你一个答案

是的,您在正在调用的单击功能中执行所有操作

然后可以使用以下jQuery方法之一:ajax、get、post、getJSON。 -是最完整的方法,也是所有其他人实际使用的方法,但没有太多细节

非常明显,它们已经定义了类型

还有一个——因为JSON是当今非常著名的信息交换方式

您应该选择最适合您的方法,例如,如果您正在PHP上构建HTML以返回页面,而您不希望JavaScript这样做,那么让我们以post为例

这段代码里面的点击,这是一个简单的例子,检查上述方法的文档

$.get( "example.php", function(data) {
 $('#yourHtmlElement').html(data);
});

当我们刚开始的时候,我们都在努力解决这个问题。这里有一个很好的处理方法

可以重用的Ajax函数

function ajaxGet(url, callBack){
    $.ajax({
        'url':url,
        'dataType': 'json'
    }).done(function(response){
        //ajax has finished and you've receive the response
        //now just pass the response to the callBack
        window[callBack](response);
    }).fail(function(x, h, r){
        console.log("fail: " + r);
    });
}
那么你可以这样称呼它

ajaxGet("/some/url?with=params", "nameOfSomeCallBackFunction");
<?php
     $param = $_GET['with'];  //will be value of 'params'

     //Do some stuff like connect to Database or computation

     //when you finish you can easily send a json response

     echo json_encode($results);  //This will be the ajax response
?>
您需要创建回调函数

function nameOfSomeCallBackFunction(response){
    //Do whatever you want with the JSON response
}
在PHP端,您将获得如下参数

ajaxGet("/some/url?with=params", "nameOfSomeCallBackFunction");
<?php
     $param = $_GET['with'];  //will be value of 'params'

     //Do some stuff like connect to Database or computation

     //when you finish you can easily send a json response

     echo json_encode($results);  //This will be the ajax response
?>


希望这有帮助。祝你好运。

“我叫阿贾克斯吗…”-是的。用法:$.post('phpfile.php',{params:params},函数(响应){//code…});我还没有彻底搜查过。但是足以让我迷惑的是,这是肯定的,哈哈。至少能验证我在正确的轨道上是很好的。非常感谢您的投入!谢谢你,伙计。这是我需要的启动。直截了当!非常感谢!