0

I'm trying to trap a user entry that cannot be found in the database. When a code is entered, the page should give a "Code Not Found" message if it is an undefined value. However, it didn't; and it always shows the "undefined" value to the page instead.

I'm really new to PHP, Javascript and AJAX, and I would greatly appreciate if you could help me on this.

Here's what I actually did:

getting user input:

<input type="text" name="fscode" size = "4" maxlength = "4" onKeyPress="return checkEnter(event,this.name,document.fchartact.locode)">

finding the code:

<?php

switch ($_GET['action']){
	case "find":
	if ($nrows == 1){
			echo $results['LOCODE'][0]."$".
				 $results['FCCODE'][0]."$".
				 $results['EXCODE'][0]."$".
				 $results['ITM'][0]."$".
				 $results['AST'][0]."$".
				 $results['IDN'][0]."$".
				 $results['SNO'][0]."$".
				 $results['QTY'][0]."$".
				 $results['NAME'][0]."$".
				 $results['CAT'][0]."$".
				 $results['EDIT'][0]."$";
	}
	else echo "not found$";
	break;
}
?>

functions called:

function checkEnter(e,obj,dest){ 
	var characterCode;
	if(e && e.which){ 
	e = e;
	characterCode = e.which; 
	}
	else{
	e = event;
	characterCode = e.keyCode;
	}
	if(characterCode == 13){
			if (this.document.fchartact.fscode.value.length == 4) 
			{
		if (obj == "fscode"){
							processcode("find");
					} 
					else if (obj == "name") {
							fs = document.fchartact.fscode.value;
							if (fs.indexOf("0") != -1) document.fchartact.save.focus(); 
							else dest.focus();
					}
					else if (obj == "edit") document.fchartact.save.click();
					else dest.focus();
					return false;
			} 
	}  
	else return true;
}
var http = getXMLHTTPRequest();
function processcode(action) {
  var coid  = document.fchartact.coidnum.value;
  var fs    = document.fchartact.fscode.value;

  var myurl = "../mas/chartajax.php";
  var param = "?fscode=" + fs + "&coidnum=" + coid + "&action=" + action;

  if (action =="save") { 
      param = param + "&locode=" + document.fchartact.locode.value 
                    + "&fccode=" + document.fchartact.fccode.value 
                    + "&excode=" + document.fchartact.excode.value  
                    + "&itm="    + document.fchartact.itm.value     
                    + "&ast="    + document.fchartact.ast.value    
                    + "&idn="    + document.fchartact.idn.value    
                    + "&sno="    + document.fchartact.snum.value   
                    + "&qty="    + document.fchartact.qty.value    
                    + "&name="   + document.fchartact.name.value   
                    + "&cat="    + document.fchartact.desc2.value  
                    + "&edit="   + document.fchartact.edit.value;
					
      if (fs=='' || document.fchartact.name.value == '') {
          alert("Please supply necessary data!");
          document.fchartact.fscode.focus();
          return false;
      }
	  
  }
	else if (action == "delete") {
		if (!confirm("Sure to delete this fscode: " + fs)) return false;
	}
   
		
	var myRand = parseInt(Math.random()*999999999999999); 
	document.fchartact.action.value = action; 
	
	modurl = myurl + param + "&rand=" + myRand;
	http.open("GET", modurl, true);
	http.onreadystatechange = getResult;
	http.send(null);
}
function getResult() {
   if (http.readyState == 4) {
    if(http.status == 200) {
       var resultlist = http.responseText.split("$");
       dispval(resultlist);
  	  }
   }
}
function dispval(res){
		var act = document.fchartact.action.value;
        
		if (act=="find") { 
			this.document.fchartact.locode.disabled = 0;
			this.document.fchartact.fccode.disabled = 0;
			this.document.fchartact.excode.disabled = 0; 
			this.document.fchartact.itm.disabled    = 0; 
			this.document.fchartact.ast.disabled    = 0; 
			this.document.fchartact.idn.disabled    = 0; 
			this.document.fchartact.snum.disabled   = 0;
			this.document.fchartact.qty.disabled    = 0; 
			this.document.fchartact.name.disabled   = 0; 
			this.document.fchartact.desc2.disabled  = 0;
			this.document.fchartact.edit.disabled   = 0;
			this.document.fchartact.locode.focus();
            alert(res[0]);
			if(res[0] == 'not found'){
					var fs = document.fchartact.fscode.value;
					var msg = fs + " not found.";
					this.document.fchartact.msg.value = msg;
					this.document.fchartact.locode.value = '';
					this.document.fchartact.fccode.value = '';
					this.document.fchartact.excode.value = ''; 
					this.document.fchartact.itm.value    = 'no'; 
					this.document.fchartact.ast.value    = 'no'; 
					this.document.fchartact.idn.value    = 'no'; 
					this.document.fchartact.snum.value   = 'no';
					this.document.fchartact.qty.value    = 'no'; 
					this.document.fchartact.name.value   = ''; 
					this.document.fchartact.desc2.value  = '';
					this.document.fchartact.edit.value   = 'no';
					this.document.fchartact.locode.focus();
			}
			else {
					this.document.fchartact.locode.value = res[0] ;
					this.document.fchartact.fccode.value = res[1];
					this.document.fchartact.excode.value = res[2]; 
					this.document.fchartact.itm.value    = res[3]; 
					this.document.fchartact.ast.value    = res[4]; 
					this.document.fchartact.idn.value    = res[5]; 
					this.document.fchartact.snum.value   = res[7];
					this.document.fchartact.qty.value    = res[6]; 
					this.document.fchartact.name.value   = res[8]; 
					this.document.fchartact.desc2.value  = res[9];
					this.document.fchartact.edit.value   = res[10];
}

I'm already having headaches trying to make this small thing work out!

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Chokladkakan
0

I am afraid I don't really have time to read it all through, but it seems like you use the var keyword when declaring variables inside your functions: this makes the variables local. If you instead simple write the variable name and its value, the variable will be added to the window object and thus global.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.