0

好的,所以我有一个 java 脚本,当输入字段停止输入操作时,它会触发一个 ajax 调用。

//setup before functions
var field = document.getElementById("UPC");
var table=document.getElementById("ScannedItems");
var typingTimer; //timer identifier
var doneTypingInterval = 1000; //time in ms, 1 seconds

//on keyup, start the countdown
$('#UPC').keyup(function(){
  clearTimeout(typingTimer);
  typingTimer = setTimeout(doneTyping, doneTypingInterval);
});

//on keydown, clear the countdown 
$('#UPC').keydown(function(){
  clearTimeout(typingTimer);
});


function doneTyping () {
 //user is "finished typing," do something
 var upc=document.getElementById("UPC").value;
 document.getElementById("noScan").className="hidden";
 document.getElementById("checkout").className="";
 document.getElementById("void").className="";

 var dataString = 'upc='+ upc;
 //alert (dataString);return false;  
 $.ajax({  
    type: "POST",  
    url: "assets/PagePHP/pos/scan.php",  
    data: dataString,  
    success: function() {  
        var row=table.insertRow(-1);
        var cell1=row.insertCell(0);
        var cell2=row.insertCell(1);
        var cell3=row.insertCell(2);
        var cell4=row.insertCell(3);
        var cell5=row.insertCell(4);
        var cell6=row.insertCell(5);
        cell1.innerHTML =upc;
        cell2.innerHTML ="Description";
        cell3.innerHTML ="PRICE";
        cell4.innerHTML ="QTY";
        cell5.innerHTML ="TOTAL";
        cell6.innerHTML ="ACTION";
        field.value ='';
    }  
});  
return false;
}

ajax 获取输入到表单中的 UPC,并使用它来获取该特定项目的描述和价格。我需要知道如何将该信息放回 Java Script 调用中以在表中创建行。PHP 中的项目需要在下面的行中返回到 java 脚本中:(从上面的脚本中提取)

cell1.innerHTML =upc;
cell2.innerHTML ="Description";
cell3.innerHTML ="PRICE";
cell4.innerHTML ="QTY";

我的 php 简短而简单,看起来像这样:

$result = mysqli_query($con,"SELECT * FROM inventory WHERE item_upc='$_POST[upc]'");

while($row = mysqli_fetch_array($result))
{
echo $row['item_upc'];
echo $row['item_description'];
echo $row['item_price'];
}

这一切都必须在不刷新页面的情况下完成。我已经用谷歌搜索了如何做到这一点,但无法获得与我的情况相匹配的结果。

4

3 回答 3

1

未经测试,但您的代码应如下所示:

 $.ajax({  
    type: "POST",  
    url: "assets/PagePHP/pos/scan.php",  
    data: dataString,  
    dataType: 'json',
    success: function(response) {
        // Build out the rows
        var rows = '';
        for(var i=0; i<response.items; i++) {
            rows += '<tr>' +
                        '<td>' + response.item_upc + '</td>' +
                        '<td>' + response.item_description + '</td>' +
                        '<td>' + response.item_price + '</td>' +
                    '</tr>';            
        }

        // Insert rows into table
        $('#my-table').html(rows);
    }  
});

PHP:

$response = array();
while($row = mysqli_fetch_array($result)) {
    $response[] = array(
        'item_upc'          => $row['item_upc']
        'item_description'  => $row['item_description']
        'item_price'        => $row['item_price']        
    );
}

echo json_encode(array(
    'items' => $response
));
die();

2 建议:

  • 您正在检索用户类型的信息,因此您应该使用 GET 而不是 POST 方法。
  • 您已经将 jQuery 用于 AJAX,所以不妨也将它用于 DOM 操作,这也是它擅长的。
于 2013-10-01T02:06:53.913 回答
1

我会执行您的 AJAX 查询并让您的 PHP 脚本返回一个带有您的数据的 JSON 字符串,您可以在成功函数中对其进行解码和处理:

<?php
    header('Content-type: application/json');
    $con = ...; //establish_connection
    $result = mysqli_query($con,"SELECT * FROM inventory WHERE item_upc='$_POST[upc]'");

    $json = array();
    $json['success'] = false;

    while($row = mysqli_fetch_array($result))
    {
        $json['success'] = true;
        $json['item_upc'] = $row['item_upc'];
        $json['item_description'] = $row['item_description'];
        $json['item_price'] = $row['item_price'];
    }

    echo json_encode($json);
?>

对于您的 AJAX 使用:

$.ajax({  
    type: "POST",  
    url: "assets/PagePHP/pos/scan.php",  
    data: dataString,
    success:function(data){
        if(data.success==true){
            // handle data array
            var row=table.insertRow(-1);
            var cell1=row.insertCell(0);
            var cell2=row.insertCell(1);
            var cell3=row.insertCell(2);
            var cell4=row.insertCell(3);
            var cell5=row.insertCell(4);
            var cell6=row.insertCell(5);
            cell1.innerHTML =upc;
            cell2.innerHTML = data.item_description;
            cell3.innerHTML = data.item_price;
            cell4.innerHTML ="QTY"; // handle these as you like
            cell5.innerHTML ="TOTAL";
            cell6.innerHTML ="ACTION";
            field.value ='';
        }
        else {
            // nothing returned - error
        }
    }
});
  • 似乎我们中的一些人达到了妙语,但你得到了你需要做的事情的要点哈哈。
于 2013-10-01T02:08:50.840 回答
0

JSON我建议您在 php 代码中将SQL 结果作为字符串返回

$result = mysqli_query($con,"SELECT * FROM inventory WHERE item_upc='$_POST[upc]'");
$return_array = array();
while($row = mysqli_fetch_array($result))
{
    $return_array[] = $row;
}

echo json_encode($return_array);

在您的 js 代码中:

$.ajax({  
    type: "POST",  
    url: "assets/PagePHP/pos/scan.php",  
    data: dataString,  
    success: function(data) {  //add parameter to receive response

        var result = $.parseJSON(data);//parse the return result 
                                       //to a native js an object
        //result should be an array so you can iterate it to get information you need

        var row=table.insertRow(-1);
        var cell1=row.insertCell(0);
        var cell2=row.insertCell(1);
        var cell3=row.insertCell(2);
        var cell4=row.insertCell(3);
        var cell5=row.insertCell(4);
        var cell6=row.insertCell(5);
        cell1.innerHTML =upc;
        cell2.innerHTML ="Description";
        cell3.innerHTML ="PRICE";
        cell4.innerHTML ="QTY";
        cell5.innerHTML ="TOTAL";
        cell6.innerHTML ="ACTION";
        field.value ='';
    }  
});  
于 2013-10-01T02:04:06.233 回答