Javascript not working in PHP class file

marulu 0 Tallied Votes 143 Views Share

I am in the process of learning PHP with JS and Ajax and have the foll. problems. I have created a class to generate master data entry screens.
(1) Javascript code that works when I run master.class.php independently does not work when I run users.php. I came across this problem while trying to set focus to the first editable text box.
(2) All the user will have to do is define the parameters for the class as in users1.php and the screen will be ready in a jiffy. But I have ended up "including" this users1.php in showdata.php which is not correct as showdata.php is a general file which will be used for all future screens. Please tell me what the correct method to do this is. It ought to have been in users.php or something.

I have shown here the minimum code just to demonstrate the problem I am facing. Any help would be appreciated. Thanks in advance!

P.S. I have the foll. files : master.class.php, users.php, users1.php,showdata.php, script.js

users.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Users Master</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="script.js"></script>

</head>

<body onLoad="init_table();">
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0" class="tableBorder">
  <tr>
    <td align="center" bgcolor="#eeeeee"><h1>Users Master</h1></td>
  </tr>
  <tr>
    <td height="25" align="center"> 
 </td>
  </tr>
  <tr>
    <td><div id="showdata">
</div></td>
  </tr>
  <tr>
    <td align="center"> </td>
  </tr>
  <tr>
    <td height="30" align="center" bgcolor="#eeeeee"> </td>
  </tr>
</table>
<p> </p>

</body>
</html>


users1.php :

<?php
$mast = new master;
$mast->FormatFld("id", "ID", 5);
$mast->FormatFld("username", "User Name", 10);
$mast->FormatFld("password", "Password", 10);
$mast->table("tbl_admin");
$mast->keyfld("id");
$mast->findfld("username");
$mast->fldlist(); 
$mast->init();
?>

master.class.php

<html>
<head>
<title>Users Master***</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="script.js"></script>

</head>
<body onLoad="test();">
<script language="javascript">
alert("hallo");
</script>
</body>

</html>

<?php
class master{
	var $table;			# Tablename
	var $keyfld;          
	var $findfld;          
	var $keyfldval; 
	var $fieldsArray = array();	 
	var $sqlfirst;
	var $sqllast;
	var $sqlnext;
	var $sqlprev;
	var $sqlthis;
	var $fldlist;
	var $fldcnt;
	var $MaxPrmLen; 
	var $Button; 
	var $ButtStr; 
	var $mode;
	var $reccnt;
	var $disabled;
	var $result;
	var $lastfld;	
	var $fldarray;	
	
function table($table){
	$this->table = $table;
	}	

function keyfld($keyfld){
	$this->keyfld = $keyfld;
	}

function findfld($findfld){
	$this->findfld = $findfld;
	}

function init(){
}

function MaxPrmLen(){
	$maxlen = 0;
	foreach ($this->fieldsArray as $value){
		$mprompt = $value["FldPrompt"];
		if (strlen($mprompt) > $maxlen)
			$maxlen = strlen($mprompt);
		}
	return($maxlen);
}

function GetData(){
}		

function dspdata(){
}

function FormatFld($FldName, $FldPrompt, $FldWidth){
	$this->fieldsArray["$FldName"]["FldName"] = $FldName; 		  
	$this->fieldsArray["$FldName"]["FldPrompt"] = $FldPrompt; 	
	$this->fieldsArray["$FldName"]["FldWidth"] = $FldWidth; 	
	$this->fieldsArray["$FldName"]["FldVal"] = ''; 	
}

function fldlist(){
}

function saveadd(){
}

function savemod(){
}

function deldata(){
}
}
echo "<input type='text' id='id' value = 'some text' size=10> </td> </tr>";
echo "<script language=\"javascript\"> alert('aft class'); </script>";
?>



showdata.php

<html>
<head>
<script type="text/javascript" src="script.js"></script>
</head>
<body onLoad="test();">
</body>
</html>

<?php

require_once ("master.class.php");
require_once ("users1.php");

$mast->mode=$_GET["mode"];

if (($mast->mode != 'listblnk') and ($mast->mode != 'first') and ($mast->mode != 'last') and ($mast->mode != 'find') and ($mast->mode != 'add')) 	
	$mast->keyfldval = $_GET["$mast->keyfld"];
error_reporting(E_ALL ^ E_NOTICE);

echo "<table width='700' border='0' cellpadding='4' cellspacing='1' bgcolor='#eeeeee' align='center'>";
echo "<tr>";

switch ($mast->mode)	{
	case 'list':
	case 'listblnk':
		$mast->disabled = 'true';
		$mast->getdata();
		break;
	case 'add':	
	case 'modify':
		$mast->disabled = 'false';
		$mast->GetData();
		break;
	case 'first':	
	case 'next':	
	case 'prev':	
	case 'last':	
	case 'find':	
		$mast->GetData();
		break;
	case 'delete':	
		$mast->deldata();
		break;
	case 'addsave':	
		$mast->saveadd();
		break;
	case 'modsave':	
		$mast->savemod();
		break;
	}
