dongqishou7471 2016-11-24 12:16
浏览 17

一些php代码追加

I need to append some php code, when user click in sample button, my code works properly, but I need do my job cleaner. I've searched and I found out, I should be using jquery ajax for this, but I don't know what should to do with ajax. I read some articles about jquery ajax, and I found some functions, like $.ajax(), $.get(), $.post() but I dont know really what to do? can someone say me, what I should to do

button.live( 'click', function(){
    sample.append(<?phpcodes?>);
});
append(
   '<?php echo get_template_directory_uri(); ?>'+
    '<?php foreach ( $terms as $term ){ echo '<option value="'. $term->slug .'">'. $term->name .'</option>'; } ?>'+
    '<?php echo __( "Sample","textDomain" ); ?>'+
    '<?php echo $example[0]['options']['0']['id']; ?>'+
    'and more ...'
);
  • 写回答

1条回答 默认 最新

  • douhan4093 2016-11-25 12:19
    关注

    You should put the PHP code in a separate .php file and call that file with JavaScript (ajax) when the user clicks the button. When the file is called the php functions in that file will execute.

    Examlple HTML

    <div id="mybutton">My button</div>
    <div id="result">My result</div>
    

    Example JavaScript

    $( "#mybutton" ).on( "click", function() {
        $( "#result" ).load( "myphpcode.php", function() {
        });
    });
    

    Example myphpcode.php

    <?php
    
        // Do your cleaning here
    
        echo 'Done cleaning!';
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