目前我有一个带有多选复选框的表单。提交后,它会发布到 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,但我不知道如何实现它。
谢谢。
最佳答案
将表单标记更改为:-
<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;
}
})
关于javascript - 提交表单并重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40896172/