简体   繁体   English

如何在单引号之间添加两个变量

[英]How can i add two variables between single quotes

I have我有

function delImage(posteId,imagebinId) { 
    $.get("<?php  echo base_url('/Home/deletePostimage/') ?>"); 

    return false; 
} 

i want to be like /Home/deletePostimage/posteId/imagebinId我想像 /Home/deletePostimage/posteId/imagebinId

i tried我试过

$.get("<?php  echo base_url('/Home/deletePostimage/')+posteId+"/"+imagebinId ?>"); 

but it divide the two numbers但它将两个数字相除

like posteId=5 imagebinId =2像 posteId=5 imagebinId =2

the results will be结果将是

/Home/deletePostimage/2,5 /首页/删除帖子图片/2,5

the params from来自的参数

<a class="postimgsd" onClick="if(confirm('Are you sure you want to delete this image ?')){delImage(<?php echo $value['id'],$get_images[0]['binId']; ?>); rmItem(<?php echo $i; ?>);}else{} ;  " >

With ES6, you can do this with string interpolation :在 ES6 中,你可以通过字符串插值来做到这一点:

$.get(`<?php  echo base_url('/Home/deletePostimage/${postId}/${imagebinId}') ?>`);

Without ES6, there is another answer that answers it, or use this:如果没有 ES6,还有另一个答案可以回答它,或者使用这个:

var url = '/Home/deletePostimage/' + postId + '/' + imagebinId;
$.get("<?php  echo base_url('" + url + "') ?>");

Furthermore, the code that calls this function should actually be:此外,调用此函数的代码实际上应该是:

<a class="postimgsd" onClick="checkDeletion()" />

<script type='text/javascript'>
    function checkDeletion() {
        if(confirm('Are you sure you want to delete this image ?')) {
            var postId = <?php echo $value['id']; ?>;
            var imagebinId = <?php echo $get_images[0]['binId']; ?>;
            delImage(postId, imagebinId);
            rmItem(<?php echo $i; ?>);
        }
    }
</script>

You need to keep in mind that PHP is processed on back-end before send the html to your user browser.您需要记住,在将 html 发送到您的用户浏览器之前,PHP 是在后端处理的。 That said, everything between php tags ( <?php ... ?> ) is rendered/processed before javascript even exists.也就是说,php 标记( <?php ... ?> )之间的所有内容都在 javascript 存在之前呈现/处理。

So if you put any JavaScript code inside your PHP code it won't work.因此,如果您在 PHP 代码中放入任何 JavaScript 代码,它将无法工作。

To make it work and be clean, do something like this:要使其正常工作并保持清洁,请执行以下操作:

function delImage(posteId,imagebinId) { 
    let url = "<?php  echo base_url('/Home/deletePostimage/') ?>";
    $.get(url + posteId + "," + imagebinId); 

    return false; 
}

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

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