![](/img/trans.png)
[英]How can I convert this PHP code in a way that will allow me to add it in .JS using Ajax?
[英]Using AJAX to add PHP backend code in js file
目前我有一個 PHP 文件和 a.js 文件。 在我的 PHP 文件中,我有名為 wrapper_tab-content 的 div class。 現在,此代碼出現在我的 js 文件中,該文件顯示以下代碼:
$(document).ready(function() {
portalarray = new Array();
$('input.checkbox').change(function(){
portalname = $(this).attr('data-name'); pid= $(this).attr('id');
if ($(this).is(':checked')) {
portalarray.push(pid);
$(".wrapper_tab-content").append('<div class="portalcontent content--active" id="'+pid+'"><div class="col-md-12 text-left">
<label class="control-labels ">Title</label><input id="'+pid+'" name="'+portalname+'" placeholder="'+portalname+' Title" type="text"></div></div>');
}else {
$(".portaltabs .container--tabs li#"+pid).remove();
$(".wrapper_tab-content #"+pid).remove();
tabslength = $(".wrapper_tab-content").length;
}
});
});
現在我必須在值屬性中輸入屬性標題,我可以在 my.js 文件中使用<?php get_portals[0]['property_title']?>
制成?
為您解決問題 Javascript 是客戶端,PHP 是服務器端。
您正在做的是將 PHP 值轉換為 Javascript。有幾種方法可以做到這一點。 大多數程序員使用事件,因為服務器端可能在頁面的生命周期內更新
最常見的是通過 Ajax 發布和獲取請求,但您也可以通過事件獲取數據
<script type='text/javascript'>
document.body.onclick(function(){
var Variable = <?php echo(json_encode($Variable)); ?>;
};
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.