我正在尝试更新用户名,然后在使用JQuery AJAX成功或不成功时显示消息。
这是changename.php
中的表单:
<form class="form-horizontal" role="form">
<div class="form-group-sm">
<div class="col-lg-offset-1 col-lg-4">
<input type="text" id="inputName" class="form-control input-lg" placeholder="New name" required="">
<button class="btn btn-lg btn-primary btn-block" input-type="submit" id="submitName" style="margin-top:10px;">Update name</button>
<div id="resultDiv"></div>
</div>
</div>
</form>
这是changename.php
<?php
if(isset($_POST['name']))
{
echo 'is set';
$postedname = $_POST['name'];
$safename = mysqli_real_escape_string($con, $postedname);
$checkname = "SELECT * from accounts where name='$safename' LIMIT 1";
$query = mysqli_query($con, $checkname);
$row = mysqli_num_rows($query);
if($row) {
echo 'Name is already taken!';
} else {
$changename = "UPDATE accounts SET name='$safename' WHERE Name='$_SESSION[username]'";
$query = mysqli_query($con, $changename);
if($query)
{
echo 'Name is changed!';
$_SESSION['username'] = $safename;
}
}
}
else
{
echo 'is not set';
}
?>
JQUERY:
$(function(){
$("form").submit(function() {
var name = $('#inputName').val();
$.post( "changename.php",
{
name: name;
}
function(result)
{
$("#resultDiv").text(result);
});
});
});
我环顾四周,似乎无法找到解决方案。即使我在文本框中输入内容,按下按钮,它也会在页面顶部显示“未设置”。我看了很多视频和来源,都使用了一些不同的方法等。抱歉,如果这看起来很无趣。