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;
            }
        })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题