doufu6504 2014-04-19 10:36
浏览 124
已采纳

多个表单,多个提交按钮,1个使用ajax的处理表单

On my page I have a total of 18 forms which are generated from a database, each form us a category and allows the user to update a price in any of the categories. All forms have the same id, I was assuming that then I would be able to process all of them with a click, however it will only update form 1 if submit button 1 is clicked, any other button just reloads the page, is there a way of doing this with out giving every form a numerical id then a massive if statement on my submit function?

My Code for the forms:

<?php

$shopid = $_GET['user'];

$db = new PDO('mysql:host=localhost;dbname=servershop', 'user', '****');
$stmt = $db->prepare("SELECT DISTINCT group_ID FROM `items` Order By group_id");
$stmt->execute();
$gpID = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach($gpID as $igroup)
{
    $groupID = $igroup['group_ID'];

    $db = new PDO('mysql:host=localhost;dbname=servershop', 'shop', '1butter22');
    $stmt = $db->prepare("SELECT name FROM groups WHERE id = '$groupID'");
    $stmt->execute();
    $groupname = $stmt->fetchColumn();
    ?>
    <details>
    <summary><?php echo $groupname;?></summary>
    <div class="ccontainer">
    <div class="ccontent">
    <form id="savechanges">
    <input type="hidden" value="prices" id="tag" name="tag">
    <input type="hidden" value="<?php echo $shopid;?>" id="shopid" name="shopid">
    <input type="submit" value="Save" id="submit">

    <?php 


        $stmt = $db->prepare("SELECT * FROM `items` WHERE group_id = '$groupID'");
        $stmt->execute();
        $arr = $stmt->fetchAll(PDO::FETCH_ASSOC);
        echo '<table class="itemTable">';
        echo '<tr><td>Name & Quantity</td><td>Image</td><td>Price</td></tr>';
        foreach ($arr as $item)
        {

            echo '<tr><td>' . $item['name'] . '('. $item['quantity'] .')</td>';
            echo '<td><img src="images/items/' . $item['image'] . '" /></td>';
            echo '<input type="hidden" value=' . $item['id'] .' name="itemid[]">';


                $stmt = $db->prepare("SELECT price FROM prices WHERE shop_id = :shop AND item_id = :item");
                $stmt->bindParam(':shop', $shopid);
                $stmt->bindParam(':item', $item['id']);
                $stmt->execute();
                $barr = $stmt->fetchColumn();
                if(!empty($barr))
                    {
                        echo '<td><input type="text" name="price[]"  value="'. $barr .'" /></td></tr>';
                    }
                else
                    {
                        echo '<td><input type="text" name="price[]" value="0.0" /></td></tr>';
                    }
        }


    ?>
</table>
</form>
</summary>
</details>
<?php } ?>

and my processing code:(taken from a larger script)

 document.getElementById('savechanges').onsubmit=function (e)
            {
                e.preventDefault();
                var f = $("#savechanges");
                request = $.ajax(
                    {
                        url: "processing/db.php",
                        type: "POST",
                        data: f.serialize(),
                        cache: false
                    }
                                );

EDIT:

Using the idea in the accepted answer, the trick was the just create one form and have all the inputs serialized by the class of form, in theory, this should now work with id as well as the class, execution time to process all 120 odd items has risen from an average of 60ms to 105ms, not a unacceptable increase!

  • 写回答

2条回答 默认 最新

  • dongyong9224 2014-04-19 10:43
    关注

    id attribute is meant to be unique. Try giving a common class to all forms instead of an id and then on submit serialize all these forms using the common class.

    If you run $('form').serialize() on a page with multiple forms, it will correctly serialize all the forms into one string.

    To include forms with some perticular class, use $('.myforms').serialize();

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

报告相同问题?

悬赏问题

  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档