【问题标题】:Echo form submit to div using AJAX使用 AJAX 将 Echo 表单提交给 div
【发布时间】:2016-07-20 11:15:48
【问题描述】:

好的,我不是 AJAX 专家,所以请听我说...

我正在尝试使用表单提交、AJAX 和 PHP 将一些数据回显到 div。我希望这是可能的。我环顾 stackoverflow 和 google 寻找答案,但找不到适合我的目的。

这是我的 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>

在 div txtHint 中,我希望输出回显。这是我的表单代码:

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

这是我的 AJAX 脚本:

$("#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;
});

这是在我的 php 文件中:

<?php echo hello; ?>

发生了一些事情,页面已刷新,但这也可能是表单中的提交操作。任何人都可以帮我解决这个问题吗?

干杯

【问题讨论】:

  • 由于您在 jquery 中返回 false,因此此页面刷新不会通过提交功能。尝试控制台一些东西并在控制台日志中检查结果
  • 谷歌网络控制台没有给出任何错误...

标签: javascript php jquery html ajax


【解决方案1】:

改变

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

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

因为,txtHint 是 ID 而不是类。

看,可能是hello 被打印出来。但是,由于页面重新加载,&lt;div id='txtHint'&gt; 无法显示ajax response。因此,您必须使用event.preventDefault(); 来停止页面刷新。

按原样使用给定的代码。我删除了data,因为您也不知道为什么要使用它,因为您从某处复制了它。

$("#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;
});

在这里,将hello 放在引号中。

reload.php

<?php echo "hello"; ?>

【讨论】:

  • 我使用了你的代码,没有任何回应。但这是网络控制台给我的:POST XHR example.com/reload.php [HTTP/1.1 200 OK 27ms]
  • hello 是否打印?您是否使用了我的整个代码?是的。它正在工作@Steggie
  • 页面刷新没有停止它仍然刷新。或者至少我最喜欢的图标变成了一个微调器,然后返回常规。你好没有打印,我从这里复制了你的代码并将其粘贴到我的文本编辑器中
  • 哈哈:D查看我更新的答案。仔细阅读。 @Steggie
  • 你好,我打印的你好!有用!谢谢娜娜!现在我必须找出如何在其中回显我的会话。但这是朝着正确方向迈出的一大步!
【解决方案2】:

使用代替

$("#ajaxform").submit(function(){

"id=" + a_href,

进入

$("#ajaxform").submit(function(e){
e.preventDefault();

{id : a_href},

【讨论】:

  • 喜欢这个? $("#ajaxform").submit(function(e){ e.preventDefault(); $.ajax({
  • 不,没有任何反应... Web 控制台也没有给出任何错误。
  • 这是什么“a_href”?
  • 我不知道它在代码中...我从其他站点复制。我试过你的代码:$("#ajaxform").submit(function(e){ e.preventDefault(); $.ajax({ type: "GET", url: "example.com/reload.php", data: {id : a_href}, success: function(data, textStatus) { $(".txtHint").html(data); }, error: function() { alert('Not OKay'); } }); return false; }); 又没有
  • 我将 reload.php 文件更改为 &lt;?php echo hello; ?&gt;,这样我就可以知道它是一个简单的回显
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 2013-05-19
相关资源
最近更新 更多