Hi,
Good day!
I encountered problem on my ajax code. I think it my responsetext has a whitespace so that it falls on my else statement.
here is my ajax code:
//----barcode---//
function get_data(){
if (window.event.keyCode==13 || window.event.keyCode==10) {
var barcode_wms = document.getElementById("barcode").value;
// var divid = 'display_data';
var url = "getbarcode_data.php";
var str = "mc=" + barcode_wms;
var xmlHttp = AJAX();
// alert(xmlHttp);
xmlHttp.onreadystatechange=function(){
//alert("test");
/*
if(xmlHttp.readyState > 0 && xmlHttp.readyState < 4){
// document.getElementById(divid).innerHTML=loadingmessage;
}
*/
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
//alert('test');
var jsonart = xmlHttp.responseText;
//jsonart = jsonart.Itrim()
alert(jsonart);
if(jsonart==="Invalid")
{
alert("Invalid Barcode");
//notify("Invalid Barcode");
//refresh(1500);
}
else
{
alert("Correct Barcode");
/*
var strsplit = jsonart.split("^");
document.getElementById("po_number").value = strsplit[0];
document.getElementById("unique_id").value = strsplit[1];
document.getElementById("kind").value = strsplit[2];
document.getElementById("material_code").value = strsplit[3];
document.getElementById("description").value = strsplit[4] ;
document.getElementById("uom").value = strsplit[5] ;
document.getElementById("supplier").value = strsplit[6] ;
document.getElementById("qty").value = strsplit[7] ;
}
*/
}
//}
}
}
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", str.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(str);
// }
}
}
get_barcode_data.php
<?php
include("includes/connection.php");
//if($_POST["mc"])
// {
$barcode = $_POST['mc'];
$po_number = substr($barcode, 0, 5);
$item_code = substr($barcode, 5, 6);
$supplier = substr($barcode, 11, 3);
$uom = substr($barcode, 14, 2);
$unique_id = substr($barcode, 16, 6);
$qty = substr($barcode, 22);
$query = "SELECT mat_id, supplier_id, uom_id ";
$query .= "FROM check_info ";
$query .= "WHERE mat_id = '$item_code' AND supplier_id = '$supplier' AND uom_id = '$uom'";
$result = mysql_query($query);
$cnt = mysql_num_rows($result);
if($cnt==0)
{
echo("Invalid");
}
else
{
echo("Correct");
}
exit;
and the index.php
<?php
include('includes/connection.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/wms.css">
<script type="text/javascript" src="javascript/wms.js"> </script>
<script type="text/javascript" src="javascript/prompt.js"> </script>
<script type="text/javascript" src="javascript/notification.js"> </script>
<link rel="stylesheet" type="text/css" href="css/prompt.css">
<link rel="stylesheet" type="text/css" href="css/notify.css">
<title>Warehouse Barcoding</title>
</head>
<body>
<form name="wms" action="" method="post">
<?php
echo "<div>";
echo "<label> Material Barcode: </label>";
echo "<input type='password' name='barcode' id='barcode' value='' onkeypress='get_data();' size='40'>";
echo "</div>";
//$barcode = '10001CHE00102401100200250000.50';
/*
echo "<div id='display_data'>";
echo "<table>";
echo "<tr>";
echo "<th>PO Number</th>";
echo "<th>Unique ID</th>";
echo "<th>Kind</th>";
echo "<th>Item Code</th>";
echo "<th>Description</th>";
echo "<th>UoM</th>";
echo "<th>Supplier</th>";
echo "<th>Quantity</th>";
echo "</tr>";
echo "<tr>";
echo "<td><input type='text' name='po_number' id='po_number' value='' style=' border: none;
background: transparent;'></td>";
echo "<td><input type='text' name='unique_id' id='unique_id' value='' style=' border: none;
background: transparent;'></td>";
echo "<td><input type='text' name='kind' id='kind' value='' style=' border: none;
background: transparent;'></td>";
echo "<td><input type='text' name='material_code' id='material_code' value='' style=' border: none;
background: transparent;'></td>";
echo "<td><input type='text' name='description' id='description' value='' style=' border: none;
background: transparent;'></td>";
echo "<td><input type='text' name='uom' id='uom' value='' style=' border: none;
background: transparent;'></td>";
echo "<td><input type='text' name='supplier' id='supplier' value='' style=' border: none;
background: transparent;'></td>";
echo "<td><input type='text' name='qty' id='qty' value='' style=' border: none;
background: transparent;'></td>";
echo "</tr>";
echo "</table>";
echo "</div>";
*/
?>
</form>
</body>
</html>
I can’t figured out what is the cause why it did not fall into if condition even the value of responsetext is Invalid.
I hope somebody can help me.
Thank you.