jquery作为回调执行请求

时间:2013-11-01 19:31:27

标签: php jquery jquery-callback

我想在回调中执行一个返回给我的请求,

事实上,这是一个系统,当我点击某些东西时,它会忽略对话,

所以我开始做我的查询,当我点击一个元素时,它会将数据发送到返回请求的php文件。

下面是我的jquery代码:

 $( ".load_message" ).click(function() {
            //On marque uniquement l'id de l'expediteur et du destinataire
            // pour chercher les messages expédiés par A vers B ou inversement
            var from = $(this).closest('tr').find('span.from').text();
            var to = $(this).closest('tr').find('span.to').text();
            $.ajax({
                type: 'POST',
                url: 'pages_ajax/fetch-messages.php',
                data: { from: from,  to: to},
                dataType: "json",
                beforeSend:function(){
                    // this is where we append a loading image
                    $('#message_display').empty()
                    $('#message_display').attr('style','text-align:center');
                    $('#message_display').append('<img src="images/loading.gif" alt="Loading..." />');
                },
                success:function(data){
                    // successful request; do something with the data
                    if(data.error==1){
                        $('#message_display').empty().removeAttr('style');

                    }
                    // if faliure
                    else if(data.error==0){
                        // failed request; give feedback to user
                        $('#message_display').empty().removeAttr('style');
                        $('#message_display').attr('class','error').empty().append('<b>Il n\'y a pas de messages à afficher.</b>');
                    }
                },
                error:function(){
                    // failed request; give feedback to user
                    $('#message_display').empty().removeAttr('style');
                    $('#message_display').attr('class','error').empty().append('<b>Il n\'y a pas de messages à afficher.</b>');
                }
            });
        });

在php文件中它只是返回一个sql请求

这是我的php文件

<?php

session_start();
require_once("../../lib_php/librairie.php");
require_once("../../config/connexion.php");
header('Content-Type: application/json; charset=utf8');

/**
 * Fonction qui retourne une liste de messages
 * @return int
 */
function fetchMessages() {
    $from = mysql_real_escape_string($_POST['from']);
    $to = mysql_real_escape_string($_POST['to']);

    $query = "SELECT `id`, `from`, `to`, `message`, `sent`, `read`, `direction` 
    FROM `cometchat` 
    WHERE `from` = {$from} || `from` = {$to} || `to` = {$to} || `to` = {$from}";
    $data = mysql_query($query);
    if (mysql_num_rows($data) == 0) {
        $data['error'] = 1;
        $data['sql'] = $data;
    } else {
        $data['sql'] = null;
        $data['error'] = 0;
    }

    return $data;
}

echo fetchMessages();

我不知道如何继续使用该查询创建循环以将所有结果显示到div中,因为这是我第一次返回查询,通常我只返回一些我应用的变量。但是这次我显示的数据太多了。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

试试这个:

function fetchMessages() {
    $from = mysql_real_escape_string($_POST['from']);
    $to = mysql_real_escape_string($_POST['to']);

    $query = "SELECT `id`, `from`, `to`, `message`, `sent`, `read`, `direction` 
    FROM `cometchat` 
    WHERE `from` = {$from} OR `from` = {$to} OR `to` = {$to} OR `to` = {$from}";

    $results = mysql_fetch_assoc(mysql_query($query));

    $data = array();
    if (!sizeof($results)) {
        $data['error'] = 1;
        $data['sql'] = $query;
    } else {
        $data['sql'] = null;
        $data['error'] = 0;
        $data['results'] = $results;
    }

    return $data;
}

用法:

$data = fetchMessages();
if ($data['error']) {
    // show error
}
else
{
    foreach ($data['results'] as $result) {
        // debug
        print_r($result);
    }
}

答案 1 :(得分:0)

第1步:不要使用MYSQL_ *,由于安全问题,它已被弃用。 第2步:使用预准备语句。

我在这里重写了你的php:

function fetchMessages() {
    $from = $_POST['from'];
    $to = $_POST['to'];

    $query = $database->prepare("SELECT `id`, `from`, `to`, `message`, `sent`, `read`, `direction` 
    FROM `cometchat` 
    WHERE `from` = ? || `from` = ? || `to` = ? || `to` = ?");
    $query->bind_param('ssss' $from, $to, $to, $from);
    $result=$query->execute();
    $query->data_bind($id, $rfrom, $rto, $message, $sent, $read, $direction);

    $data=""; //initialize the HTML you want in here, any sort of header for the data
    while ($query->fetch()){
      $data=$data.$id." ".$rfrom." ".$rto." ".$message." ".$sent." ".$read." ".$direction."<br/> //or whatever html formatting you want to do
    }
    $data=$data."<br/>" //or whatever tailing html you want to add to the data

    return $data;
}
echo  fetchMessages()