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

时间:2015-05-24 13:47:02

标签: javascript php post

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

用户扫描条形码(studentNr) studentnr - >变量将被发布到php文件。 在php文件中:查询与变量进行比较 查询结果 - >回到函数的参数(数据) 打印结果。 听起来很简单,但我该怎么做呢?

Index.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"]);

1 个答案:

答案 0 :(得分:1)

在您的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语句然后绑定它们,因此该行应该是(类似):

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