我试图运行一个简单的场景:在我的页面上显示了一个表单,->,-> PHP脚本从数据库读取并编码到数据。
MySQL上的字段是:Name和Password
步骤1-我的表单- Search.php
<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="Query.js"></script>
<title></title>
</head>
<body>
<form method="post" id="formoid" action="">
<input type="text" name="enterpass" placeholder="Enter Password">
<input type="submit" name="subbpass">
</form>
<input type="text" id="showname"><br/>
<input type="text" id="showpassword">
步骤2-我的jquery文件- Query.js
$(document).ready(function(){
$("#formoid").submit(function(){
alert("form submitted") // this alert goes through
var passid = $("#enterpass").val();
$.ajax({
url: "ModelQuery.php",
method: "POST",
dataType: "JSON",
data: {args: passid},
success: function(data)
{
console.log('ajax successfully sent'); // this alert isn't working
$("#showname").text(data.Name); // data isn't showing here
$("#showpassword").text(data.Password);
console.log(data); // or here ...
}
});
});
});最后,我的php -- ModelQuery.php -- --我省略了一些代码行,但是这个脚本是从数据库中读取的普通脚本,过去一直为我工作。
<?php
if(isset($_POST['args'])) {
$arg = $_POST['args']
$CON = mysqli_connect('127.0.0.1','root','','testdb');
// ....
$QUERY = "SELECT * FROM testtable where Password = '$arg'";
// ...
while($row = mysqli_fetch_array($RESULT))
{
$jsonresults["Name"] = $row['Name'];
$jsonresults["Password"] = $row['Password'];
}
echo json_encode($jsonresults);
}表单提交后,Jquery脚本中的警报确实经过,但是ajax本身没有显示任何东西,无论是在控制台上还是在我的两个文本框上。
我在这里做错什么了?
非常感谢!
发布于 2019-01-06 01:18:33
我发现了这个问题,其中包括三个不同的问题: 1)我的输入文本框<input type="text" name="enterpass" placeholder="Enter Password">没有id属性,只有名称2),正如@RamRaider建议的那样,我将其更改为按钮,并给出了id。
3) php中的一行在其末尾没有;。
谢谢你!
https://stackoverflow.com/questions/54057530
复制相似问题