我无法在不离开当前页面的情况下尝试将数据发送到php页面,如果成功,则会显示从数据库加载的外部php页面的内容。我在下面列出了我目前的代码。请大家,我需要帮助,我将感激不尽。
<form class="myform">
<input type="text" name="username" placeholder="Username" class="a" id="x" required ></input>
<input type="password" name="password" placeholder="Password" class="a" id="y" required></input>
<button class="b" id="n">Submit</button>
</form>
<div id="display"></div>
&#13;
以下是我的javascript
$(document).ready(function() {
$("button#n").click(function() {
var $username = $('input#x').val();
var $password = $('input#y').val();
var $mydata = $('.myform').serialize();
if ($username != "" && $password != "") {
$.ajax({url: 'http://localhost/products/s1.php'
type: 'POST',
data: $mydata,
dataType: 'text',
success: function(data) {
$("#display").html(data);
}
});
}
});
});
});
});
&#13;
以下是我发布到`
的php页面的内容
<?php
require_once("data-var.php");
$mysqli = new mysqli($servername, $username, $password, $dbname);
if ($mysqli->connect_errno) {
echo "Failed to connect to database: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if (!($stmt = $mysqli->prepare("SELECT username, password from Administrator WHERE password = (?)"))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (isset($_POST['password'])) {
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
}
if (!$stmt->bind_param("s", $password)) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->store_result()) {
echo "Failed to store result: (" .$stmt->errno . ") " . $stmt->error;
}
if ($stmt->num_rows > 0) {
echo "welcome";
} else {
echo 0;
}
// free result
$stmt->free_result();
// close statement
$stmt->close();
// close connection
$mysqli->close();
?>v
&#13;
`
答案 0 :(得分:0)
将ajax请求数据设置为像这样的对象
data: {my data:$mydata,password:$password},
您的代码还存在许多其他问题