doudihuang7642 2016-11-30 19:02
浏览 73
已采纳

提交表格并重新加载

Currently I have a form with multi-select checkboxes. When submitted it posts to a PHP script with an array similar to: &id%5B%5D=3&id%5B%5D=7

HTML:

<form id="frm-example" action="vmwaressl_admin_process.php" method="POST">

The php script loops through the array and does a SQL update for each row that is selected. The script then uses the header() function to redirect back to the page.

PHP:

<?php
foreach ($_POST['id'] as $key => $idValue) {
    $host_id_sql = "query";
    $stmt        = sqlsrv_query($conn, $host_id_sql);
    if ($stmt === false) {
        die(print_r(sqlsrv_errors(), true));
    }
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    $host_id = $row['HOST_ID'];

    }
    $host_update_sql = "update query";
    $stmt            = sqlsrv_query($conn, $host_update_sql);
    if ($stmt === false) {
        die(print_r(sqlsrv_errors(), true));
    }
    $host_id_sql = null;
    $stmt        = null;
}

header('Location: home_page.php');
?>

My question is about the page refresh. Is there any way to submit the form but not refresh the page? I'm thinking ajax but I'm not sure how to accomplish it.

Thanks.

  • 写回答

1条回答 默认 最新

  • dqjcb132285 2016-11-30 19:09
    关注

    Change your form tag to just :-

    <form id="frm-example">
    

    and add this in a script tag :-

    $('#frm-example').on('submit', function (e) {
            if (!e.isDefaultPrevented()) {
                var url = "vmwaressl_admin_process.php";
    
                $.ajax({
                    type: "POST",
                    url: url,
                    data: $(this).serialize(),
                    success: function (data)
                    {
                        //Whatever You want to do on success
                    }
                });
                return false;
            }
        })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来