将 PHP 变量转换为 JQuery

如果我单击按钮,我会尝试从音频标签更新 src。所以我需要将 $muziek 变量转换为 Jquery


看法:


<?php

    foreach ($muziek as $ms)

    {

        if ($ms->id == 2) {

            echo '<audio id="player" controls src="data:audio/mpeg;base64,' . base64_encode($ms->audio) . '">';

            echo '</audio>';

        }

    }


    foreach ($muziek as $ms)

    {

        echo '<br>';

        echo '<input id="'.$ms->id.'" type="button" value="' . $ms->naam . '" class = "btn btn-login login-formcontrol"/>';

    }

?>

</div>


<script>

    $("input").click(function () {

       var test = $(this).attr("id");

       console.log(test);

       //Here needs to be the foreach muziek       

    });

</script>

Muziek 变量:这就是我填充音乐变量的方式


function getAllMuziek()

{

    $query = $this->db->get('muziek');

    $muziek = $query->result();


    return $muziek;

}

有人有想法或告诉我如何做到这一点吗?


慕丝7291255
浏览 181回答 2
2回答

aluckdog

我花了一些时间试图弄清楚你想要什么,并根据我的理解你想要返回一个数组,所有muzieks返回给你的 js 来做你想做的任何事情,你可以简单地通过一个简单的 ajax 请求得到:$.get( "base_url/your_controller/getAllMuziek" )&nbsp; &nbsp; .done(function( muziek ) {&nbsp; &nbsp; &nbsp; &nbsp; //Here needs to be the foreach muziek&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $.each(muziek, function( index, value ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // whatever&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });对您的方法进行简单修改getAllMuziek:function getAllMuziek(){&nbsp; &nbsp; $query = $this->db->get('muziek');&nbsp; &nbsp; $muziek = $query->result();&nbsp; &nbsp; header('Content-Type: application/json');&nbsp; &nbsp; echo json_encode($muziek);}现在当你让你调用 ajax 时,你会得到你的结果。

凤凰求蛊

使用转换$muziek为javascript数组json_encode<script>var myArray = <?php echo json_encode($muziek); ?>;</script>
打开App,查看更多内容
随时随地看视频慕课网APP