PHP如何返回获取到的数据到前台html的表单中?
页面A有一个input。网页B是php。已经连接数据库并获得了数据,如何把这个获得的数据,放到页面A的input的value中呢。。
页面A内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表单所在页面</title>
<style>
#msg{color:red;}
</style>
</head>
<body>
<form>
<p>用户名:<input type="text" id="user"></p>
<p>密码:<input type="test" id="psd"></p>
<p><input type="button" value="提交" id="submit"/></p>
<p id="msg"></p>
</form>
<script src="jquery.min.js"></script>
<script>
$("#submit").click(function(){
var data={
'user':$("#user").val(),
'psd':$("#psd").val(),
};
$.post("indextest.php",data,function(d){
$("#msg").text(d);
})
})
</script>
</body>
</html>
页面B内容:
<?php
try{
$db = new PDO("sqlsrv:Server=IP;Database=test","webtest","****");
}catch(PDOException $e){
echo "数据库连接失败".$e->getMessage();
}
$sql = "SELECT * FROM LOGIN where name = '刘某'";
$result = $db->query($sql);
if($result->rowCount()!=0){ //用户存在
while($row = $result->fetch()) {
echo $row['color'] . "<br>";
$r = $row['color'] . "<br>";
}
}
?>