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!

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器