簡體   English   中英

如何從PHP到jQuery 1檢索回聲數據1

[英]how to retrieve echo data from php to jquery 1 by 1

我有一個單擊的JQuery,它將php查詢發送到MySql,然后我想在JQuery上將數據一一發送回去。

但是我只知道如何將結果從php發送回整個JQuery。

我目前的JQuery:

$(function() {
    $(".img_thumb_holder").on("click", function() {
        $.ajax({
        type: "POST",
        url: "CMS/PHP/retrieveAuthorDetails.php",
        success: function(data) {
                alert(data);
            }
        });
    });
});

我目前的php:

<?php
include 'dbAuthen.php';
$sql = "SELECT * FROM users WHERE Name = 'james'";

$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
    echo $row['UserID'];;
    echo $row['EmailAddr'];
}
?>

輸出都是UserID和EmailAddr,我不知道如何只顯示UserID或EmailAddr

我嘗試過alert(data [0]),但是它只顯示結果的一個字母。有關如何執行此操作的任何想法?

更新:在肖恩的幫助下,我有了當前更新的代碼

jQuery的:

$(function() {
    $(".img_thumb_holder").on("click", function() {
        $.ajax({
        type: "POST",
        dataType: "json",
        url: "CMS/PHP/retrieveAuthorDetails.php",
        success: function(data) {
                $.each(data, function() {
                    var userid = data.userid;
                    var useremail = data.email;
                    // i think there something wrong with this as it will keep repeating storing the userid and email for each data.. can someone verify?
                });
            }
        });
    });
});

PHP

<?php
include 'dbAuthen.php';
$sql = "SELECT * FROM users WHERE Name = 'honwenhonwen'";

$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
    $arr = array(
        "userid" => "HonWen",
        "email" => "honwen@hotmail.com"
    );
}

echo json_encode($arr);
?>

在您的php中,將結果保存到數組中-

<?php
include 'dbAuthen.php';
$array = array(); // create a blank array
$sql = "SELECT * FROM users WHERE Name = 'james'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
    // add each result to the array
    $array[] = array('UserID'=> $row['UserID'], 'EmailAddr'=> $row['EmailAddr']);
}

echo json_encode($array); // json_encode() the array
?>

然后在js / ajax中,您可以遍歷每個值

$(function() {
    $(".img_thumb_holder").on("click", function() {
        $.ajax({
          type: "POST",
          url: "CMS/PHP/retrieveAuthorDetails.php",
          success: function(data) {
            // loop through each returned value
            $.each(data, function(){
                  //alert each result, this is just an example as alert() for each result is not a great idea
                  alert("UserID:"+ this.UserID + " EmailAddr:" + this.EmailAddr);
            });
          }
        });
    });
});

jQuery的

$(function() {
        $(".img_thumb_holder").on("click", function() {
            $.ajax({
            type: "POST",
            dataType: "json",
            url: "CMS/PHP/retrieveAuthorDetails.php",
            success: function(data) {
                    $.each(data, function() {
                        var userid = data.userid;
                        var useremail = data.email;
                        // i think there something wrong with this as it will keep repeating storing the userid and email for each data.. can someone verify?
                    });
                }
            });
        });
    });

PHP

<?php
include 'dbAuthen.php';
$sql = "SELECT * FROM users WHERE Name = 'honwenhonwen'";

$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
    $arr = array(
        "userid" => "HonWen",
        "email" => "honwen@hotmail.com"
    );
}

echo json_encode($arr);
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM