doutan8601 2013-07-25 06:09
浏览 37
已采纳

从AJAX Success中提交AJAX表单

I need to perform another AJAX Form Post from within the first forms success function.

http://i.imgur.com/rZ2gx6G.jpg

Example, this does 2 AJAX requests. Search Movie => Pick Movie Wanted => View Specific Movie Details

I am able to load the results into a div <div id="results"></div> just fine but once I select a movie title it isnt performing another AJAX Request, the request goes to the main window.

Here is the initial search page that handles the results.

<script type="text/javascript">
   $(document).ready(function(){
       $("#searchtitle").submit(function() {
           var id = $(this).children('input[name="thetitle"]').attr('value');
           $.ajax({
               type: "POST",
               url: "s.php",
               data: $('#searchtitle').serialize(), 
               cache: false,
               success: function(data){
            $('#status').html(data);
               }
           });
          return false;
      });
   });

</script>

<form id="searchtitle">
   <input type="text" name="thetitle" />
   <input type="submit" name="submit" class="button expand postfix" value="Search" />
</form>
<div id="status"></div>

s.php which returns results within #results

<?php
   if(empty($_POST['thetitle'])) {
   ?>
<div class="alert-box error">
   <div class="alert-error"></div>
   Error: Nothing Found
</div>
<?php
   }
   if(!empty($_POST['thetitle'])) {
      $myid = strtoupper($_POST['thetitle']);

      $searchReults = $tmdb_V3->searchMovie($myid,'en');
      ?>
<?php
   foreach($searchReults['results'] as $result) {
   ?>
<form class="sform">
   <input type="hidden" name="mid" value="<?php echo $result['id']); ?>" />
   <h5><?php echo $result['title']; ?></h5>
   <span class="mreleased">Year: <?php echo $result['year']; ?></span>
   <input type="submit" class="button" value="Select">
</form>
<?php
   }
   }
   ?>

This is the code that will post the results from s.php

<script type="text/javascript">
$(".sform").submit(function () {
    $.ajax({
        type: 'POST',
        url: 'sx.php',
        data: $(this).closest("form").serialize();
        success: function (response) {
            $('#status').load(response);
            $('#status').find('script').each(function (i) {
                eval($(this).text());
            });

        }
    });

    return false;
}  
</script>

I have tried putting this within s.php, within the bottom of the initial search page, in the head of the initial page and no luck, it submits fine just not the sx.php where it should.

  • 写回答

1条回答 默认 最新

  • dppn67180 2013-07-25 07:35
    关注

    In s.php the statement:

    <input type="hidden" name="mid" value="<?php echo $result['id']); ?>" />
    

    Should be:

    <input type="hidden" name="mid" value="<?php echo $result['id']; ?>" /> //remove extra bracket
    

    In your javascript code in s.php there are some typos:

    data: $(this).closest("form").serialize(); // here should be comma not semicolon
    

    After return false you should close the script properly } should be });.

    And since you are trying to submit the dynamic content $(".sform").submit(function () will not work. You should use on for dynamic contents. So the correct script would be:

    <script type="text/javascript">
     $(document).on('submit', '.sform', function () {
        $.ajax({
            type: 'POST',
            url: 'sx.php',
            data: $(this).closest("form").serialize(),
            success: function (response) {
                $('#status').load(response);
                $('#status').find('script').each(function (i) {
                    eval($(this).text());
                });
    
            }
        });
    
        return false;
     });  
    </script>
    

    I have checked and verified in my localhost (with a simple setup). It is making both ajax request. Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?