我有一个表单,负责在数据库中插入值。它工作得很好,但每当提交值时,页面就会刷新,我试图从here获得帮助,但在我的情况下不起作用。谁能告诉我如何在不刷新页面的情况下提交这些值。
我现在拥有的代码是
<?
if($_POST['submit'])
{
$sender_id = mysqli_real_escape_string($con, $_POST['sender_id']);
$receiver_id = mysqli_real_escape_string($con, $_POST['receiver_id']);
$message = mysqli_real_escape_string($con, $_POST['message']);
// execute insert query here
}
?>
<form class="form-horizontal" enctype="multipart/form-data" role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<input type="hidden" name="sender_id" value="<? echo $recruiterid; ?>">
<input type="hidden" name="receiver_id" value="<? echo $recv_id; ?>">
<textarea name="message" class="form-control" ></textarea>
<button type="submit" value="submit" name="submit" class="btn btn-greensea btn-ef btn-ef-7 btn-ef-7b b-0 br-2"><i class="fa fa-envelope"></i> Send Message</button>
</form>发布于 2015-12-04 20:36:04
您需要使用AJAX在不刷新的情况下提交表单。在您的JavaScript中添加以下内容:
$("form").submit(function (e) {
e.preventDefault();
$.post($(this).attr("action"), $(this).serialize(), function () {
alert("Submitted!");
});
});https://stackoverflow.com/questions/34088541
复制相似问题