douhe4608 2016-07-20 11:15
浏览 21
已采纳

回声表单使用AJAX提交给div

Ok, I'm not a AJAX specialist so please hear me out...

I'm trying to echo some data to a div using a form submit, AJAX and PHP. I hope this is possible. I've looked around stackoverflow and google for an answer but could not find one for my purpose.

This is my HTML:

<article id="search-results">
    <div class="container">
        <div class="row">
            <div class="col-sm-3">
                <?php echo do_shortcode('[searchandfilter id="15"]');?>
            </div>
            <div class="col-sm-7">
                <?php echo do_shortcode('[searchandfilter id="15" show="results"]'); ?>
                <?php get_template_part('pagination'); ?>
            </div>
            <div class="col-sm-2">
                <div id="txtHint">
                    <h4>Saved Items</h4>
                    <!-- ECHO OUTPUT -->
                </div>    
            </div>
        </div>
    </div>
</article>

In the div txtHint is where I want to have the output echoed. This is my form code:

<form id="ajaxform" class="content-items">
  <div class="row">
    <div class="col-sm-3">
       <div class="thumbnail-content-items">
         <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
           <?php the_post_thumbnail(array(120,120)); // Declare pixel size you need inside the array ?>
           <?php endif; ?>
       </div>
     </div>
   <div class="col-sm-9">
     <h2><?php the_title(); ?></h2>
       <?php html5wp_excerpt('html5wp_index'); ?>

       <div class="text-right">
        <button type="submit" class="btn btn-primary text-right" data-toggle="modal" data-target=".post-<?php the_ID(); ?>" data-attribute="<?php the_title(); ?>" data-whatever="<?php the_title(); ?>">Save this item <span class="glyphicon glyphicon-heart" aria-hidden="true"></span></button>
      </div>
    </div>
  </div>

And here is my AJAX script:

$("#ajaxform").submit(function(){
   $.ajax({
      type: "POST",
      url: "example.com/reload.php",
      data: "id=" + a_href,
       success: function(data, textStatus) {
        $(".txtHint").html(data);    
        },
        error: function() {
            alert('Not OKay');
        }
   });

   return false;
});

This is in my php file:

<?php echo hello; ?>

There is something happening, the page is refreshed but this could also be the submit action from the form. Could any one help me get this to work?

Cheers

  • 写回答

2条回答 默认 最新

  • dongyong6428 2016-07-20 11:41
    关注

    Change

    $(".txtHint").html(data);
    

    To

    $("#txtHint").html(data);    
    

    As, txtHint is ID not Class.

    See, may be hello get printed. But, due to page-reload, <div id='txtHint'> is not able to show ajax response. So, you have to use event.preventDefault(); for stopping page refresh.

    Use the given code as it is. I removed data as you also don't know why it is used as you copied it from somewhere.

    $("#ajaxform").submit(function(event){
       event.preventDefault();
       $.ajax({
          type: "POST",
          url: "example.com/reload.php",
          success: function(data) {
            $("#txtHint").html(data);    
          },
          error: function() {
            alert('Not OKay');
          }
       });
       return false;
    });
    

    And here, put hello in quotes.

    reload.php

    <?php echo "hello"; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突