如何在脚本中附加php代码

时间:2016-11-26 18:14:45

标签: javascript php jquery ajax append

点击.button - >追加{HTML + PHP代码}内部样本ul

<script>

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

</script>

/ *点击此按钮时执行* /

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

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

<ul>

</ul>
像这样......

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++;

        });

    });

按钮点击时附加php + html代码的真实方法? 如何使用jquery ajax,(在脚本标记/文件中使用php数组)? 如何使用json(从其他文件中获取php代码)? 我该怎么做以避免spageti代码? 谢谢!

2 个答案:

答案 0 :(得分:2)

您无法在JavaScript中附加PHP代码。 PHP是服务器端语言,因此需要在服务器上执行,而JavaSript则在客户端执行(在用户中#39) ;浏览器)。

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

答案 1 :(得分: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>