duanhan1392 2016-06-25 14:19
浏览 63

Ajax POST具有多个PHP目标的多个表单

I have a page with two forms and each form uses a different PHP page for its post. I can only find examples / documentation on using multiple forms with the same PHP post script. I am struggling to get this to work, can any help ?

This is the JQUERY, that works if i use one form, I've tried to add an ID tag but it didn't seem to work:

         $(function () {

           $('form').on('submit', function (e) {

var form = $(this);
e.preventDefault();

$.ajax({
 type: 'post',
 url: form.attr('action'),
 data: form.serialize(),
 success: function () {
   alert('Suppiler Amended!');
 }
});

           });

         });
         </script>
         </head>
         <body>
         <?php 
         echo "<div class='table1'>";
         echo "<div class='datagrid'>";
          echo "<table id='tblData2'><thead><tr><th>Notes</th><th>Updated By</th><th></th></thead></tr>";
         while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
         ?>
         <tbody><tr>
         <td><FONT COLOR='#000'><b><?php echo "".$row["notes"].""?></td>
         <td><FONT COLOR='#000'><b><?php echo "".$row["enteredby"].""?></td>
         <td><FONT COLOR='#000'><b><a href="edit.php">

         <form name="edit" action="script1.php" method="post">
            <input type="hidden" name="notes" value="<?php echo"".$row["notes"]."";?>">
            <input type="hidden" name="noteid" value="<?php echo"".$row["noteid"]."";?>">
         <input type="submit" value="EDIT">
         </form>
         </a></td>
         </tr></tbody>
         <?php
         $companyid = "".$row['id']."";
         }
         ?>
         </table>
         </div>
         <br>
         <form name="notes" action="add-note.php" method="post">
           ADD NEW NOTE:<br>
           <input type="text" name="newnote" style="height:120px;width:200px;"><br>
            <input type="hidden" name="companyid" value="<?php echo"".$companyid."";?>">
             <input type="hidden" name="user" value="<?php echo"".$user."";?>">
             <br>
         <input type="submit" value="ADD NOTE">
         </form>
  • 写回答

2条回答 默认 最新

  • doujingtang6580 2016-06-25 14:26
    关注

    What you need to do is to simply get the action attribute dynamically. You can do that easily with form.attr('action'); inside the function. See bellow -

    $(function () {
    
      $('form').on('submit', function (e) {
    
        var form = $(this);
        e.preventDefault();
    
        $.ajax({
         type: 'post',
         url: form.attr('action'),
         data: form.serialize(),
         success: function () {
           alert('Note has been edited!');
         }
        });
    
      });
    
    });
    

    Update:

    <html>
        <head>
            <script src="https://code.jquery.com/jquery-1.12.4.min.js"   integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="   crossorigin="anonymous"></script>
        </head>
        <body>
            <form name="edit" action="script1.php" method="post">
                <input type="hidden" name="notes" value="1">
                <input type="hidden" name="noteid" value="2">
                <input type="submit" value="s1">
             </form>
    
             <form name="edit" action="script2.php" method="post">
                <input type="hidden" name="notes" value="1">
                <input type="hidden" name="noteid" value="2">
                <input type="submit" value="s2">
             </form>
    
             <script type="text/javascript">
                 $(function () {
    
                      $('form').on('submit', function (e) {
    
                        var form = $(this);
                        e.preventDefault();
    
                        $.ajax({
                         type: 'post',
                         url: form.attr('action'),
                         data: form.serialize(),
                         success: function () {
                           alert('Note has been edited!');
                         }
                        });
    
                      });
    
                    });
             </script>
        </body>
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?