gpt4 book ai didi

javascript - PHP 表单保持在同一页面上

转载 作者:搜寻专家 更新时间:2023-10-31 21:53:49 25 4
gpt4 key购买 nike

您好,我正在尝试从与 php 代码位于同一页面上的表单更新数据库中的数据,而无需重定向/重新加载页面。

我试过这个教程,但没用:http://www.formget.com/form-submit-without-page-refreshing-jquery-php/

更新代码:

<?php
include "db.php";
session_start();
$value=$_POST['name'];
$query = mysqli_query($connection,"UPDATE users SET profiel='$value' WHERE username='$_SESSION['user']'");
?>

Profilecomplete.js:

$(document).ready(function() {
$("#submit").click(function() {
var name = $("#name").val();
if (name == '') {
alert("Insertion Failed Some Fields are Blank....!!");
} else {
// Returns successful data submission message when the entered information is stored in database.
$.post("config/profilecomplete.php", {
value: name
}, function(data) {
alert(data);
$('#form')[0].reset(); // To reset form fields
});
}
});
});

形式:

<form method="POST" id="form">
<div class="input-field col s6">
<input id="name" type="text" class="validate">
<label for="name">Value</label>
</div>
<button type="submit" id="submit" class="btn-flat">Update</button>
</form>

最佳答案

使用它,它已经在工作了。

index.php

<form method="post">
<input type="text" id="name">
<input type="submit" id="submit">
</form>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#submit").click(function(e) {
e.preventDefault();
var nameInput = $("#name").val();
var name = {
'name' : nameInput
}
if (nameInput == '') {
alert("Insertion Failed Some Fields are Blank....!!");
} else {
// Returns successful data submission message when the entered information is stored in database.
$.post("config/profilecomplete.php", {value: name}, function(data) {
alert(data);
//$('#form')[0].reset(); // To reset form fields
});
}
});
});
</script>

profilecomplete.php

<?php

$_POST = array_shift($_POST); // array_shift is very important, it lets you use the posted data.

echo $_POST['name'];


?>

如果你想要更简单的方法。

尝试使用$.ajax()

关于javascript - PHP 表单保持在同一页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35939366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com