在函数外部使用insert查询时它的工作原理。但是当使用功能时,它不起作用。
php代码:
<?php
function registerusers(){
$username =$_POST['username'];
$password =$_POST['password'];
$error = "Your Login Name or Password is invalid";
echo "$username";
$sql = "SELECT id FROM users WHERE username = '$username' and password = '$password'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
if($count == 1) {
?>
<script type="text/javascript">
$("div#ack").html("Successfully");
</script>
<?php
}else {
?>
<script type="text/javascript">
$("div#ack").html("Invalid user and password");
</script>
<?php
}
}
registerusers()
jquery代码:
$("button#submit").click( function() {
if( $("#username").val() == "" || $("#password").val() == "" ){
$("div#ack").html("Please enter both username and password");
}else{
$.post( $("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(data) {
$("div#ack").html(data);
});
$("#myForm").submit( function() {
return false;
});
}
});
<form id="myForm" action="functions.php" method="POST">
username: <input type="text" name="username" /><br />
password: <input type="password" name="password" /><br />
<button id="submit">Login</button>
</form>
我同时使用方法按钮类型按钮和按钮类型提交