繁体   English   中英

如何显示从jQuery发布的PHP数组中的JS中的值

[英]How to display values in JS from a PHP array that was posted from jQuery

我有一个名为“ rename的按钮,当按下该按钮时, rename.php在jQuery中执行一个rename.php文件。 在该php文件中,程序从mysql中选择数据,使用该数据创建一个数组,然后处理到json_encode($array); 然后,如何获取该json编码的数组并将其回显到javascript中?

我试图回显数组,以便javascript显示我的图片src。

这是我的ajax的第二行,因此我只是像把javascript一样写出了javascript,因为我不确定js中的命令或结构。

$.ajax
        (
            {
                url:"test4.php",
                type: "GET",
                data: $('form').serialize(),
                success:function(result)
                {
                    /*alert(result);*/
                    document.getElementById("images_to_rename").innerHTML = foreach(jArray as array_values) 
                    {
                        "<img src=\""array_values['original_path']"/"array_values['media']"/>";
                    }
                }
            }
        );

和我的jQuery php文件:

<?php
include 'db/mysqli_connect.php';
$username = "slick";
if(empty($_GET['image_name']))
{
    echo '<div class="refto" id="refto">image_name is empty</div>';
}
else
{
    echo '<div class="refto" id="refto">image_name is not empty</div>';
    foreach($_GET['image_name'] as $rowid_rename)
    {
        //echo '<br><p class="colourful">rowid_refto: '.$rowid_refto.'</p><br>';

        $active = 1;
        $command = "Rename";
        $stmt = $mysqli->prepare("UPDATE ".$username." SET active=?, command=? WHERE rowid=?");
        $stmt->bind_param("isi", $active, $command, $rowid_rename);
        $stmt->execute();
        $stmt->close();
    }
                                                                                                //go to database, get parameters of sort
    $command = "Rename";
    $active = 1;
    $stmt = $mysqli->prepare("SELECT original_path, media FROM " . $username . " WHERE active=? and command=?");            
    $stmt->bind_param("is", $active, $command);
    $stmt->execute();
    $result = $stmt->get_result();
    while($row = $result->fetch_assoc())
    {
    $arri[] = $row;
    }
    foreach($arri as $rows)                                                                                         //put them into workable variables
    {
        $rowt = $rows['original_path'];
        $rowy = $rows['media'];
        //echo 'rows[\'original_path\'] = '.$rows['original_path'].''.$rows['media'].'';
    }
    $stmt->close();
    echo json_encode($arri);
    ?>
    <script type="text/javascript"> 
    var jArray= <?php echo json_encode($arri); ?>;

    </script>
    <?php


}       

echo "something2";
?>

我的PHP文件是jQuery url:“ test4.php”,键入:“ GET”,而不是主文件。 当用户单击重命名时,主文件称为main.php,而jQuery中称为test4.php。

有人建议控制台日志,所以这是chrome所说的:

<div class="refto" id="refto">image_name is not empty</div>[{"original_path":"Downloads","media":"shorter.jpg"},{"original_path":"Album 2","media":"balls.jpg"}]    <script type="text/javascript"> 
    var jArray= [{"original_path":"Downloads","media":"shorter.jpg"},{"original_path":"Album 2","media":"balls.jpg"}];

    </script>
    something2

您的ajax php文件未在浏览器中呈现,因此变量jArray未定义。 根据您的情况,让php文件返回json,然后将其作为变量保存。

    <?php
include 'db/mysqli_connect.php';
$username = "slick";
if (empty($_GET['image_name'])) {
    //echo '<div class="refto" id="refto">image_name is empty</div>';
} else {
    //echo '<div class="refto" id="refto">image_name is not empty</div>';
    foreach ($_GET['image_name'] as $rowid_rename) {
        //echo '<br><p class="colourful">rowid_refto: '.$rowid_refto.'</p><br>';

        $active = 1;
        $command = "Rename";
        $stmt = $mysqli->prepare("UPDATE " . $username . " SET active=?, command=? WHERE rowid=?");
        $stmt->bind_param("isi", $active, $command, $rowid_rename);
        $stmt->execute();
        $stmt->close();
    }
    //go to database, get parameters of sort
    $command = "Rename";
    $active = 1;
    $stmt = $mysqli->prepare("SELECT original_path, media FROM " . $username . " WHERE active=? and command=?");
    $stmt->bind_param("is", $active, $command);
    $stmt->execute();
    $result = $stmt->get_result();
    while ($row = $result->fetch_assoc()) {
        $arri[] = $row;
    }
    foreach ($arri as $rows) {                                                                                         //put them into workable variables
        $rowt = $rows['original_path'];
        $rowy = $rows['media'];
        //echo 'rows[\'original_path\'] = '.$rows['original_path'].''.$rows['media'].'';
    }
    $stmt->close();
    echo json_encode($arri);
    //stop render here
    die();
}
?>

php文件只需要返回json字符串。 然后我们将结果作为javascript中的json变量。

和Js:

$.ajax
    (
        {
            url:"test4.php",
            type: "GET",
            data: $('form').serialize(),
            success:function(result)
            {
                var jArray = JSON.parse(result);
                /*alert(result);*/
                var txt = "";
                jArray.forEach(function(array_values){
                    txt += `<img src=\""array_values.original_path."/"array_values.media"/>`;
                })  
                document.getElementById("images_to_rename").innerHTML = txt;
            }
        }
    );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM