如何在脚本中添加php代码

时间:2021-07-17 00:07:18

when click on .button -> append { HTML + PHP codes } inner sample ul

当单击.button ->附加{HTML + PHP代码}内样例ul时

<script>

$(document).ready( function(){
   $('.button').on( "click", function() {
       $('.ul').append('<li>'+ about 100 lines HTML + PHP codes+'</li>');
   }
});

</script>

/* when click on this button do something */

/*当点击这个按钮时,做一些事情*/。

<button class="button">button</button>

/* append() inner this ul tag */

/* append()在这个ul标签内*/

<ul>

</ul>

something like this ...

是这样的……

jQuery(document).ready( function() {

        var count = 0;
        var button   = jQuery('.add-module');
        var sample  = jQuery('.sample-page-builder');
        var geturi   = '<?php echo get_template_directory_uri(); ?>';
        var category = '<?php foreach ( $terms as $term ){ echo '<option value="'. $term->slug .'">'. $term->name .'</option>'; } ?>';

        button.live( 'click', function() {

            sample.append('<li class="apanel"><div class="apanel-head"><?php _e( 'Module Title', 'sample' ); ?></div><div class="apanel-body"><div class="apanel-content"><label for="sample_page_builder['+ count +'][title]" class="apanel-label"><?php _e( 'Module Title', 'sample' ); ?></label><input type="text" id="sample_page_builder['+ count +'][title]" name="sample_page_builder['+ count +'][title]" value="" size="150"></div><div class="apanel-content"><label for="meta-radio-1" class="apanel-label"><?php _e( 'Module Layout', 'sample' ); ?></label><ul class="radio-images"><li><input type="radio" name="sample_page_builder['+ count +'][layout]" id="meta-radio-1" value="" /><img src="'+ geturi +'/assets/img/module-1.png" alt="Layout 1"></label></li><li><input type="radio" name="sample_page_builder['+ count +'][layout]" id="meta-radio-2" value="" /><img src="'+ geturi +'/assets/img/module-2.png" alt="Layout 2"></label></li><li><input type="radio" name="sample_page_builder['+ count +'][layout]" id="meta-radio-3" value="" /><img src="'+ geturi +'/assets/img/module-3.png" alt="Layout 3"></label></li><li><input type="radio" name="sample_page_builder['+ count +'][layout]" id="meta-radio-4" value="" /><img src="'+ geturi +'/assets/img/module-4.png" alt="Layout 4"></label></li><li><input type="radio" name="sample_page_builder['+ count +'][layout]" id="meta-radio-5" value="" /><img src="'+ geturi +'/assets/img/module-5.png" alt="Layout 5"></label></li><li><input type="radio" name="sample_page_builder['+ count +'][layout]" id="meta-radio-6" value="" /><img src="'+ geturi +'/assets/img/module-6.png" alt="Layout 6"></label></li><li><input type="radio" name="sample_page_builder['+ count +'][layout]" id="meta-radio-7" value="" /><img src="'+ geturi +'/assets/img/module-7.png" alt="Layout 7"></label></li><li><input type="radio" name="sample_page_builder['+ count +'][layout]" id="meta-radio-8" value="" /><img src="'+ geturi +'/assets/img/module-8.png" alt="Layout 8"></label></li><li><input type="radio" name="sample_page_builder['+ count +'][layout]" id="meta-radio-9" value="" /><img src="'+ geturi +'/assets/img/module-9.png" alt="Layout 9"></label></li><li><input type="radio" name="sample_page_builder['+ count +'][layout]" id="meta-radio-10" value="" /><img src="'+ geturi +'/assets/img/module-10.png" alt="Layout 10"></label></li><li><input type="radio" name="sample_page_builder['+ count +'][layout]" id="meta-radio-11" value="" /><img src="'+ geturi +'/assets/img/module-11.png" alt="Layout 11"></label></li><li><input type="radio" name="sample_page_builder['+ count +'][layout]" id="meta-radio-12" value="" /><img src="'+ geturi +'/assets/img/module-12.png" alt="Layout 12"></label></li></ul></div><div class="apanel-content"><label for="sample_page_builder['+ count +'][number]" class="apanel-label"><?php _e( 'Max Number of Post', 'sample' ); ?></label><input type="text" id="sample_page_builder['+ count +'][number]" name="sample_page_builder['+ count +'][number]" value=""></div><div class="apanel-content"><label for="sample_page_builder['+ count +'][offset]" class="apanel-label"><?php _e( 'Posts Offset', 'sample' ); ?></label><input type="text" id="sample_page_builder['+ count +'][offset]" name="sample_page_builder['+ count +'][offset]" value=""></div><div class="apanel-content"><label for="sample_page_builder['+ count +'][order]" class="apanel-label"><?php _e( 'Order Posts by', 'sample' ); ?></label><select name="sample_page_builder['+ count +'][order]" id="sample_page_builder['+ count +'][order]"><option value="date">Order by Date</option><option value="title">Order by Title</option><option value="rand">Random Order</option><option value="comment_count">Number of Comments</option></select></div><div class="apanel-content"><label for="sample_page_builder['+ count +'][category]" class="apanel-label"><?php _e( 'Filter by Category', 'sample' ); ?></label><select name="sample_page_builder['+ count +'][category]" id="sample_page_builder['+ count +'][category]">'+ category +'</select></div><input id="sample_page_builder['+ count +'][id]" name="sample_page_builder['+ count +'][id]" value="" type="hidden" /></div></li>');

            count++;

        });

    });

its true way to append php + html codes when button click?? how do this work with jquery ajax, ( use php array in a script tag/file )?? how do this work with json ( get php codes from other file )?? what i should to do to avoid spageti codes? thanks!

当按下按钮时,它的真实方法是添加php + html代码。如何使用jquery ajax(在脚本标记/文件中使用php数组)进行此工作?如何使用json(从其他文件获取php代码)?如何避免spageti编码?谢谢!

2 个解决方案

#1


2  

You cannot append PHP code in JavaScript. PHP is a server-side language, so it needs to be executed on the server, while JavaSript is executed on the client side (in a user's browser).

不能在JavaScript中附加PHP代码。PHP是一种服务器端语言,因此它需要在服务器上执行,而JavaSript在客户端(在用户的浏览器中)执行。

So if you append a PHP code, there will be only source code visible to the end user.

因此,如果您附加一个PHP代码,那么最终用户只能看到源代码。

#2


1  

You can do it like this

你可以这样做

$(function(){
    $(".button").on("click",function(){
        $('.ul').append('<li>'+ "<h1>HTML CODE </h1>" + '<?php $array = ["array1", "array2"]; echo("PHP CODE - " . $array[0] ); ?>' + '</li>');
    });
});

*

*

<button class="button">button</button>
<ul class = "ul"></ul>

#1


2  

You cannot append PHP code in JavaScript. PHP is a server-side language, so it needs to be executed on the server, while JavaSript is executed on the client side (in a user's browser).

不能在JavaScript中附加PHP代码。PHP是一种服务器端语言,因此它需要在服务器上执行,而JavaSript在客户端(在用户的浏览器中)执行。

So if you append a PHP code, there will be only source code visible to the end user.

因此,如果您附加一个PHP代码,那么最终用户只能看到源代码。

#2


1  

You can do it like this

你可以这样做

$(function(){
    $(".button").on("click",function(){
        $('.ul').append('<li>'+ "<h1>HTML CODE </h1>" + '<?php $array = ["array1", "array2"]; echo("PHP CODE - " . $array[0] ); ?>' + '</li>');
    });
});

*

*

<button class="button">button</button>
<ul class = "ul"></ul>