簡體   English   中英

如何將 PHP 變量傳遞給 another.JS 文件以在 JQUERY 中使用它?

[英]How to pass a PHP variable to an another .JS file for using it in JQUERY?

我是 JQUERY 初學者。 我正在嘗試將 PHP 變量傳遞給 js 文件,以便我可以在 JQUERY 中使用它。 但是找不到資源來學習這樣做。 I'm able to pass a JQUERY variable to a different php file but I coundn't pass PHP variable to a jquery file? 真的可以這樣做嗎?

function.php:

<?php $variable = $fetch['username']; ?>

如何在 app.js 中使用這個變量?

$(document).ready(function(){ var flwbtn = $(".findrowpplfollowbtn");

flwbtn.click(function(){
    var selectflwusername = $(this).parent().prev().last().find(".findpplrowusername").text();
    
    $.post("../php/flw.php",{flwusernameselected:selectflwusername})
    
});
});

將其回顯到 HTML 模板。 如果它是一些結構/數組,例如使用 json 格式。

<script>
<?php $variable = $fetch['username']; ?>
var js_variable = <?php echo json_encode($variable); ?>
</script>

或回顯一些隱藏的元素,例如

<input type="hidden" id="custId" name="custId" value="<?php echo($fetch['username']); ?>">

並抓住 Jquery 之類的

<script>
$( "#custId" ).value();
</script>

使用您的代碼 JS:

$(document).ready(function(){ var flwbtn = $(".findrowpplfollowbtn");

flwbtn.click(function(){
    var selectflwusername = $(this).parent().prev().last().find(".findpplrowusername").text();

    $.post("../php/flw.php",{$( "#custId" ).value()})

});
});
<script>
<?php $variable = $fetch['username']; ?>
var name = <?php echo json_encode($variable); ?>
alert('name');
</script>

get your customerid

<input type="text" id="custId" name="custId" value="<?php echo($fetch['username']); ?>">


<script>
$( "#custId" ).value();
</script>

暫無
暫無

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

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