newphpbees
24 May 2012, 03:55 AM
Hi..
I have ajax code for saving data to database
here is my ajax code:
<script type="text/javascript">
var ajaxTimeOut = null;
var ajaxTimeOutOperator = null;
var responsePHP; // = "no_reply"
var responsePHPOperator;
var changeFocus; //= false;
var transactionWasSaved;
function remoteRequestObject() {
var ajaxRequest = false;
try {
ajaxRequest = new XMLHttpRequest();
}
catch(err) {
try{
ajaxRequest = new ActiveXObject("MSxml2.XMLHTTP");
}
catch(err) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(err){
// --> change to DOM alert("Not Supported Browser") + err.description;
notify('Not Supported Browser.');
return false;
}
}
}
return ajaxRequest;
}
//var ajaxRequest; // = remoteRequestObject();
var ajaxRequest = remoteRequestObject();
var ajaxRequestOperator;
</script>
<script type="text/javascript">
function lotIdCheck(element) {
if (window.event.keyCode==13 || window.event.keyCode==10) {
var txtElementID = element;
if (txtElementID.value == "") { return false; }
// alert(txtElementID.value);
if (ifMatch(txtElementID.value, document.getElementById('txtLotCode').value)) {
capture(element);
}
else {
txtElementID.value = "";
txtElementID.focus();
notify("Lot ID mismatch.Scan again"); //box instead of this...
}
}
}
function timeOut(object) {
if (ajaxTimeOut != null) {
window.clearTimeout(ajaxTimeOut);
}
//alert("this is the reponse " + responsePHP)
if (changeFocus = false || responsePHP == "no_reply" || ajaxRequest.readyState != 4 && ajaxRequest.readyState != 0) {
//alert('ajaxRequest not ready');
ajaxRequest.onreadystatechange = function() {};
ajaxRequest.abort();
document.getElementById(object).disabled = false;
document.getElementById(object).value = "";
changeInputType(document.getElementById(object),'password');
document.getElementById(object).focus();
document.getElementById(object).focus();
var txt = document.getElementById(object);
if (txt.attachEvent) {
txt.attachEvent ("onkeypress", function () {lotIdCheck(txt)});
}
//notify("Please Scan again.");
alert("Please Scan again.");
}
else {
return false;
}
}
function timeOutOperator() {
if (ajaxTimeOutOperator !== null) {
window.clearTimeout(ajaxTimeOutOperator);
}
if (transactionWasSaved = false || responsePHPOperator == "no_reply" || ajaxRequestOperator.readyState != 4 && ajaxRequestOperator.readyState != 0) {
//alert('ajaxRequest not ready');
ajaxRequestOperator.onreadystatechange = function() {};
ajaxRequestOperator.abort();
document.getElementById('txtLotCode').disabled = false;
document.getElementById('txtLotCode').value = "";
document.getElementById('txtLotCode').focus();
document.getElementById('txtLotCode').focus();
//notify("Please Scan again.");
alert("Please Scan again.");
}
else {
return false;
}
}
function clearTiming() {
changeFocus = true;
if (ajaxTimeOut !== null) {
window.clearTimeout(ajaxTimeOut);
}
}
function clearTimingOperator() {
transactionWasSaved = true;
if (ajaxTimeOutOperator !== null) {
window.clearTimeout(ajaxTimeOutOperator);
}
}
function postSet() {
if (window.event.keyCode==13 || window.event.keyCode==10) {
document.getElementById('txtLotCode').disabled = true;
verifyLotcode();
}
}
//------------------------------------------------------------------------------------------------------------------
function verifyLotcode() {
responsePHPOperator = "no_reply";
transactionWasSaved = false;
ajaxRequestOperator = remoteRequestObject();
ajaxRequestOperator.onreadystatechange = function () {
if (ajaxRequestOperator.readyState==4 && ajaxRequestOperator.status==200) {
//var result = ajaxRequestOperator.responseText;
responsePHPOperator = ajaxRequestOperator.responseText;
// alert(responsePHPOperator);
if (responsePHPOperator == "failed") {
document.getElementById('txtLotCode').disabled = false;
document.getElementById('txtLotCode').value = "";
document.getElementById('txtLotCode').focus();
// notify("Please scan again.");
alert("Please scan again.");
}
if (responsePHPOperator == "0") {
//->> change to DOM alert("Unauthorized operator");
// notify("Unauthorized operator.<br />Scan Again.");
alert("Unauthorized operator.<br/>Scan Again.");
if (document.getElementById('txtLotCode').disabled) {
document.getElementById('txtLotCode').disabled = false;
}
document.getElementById('txtLotCode').value = "";
document.getElementById('txtLotCode').focus();
}
else {
// alert ('correct');
save();
// notify("Transaction has been saved.");
// reset();
}
}
}
var str = document.getElementById('txtLotCode').value;
var url = "validate_lotcode.php?q="+str;
ajaxRequestOperator.open("GET",url , true );
// force i.e. not to cache
ajaxRequestOperator.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
//---------------------------
ajaxRequestOperator.send(null);
ajaxTimeOutOperator = window.setTimeout("timeOutOperator();", 100000000);
}
function save() {
// ajaxRequest = remoteRequestObject();
ajaxRequest.onreadystatechange = function () {
if (ajaxRequest.readyState==4 && ajaxRequest.status==200) {
var result = ajaxRequest.responseText;
// alert (result);
if (result == "failed") {
// alert (result);
document.getElementById('txtLotCode').disabled = false;
document.getElementById('txtLotCode').value = "";
document.getElementById('txtLotCode').focus();
//notify("Please scan again.");
alert("Please scan again");
}
if (result == "saved|saved" || result == "saved|saved|saved") {
//alert(result);
alert("Transaction has been saved.");
//notify("Transaction has been saved.");
// reset();
}
}
}
var parameters = "txtLotCode=" + encodeURI(document.getElementById('txtLotCode').value);
var url = "update_dependency.php";
ajaxRequest.open("POST", url, true);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", parameters.length);
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.send(parameters);
}
</script>
<script type="text/javascript">
function reset() {
// alert ('reset');
//enable elements
document.getElementById('txtLotCode').disabled = false;
//reset
document.getElementById('txtLotCode').value = "";
}
</script>
I got problem in function save, even it satisfied this code:
if (result == "saved|saved" || result == "saved|saved|saved") {
but it did not post alert and also not go to reset function.
I don't know why?:(
Thank you
I have ajax code for saving data to database
here is my ajax code:
<script type="text/javascript">
var ajaxTimeOut = null;
var ajaxTimeOutOperator = null;
var responsePHP; // = "no_reply"
var responsePHPOperator;
var changeFocus; //= false;
var transactionWasSaved;
function remoteRequestObject() {
var ajaxRequest = false;
try {
ajaxRequest = new XMLHttpRequest();
}
catch(err) {
try{
ajaxRequest = new ActiveXObject("MSxml2.XMLHTTP");
}
catch(err) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(err){
// --> change to DOM alert("Not Supported Browser") + err.description;
notify('Not Supported Browser.');
return false;
}
}
}
return ajaxRequest;
}
//var ajaxRequest; // = remoteRequestObject();
var ajaxRequest = remoteRequestObject();
var ajaxRequestOperator;
</script>
<script type="text/javascript">
function lotIdCheck(element) {
if (window.event.keyCode==13 || window.event.keyCode==10) {
var txtElementID = element;
if (txtElementID.value == "") { return false; }
// alert(txtElementID.value);
if (ifMatch(txtElementID.value, document.getElementById('txtLotCode').value)) {
capture(element);
}
else {
txtElementID.value = "";
txtElementID.focus();
notify("Lot ID mismatch.Scan again"); //box instead of this...
}
}
}
function timeOut(object) {
if (ajaxTimeOut != null) {
window.clearTimeout(ajaxTimeOut);
}
//alert("this is the reponse " + responsePHP)
if (changeFocus = false || responsePHP == "no_reply" || ajaxRequest.readyState != 4 && ajaxRequest.readyState != 0) {
//alert('ajaxRequest not ready');
ajaxRequest.onreadystatechange = function() {};
ajaxRequest.abort();
document.getElementById(object).disabled = false;
document.getElementById(object).value = "";
changeInputType(document.getElementById(object),'password');
document.getElementById(object).focus();
document.getElementById(object).focus();
var txt = document.getElementById(object);
if (txt.attachEvent) {
txt.attachEvent ("onkeypress", function () {lotIdCheck(txt)});
}
//notify("Please Scan again.");
alert("Please Scan again.");
}
else {
return false;
}
}
function timeOutOperator() {
if (ajaxTimeOutOperator !== null) {
window.clearTimeout(ajaxTimeOutOperator);
}
if (transactionWasSaved = false || responsePHPOperator == "no_reply" || ajaxRequestOperator.readyState != 4 && ajaxRequestOperator.readyState != 0) {
//alert('ajaxRequest not ready');
ajaxRequestOperator.onreadystatechange = function() {};
ajaxRequestOperator.abort();
document.getElementById('txtLotCode').disabled = false;
document.getElementById('txtLotCode').value = "";
document.getElementById('txtLotCode').focus();
document.getElementById('txtLotCode').focus();
//notify("Please Scan again.");
alert("Please Scan again.");
}
else {
return false;
}
}
function clearTiming() {
changeFocus = true;
if (ajaxTimeOut !== null) {
window.clearTimeout(ajaxTimeOut);
}
}
function clearTimingOperator() {
transactionWasSaved = true;
if (ajaxTimeOutOperator !== null) {
window.clearTimeout(ajaxTimeOutOperator);
}
}
function postSet() {
if (window.event.keyCode==13 || window.event.keyCode==10) {
document.getElementById('txtLotCode').disabled = true;
verifyLotcode();
}
}
//------------------------------------------------------------------------------------------------------------------
function verifyLotcode() {
responsePHPOperator = "no_reply";
transactionWasSaved = false;
ajaxRequestOperator = remoteRequestObject();
ajaxRequestOperator.onreadystatechange = function () {
if (ajaxRequestOperator.readyState==4 && ajaxRequestOperator.status==200) {
//var result = ajaxRequestOperator.responseText;
responsePHPOperator = ajaxRequestOperator.responseText;
// alert(responsePHPOperator);
if (responsePHPOperator == "failed") {
document.getElementById('txtLotCode').disabled = false;
document.getElementById('txtLotCode').value = "";
document.getElementById('txtLotCode').focus();
// notify("Please scan again.");
alert("Please scan again.");
}
if (responsePHPOperator == "0") {
//->> change to DOM alert("Unauthorized operator");
// notify("Unauthorized operator.<br />Scan Again.");
alert("Unauthorized operator.<br/>Scan Again.");
if (document.getElementById('txtLotCode').disabled) {
document.getElementById('txtLotCode').disabled = false;
}
document.getElementById('txtLotCode').value = "";
document.getElementById('txtLotCode').focus();
}
else {
// alert ('correct');
save();
// notify("Transaction has been saved.");
// reset();
}
}
}
var str = document.getElementById('txtLotCode').value;
var url = "validate_lotcode.php?q="+str;
ajaxRequestOperator.open("GET",url , true );
// force i.e. not to cache
ajaxRequestOperator.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
//---------------------------
ajaxRequestOperator.send(null);
ajaxTimeOutOperator = window.setTimeout("timeOutOperator();", 100000000);
}
function save() {
// ajaxRequest = remoteRequestObject();
ajaxRequest.onreadystatechange = function () {
if (ajaxRequest.readyState==4 && ajaxRequest.status==200) {
var result = ajaxRequest.responseText;
// alert (result);
if (result == "failed") {
// alert (result);
document.getElementById('txtLotCode').disabled = false;
document.getElementById('txtLotCode').value = "";
document.getElementById('txtLotCode').focus();
//notify("Please scan again.");
alert("Please scan again");
}
if (result == "saved|saved" || result == "saved|saved|saved") {
//alert(result);
alert("Transaction has been saved.");
//notify("Transaction has been saved.");
// reset();
}
}
}
var parameters = "txtLotCode=" + encodeURI(document.getElementById('txtLotCode').value);
var url = "update_dependency.php";
ajaxRequest.open("POST", url, true);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", parameters.length);
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.send(parameters);
}
</script>
<script type="text/javascript">
function reset() {
// alert ('reset');
//enable elements
document.getElementById('txtLotCode').disabled = false;
//reset
document.getElementById('txtLotCode').value = "";
}
</script>
I got problem in function save, even it satisfied this code:
if (result == "saved|saved" || result == "saved|saved|saved") {
but it did not post alert and also not go to reset function.
I don't know why?:(
Thank you