【问题标题】:Submit Form with Reloading提交表单并重新加载
【发布时间】:2016-11-30 19:02:07
【问题描述】:

目前我有一个带有多选复选框的表单。提交后,它会发布到具有类似于以下数组的 PHP 脚本: &id%5B%5D=3&id%5B%5D=7

HTML:

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

php 脚本循环遍历数组,并为选定的每一行执行 SQL 更新。然后脚本使用 header() 函数重定向回页面。

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');
?>

我的问题是关于页面刷新。有什么方法可以提交表单但不刷新页面?我在考虑 ajax,但我不知道如何实现它。

谢谢。

【问题讨论】:

    标签: javascript php jquery ajax forms


    【解决方案1】:

    将您的表单标签更改为:-

    <form id="frm-example">
    

    并将其添加到脚本标签中:-

    $('#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;
            }
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 2021-07-01
      • 2017-02-03
      • 2017-06-22
      相关资源
      最近更新 更多