?>


script.js :

var request=null;
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
} 

var http = GetXmlHttpObject(); 

    function requestInfo(url,id,redirectPage) {      
		var temp=new Array();
			http.open("GET", url, true);
			http.onreadystatechange = function() {
				if (http.readyState == 4) {
				  if(http.status==200) {
			  		var results=http.responseText;
					if(redirectPage=="" || results!="1") {
						
						var temp=id.split("~"); 
						var r=results.split("~");
						if(temp.length>1) {
							for(i=0;i<temp.length;i++) {	
								document.getElementById(temp[i]).innerHTML=r[i];
							}
						} else {
							document.getElementById(id).innerHTML = results;
						}	
					} else {
						window.location.href=redirectPage;			
					}
				  } 
  				}
			};
			http.send(null);
       }

function emptyValidation(fieldList) {
		
		var field=new Array();
		field=fieldList.split(",");
		var counter=0;
		for(i=0;i<field.length;i++) {
			if(document.getElementById(field[i]).value=="") {
				document.getElementById(field[i]).style.backgroundColor="#FF0000";
				counter++;
			} else {
				document.getElementById(field[i]).style.backgroundColor="#FFFFFF";	
			}
		}
		if(counter>0) {
				alert("Fields marked red must be filled in!");
				return false;
				
		}  else {
			return true;
		}
}

function init_table() {
requestInfo('showdata.php?mode=listblnk','showdata','');
}

function Save(fldlist) {
var field=new Array();
field=fldlist.split(",");
strng = 'showdata.php?mode=addsave';
for(i=0;i<field.length;i++)
	strng = strng + '&' + field[i] + "=" + document.getElementById(field[i]).value ;

var checkValidation=emptyValidation(fldlist);	
	if(checkValidation==true) 
		requestInfo(strng,'showdata','');
}
	
function Savem(fldlist) {
var field=new Array();
field=fldlist.split(",");
strng = 'showdata.php?mode=modsave';
for(i=0;i<field.length;i++)
	strng = strng + '&' + field[i] + "=" + document.getElementById(field[i]).value ;

var checkValidation=emptyValidation(fldlist);	
	if(checkValidation==true) 
		requestInfo(strng,'showdata','');
	}

function Last() {
requestInfo('showdata.php?mode=last','showdata','');
}	

function First() {
requestInfo('showdata.php?mode=first','showdata','');
}	

function Previous(keyfld) {
var keyfldval=document.getElementById(keyfld).value;
requestInfo('showdata.php?mode=prev&'+ keyfld + '='+ keyfldval,'showdata','');
}	

function Next(keyfld) {
var keyfldval=document.getElementById(keyfld).value;
requestInfo('showdata.php?mode=next&'+ keyfld + '='+ keyfldval,'showdata','');
}	

function Cancelm(keyfld) {
var keyfldval=document.getElementById(keyfld).value;
requestInfo('showdata.php?mode=list&'+ keyfld + '='+ keyfldval,'showdata','');
}

function Cancel() {
requestInfo('showdata.php?mode=listblnk','showdata','');
}

function Add() {
requestInfo('showdata.php?mode=add','showdata','');
fld = "id";
document.getElementById(fld).focus();
}

function Modify(keyfld) {
var keyfldval=document.getElementById(keyfld).value;
requestInfo('showdata.php?mode=modify&'+ keyfld + '='+ keyfldval,'showdata','');
}

function Find() {
requestInfo('showdata.php?mode=find','showdata','');
}

function Delete(keyfld){
   var keyfldval=document.getElementById(keyfld).value;
   var is_confirmed = confirm('Are you sure to delete this record?\n\nThis will permanently delete the Record!');
	if (is_confirmed) 
 		requestInfo('showdata.php?mode=delete&'+ keyfld + '='+ keyfldval,'showdata','');
}

function changeSymbol(val, keyfld){
var keyfldval=document.getElementById(keyfld).value;
	if( request=GetXmlHttpObject() )
		{
		keyfldval = encodeURIComponent(val);
		requestInfo('showdata.php?mode=list&'+ keyfld + '='+ keyfldval,'showdata','');
		}
}

function handleEnter(e,nextfld)
{
	var characterCode;

	if(e && e.which)
	{
		e = e;
		characterCode = e.which;
	} 
	else 
	{
		e = event;
		characterCode = e.keyCode;
	}

	if(characterCode == 13)
	{
		document.getElementById(nextfld).focus();
		return false;
	} 
	else if (charCode > 31 && (charCode < 48 || charCode > 57)) 
		return false;
	 else
		return true;
	
}


function setFocus(){
alert("in setfocus");
document.getElementById("id").focus();
} 


function test(){
	alert("after load");
	document.getElementById("id").focus();
}