gpt4 book ai didi

javascript变量到php文件并使用ajax返回

转载 作者:行者123 更新时间:2023-11-28 00:16:29 24 4
gpt4 key购买 nike

我正在制作一个在线条形码扫描仪应用程序,并且在将 javascript 变量发送到 php 进行查询并将其与数据一起返回到我的 html 文件中时遇到一些问题。

用户扫描条形码(studentNr)Studentnr --> 变量将被发布到 php 文件中。在 php 文件中:查询与变量进行比较查询结果 --> 返回函数参数(数据)打印结果。听起来很简单,但我该如何正确地做到这一点?

索引.html:

 <head>

<meta charset="utf-8" />
<!-- Set the viewport width to device width for mobile -->
<meta name="viewport" content="width=device-width" />
<script src="javascripts/jquery.js"></script>
</head>

<body>
<div>

<input type="text" name="barcode" id="barcode"> // BARCODE SCAN
<input type="text" id="student" placeholder="studentenNaam" size="30"> //NAME FROM DB
<!-- <input type="text" id="barcode" placeholder="Waiting for barcode scan..." size="40">
<input type="text" id="student" placeholder="studentenNaam" size="30">-->

</div>
// just the script for scanning the barcode :

<script>
var barcodeParsed = null;
var studentenNr = null;
$(document).ready(function() {
var pressed = false;
var chars = [];
$(window).keypress(function(e) {
if (e.which >= 48 && e.which <= 57) {
chars.push(String.fromCharCode(e.which));
}
console.log(e.which + ":" + chars.join("|"));
if (pressed == false) {
setTimeout(function(){
if (chars.length >= 10) {
var barcode = chars.join("");
console.log("Barcode Scanned: " + barcode);
barcodeParsed = barcode;
var barcodeParsing = barcodeParsed.substring(5,11);
$("#barcode").val("s" + barcodeParsing);
studentenNr = "s" + barcodeParsing;

}
chars = [];
pressed = false;
},500);
}
pressed = true;
});
});

$("#barcode").keypress(function(e){
if ( e.which === 13 ) {
console.log("Prevent form submit.");

$.post('testdb.php', {'studnr' :studentenNr}, function(data){ //POST JS VAR TO PHP FILE
$('#student').html(data); //RECEIVE DATA FROM DB INTO ID ELEMENT
});
e.preventDefault();
}
});


</script>

</body>

在我的 testdb.php 文件中,它看起来像这样:

<?PHP

$studnr = htmlspecialchars($_POST['studnr']);
if(isset($studnr)){

$user_name = "root";
$password = "";
$database = "Student";
$hostname = "127.0.0.1";

$db_handle = mysql_connect($hostname, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = "SELECT * FROM student where studentenNr= '$studnr'";
$result = mysql_query($SQL);

while ( $db_field = mysql_fetch_assoc($result) ) {
/* Here needs the data to fetch*/
print $db_field['naam'] . "<BR>";
print $db_field['studentenNr'] . "<BR>";
print $db_field['voornaam'] . "<BR>";
}






mysql_close($db_handle);

}
else {

print "Database NOT Found ";
mysql_close($db_handle);

}

}

问题是我无法将 html 文件中的任何数据接收到 php 文件中,而且我不知道如何将接收到的数据放回我的 html 文件中。在 php 文件中..如何将其写回 html ?我需要使用 Ajax,Jquery,..还有其他解决方案吗?

谢谢你帮助我!

更新

在 if(db_found) 括号之间添加此内容时,我收到内部服务器错误 500。

 $sql = $db->prepare("select*from student where studentenNr= '$studnr' ");
$sql->execute($arraytest = array(':studnr' => studentenNr,naam );)


$sql->execute(array(":studentenNr" => $studnr));

$data = $sql->fetchAll();

foreach($item as $data){
print($item["naam"]);
log($item["naam"]);

最佳答案

在您的 JavaScript 中,您发送 studnr 参数:

$.post('testdb.php', {'studnr' :studentenNr}, function(data){

在您的 php 脚本中,您尝试从 studentenNr 参数获取值:

$studnr = htmlspecialchars($_POST['studentenNr']);

您需要将脚本更改为

$studnr = htmlspecialchars($_POST['studnr']);
<小时/>

编辑:

由于这一行,您收到内部错误:

$sql = $db->prepare("select*from student where studentenNr= '$studnr' ");

您需要准备带有参数的 SQL 语句,然后绑定(bind)它们,因此该行应该是(类似于):

$sql = $db->prepare("select * from student where studentenNr= :studnr");
$sql->execute(array(':studnr' => $studnr ));

关于javascript变量到php文件并使用ajax返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30424235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com