【问题标题】:Using PHP in Jquery Variable在 Jquery 变量中使用 PHP
【发布时间】:2015-01-19 21:13:29
【问题描述】:

我正在尝试使用 Ajax 格式化 Wordpress 评论列表中新 cmets 的外观。这是我正在处理的 sn-p。

完整的文章可以在这里找到:http://pastebin.com/UHnPgf4J

success: function(data, textStatus){
if(data=="success"){
var avatar = <?php echo get_avatar( $comment, 32 ); ?>;
var author =  <?php the_author_meta( 'user_url'); ?>;
var timestamp = <?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?>;
var commenttext =  jQuery('#comment').val();

jQuery('<li>'+'<div class="comment-author vcard">'+avatar+
      '<div class="comment-meta">'+author+'</div>'+
      '<div class="comment-time-stamp">'+timestamp+'</div>'+
      '<div class="comment-text">'+commenttext+'</div>'+'</li>'+).insertBefore(respond);
statusdiv.html('<p class="ajax-success" >Thanks for your comment. We appreciate your response.</p>');

}

唯一有效的是“commenttext”,因为它没有 php。其他人(“头像”、“作者”和“时间戳”)都在 Firebug 中返回错误。

我尝试了一些我发现的建议,但都没有任何效果。任何帮助将不胜感激。

【问题讨论】:

  • 尝试引用&lt;?PHP ... ?&gt;
  • 未来:运行后在浏览器中查看源代码,看看JS中的语法是如何混乱的,然后修复它。
  • 这个文件是被php处理的吗?

标签: php jquery ajax wordpress


【解决方案1】:

&lt;?php ... ?&gt; 标签周围缺少引号

试试这个:

success: function(data, textStatus){
  if(data=="success"){
    var avatar = "<?php echo get_avatar( $comment, 32 ); ?>";
    var author =  "<?php the_author_meta( 'user_url'); ?>";
    var timestamp = "<?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?>";
    var commenttext =  jQuery('#comment').val();

    jQuery('<li>'+'<div class="comment-author vcard">'+avatar+
    '<div class="comment-meta">'+author+'</div>'+
    '<div class="comment-time-stamp">'+timestamp+'</div>'+
    '<div class="comment-text">'+commenttext+'</div>'+'</li>'+).insertBefore(respond);
    statusdiv.html('<p class="ajax-success" >Thanks for your comment. We appreciate your response.</p>');

  }
}

【讨论】:

  • 谢谢!非常简单的修复。
【解决方案2】:

您应该在 Javascript 中用双引号将字符串值括起来。
像这样:

var author =  "<?php the_author_meta( 'user_url'); ?>";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多