我想继续存储和显示我从mysql获得的结果

时间:2016-02-22 19:44:29

标签: javascript php jquery mysql arrays

我不是php的专家,只是一个新手,所以不知道该怎么做。我想做的是继续保存我从文本框This is the textbox and shirt,color and price is the result I am getting from the table得到的结果并继续显示它我的页面。附加了文本框的图片。我想继续存储从表中获得的新结果,并继续显示旧结果。我希望它清楚我想要做什么。我的代码有三个文件,主要文件products.php有以下代码

<!DOCTYPE html>
<html>
<head>
    <title>Products</title>
    <p> Scan the Barcode of the Product </p>
    <link href="mystyle.css" rel="stylesheet">
</head>
<body>

        <input type="text" name="enter" required id="item" style="text-align:centre" placeholder=" Barcode ID" autofocus />

        <div id="item-data"></div>

        <script src="js/jquery-2.2.0.min.js"></script> <!--jquery link-->
        <script src="js/global.js"></script> <!--linking event file-->

</body>
</html>

而global.js的代码是

$(function(){

//press enter on text area..

$('#item').keypress(function (event) {
var key = event.which;
if(key == 13)  // the enter key code
{

var item = $('input#item').val();    // retreiving the value from the item
    if ($.trim(item) != ''){            //send this to php file but if its     empty or spaces(trim) are there dont send it//
        $.post('ajax/name.php',{id:item}, function(data){   //using post method sending to the father(name.php), sending the data through the file item
            $('div#item-data').append(""+data+"</br>"); // grabing the data and displaying it

        });

    }

$('#item').val('');
 }
 });

});

和第三个文件我的name.php文件由以下代码组成

<?php

require '../db/connect.php';

$id= $_POST['id']; // your post variable
$sql = "SELECT BarcodeID, shirts, price FROM clothes WHERE  BarcodeID=".mysqli_real_escape_string($con,$id);

$result = mysqli_query($con,$sql) or die(mysqli_error($con));

if(mysqli_num_rows($result)>0)
{
    while($row = $result->fetch_assoc())
    {
        echo " " . $row["BarcodeID"]. " shirt color:  " . $row["shirts"]. "  price: " . $row["price"];
    }
}
else
{
        echo "ID not found, Please Scan again";
}
mysqli_close($con);

?>

1 个答案:

答案 0 :(得分:0)

通过在javascript中使用append函数解决了这个问题。签入global.js文件。