【发布时间】:2017-04-09 21:17:30
【问题描述】:
我在服务器上有一个简单的数据库(用于测试)。 这个 PHP 文件在服务器上,当我打开 URL 时可以工作。 (http://**.com/search.php?id=abc) Echo 回馈“30”
<?php
$pdo = new PDO('mysql:host=*com; dbname=*test1', '*', '*');
$idV = $_GET['id'];
$statement = $pdo->prepare("SELECT position FROM idtabelle WHERE idnumber = :idV");
$statement->bindParam(':idV', $idV);
$statement->execute();
while ($row = $statement->fetch(PDO::FETCH_ASSOC))
{ $posV = $row['position']; };
echo $posV;
?>
HTML 仅用于测试
<input type="text" id="txt1">
<button type="button" class="btn btn-info" id= "bt1">Info Button</button>
<div id= "div1"> </div>
我希望当我在文本字段中输入代码并按下按钮时,来自 PHP 的 Echo 会显示在 Div 中。 我知道我应该使用 Ajax GET,但我尝试了很多东西,但没有任何效果。 你能帮我吗?
编辑:最后一次尝试:https://jsfiddle.net/qz0yn5fx/
<input type="text" id="txt1">
<button type="button" class="btn btn-info" id="bt1">Info Button</button>
<div id="div1">Here </div>
<script>
$(document).ready(function() {
$("#bt1").click(function() {
$.ajax({
//create an ajax request to load_page.php
type: "GET",
url: "http://**.com/search.php?id=a10 ",
dataType: "html", //expect html to be returned
success: function(response){
$("#div1").html(response);
alert(response);
}
});
});
});
</script>
最好不要看下一个小提琴,我只是复制了所有第一次尝试:
【问题讨论】:
-
你需要命名你的输入并使用一个表单,如果你想使用纯 php,你需要一个 href。
-
如果您能展示您的尝试,我们可以提供帮助。
-
相关:Using Jquery Ajax to retrieve data from Mysql。尽管使用您已经拥有的 PDO 准备语句,但后端比您在该链接中找到的
mysql_*()函数更安全。 -
我发布了我尝试的小提琴。
-
在处理数据库时,您应该先清理输入,然后再分配给 php 中的变量。
标签: javascript php html mysql ajax