Hardwarez Posted June 13, 2008 Share Posted June 13, 2008 Ok, new problem. I pasted some java script into my php page. The java script will not execute. I put is some obvious stuff that should execute, but it does not. At once the top I inserted document.write("hello world"); When I view the page I see document.write("hello world"); NOT just hello world. I suspect I have a misplaced character or something, I cant find. here the page code: <script language="LiveScript"> alert("java script"); document.write("Hello World!"); function createRequestObject() { var ro; var browser = navigator.appName; if(browser == "Microsoft Internet Explorer"){ ro = new ActiveXObject("Microsoft.XMLHTTP"); }else{ ro = new XMLHttpRequest(); } return ro; } var http = createRequestObject(); function sndReq(action) { http.open('get', 'dynamic.php?job='+action); http.onreadystatechange = handleResponse; http.send(null); } function handleResponse() { if(http.readyState == 4){ var response = http.responseText; /*document.myForm.email.value=response;*/ var varArray = response.split('|'); document.myForm.client.value=varArray[0]; document.myForm.descript.value=varArray[1];*/ } } function clientcheck(form) { /*.form.email.value document.myForm.email.value = varArray[0]; */ alert("Active"); if (form.jobnumber.value !=""){ sndReq(form.jobnumber.value); } } </script> <?php echo "<html><head><title>Stanger Surveying LLC Technician Management System</title> </head>"; // BEGIN PHP //**************************************************************************************************************** //JAVA NOTE: setTimeout(window.open("http://66.76.55.66/joblist/index.php"),5000); $user = "X; $password = "xxx"; $database = "G"; $host = "localhost"; $link = mysql_pconnect($host, $user, $password); mysql_select_db("stanger") or die(mysql_error()); //******************************************************************************************************** // INCLUDES //******************************************************************************************************** //********************************************************* // MAIN BODY //********************************************************* echo '<style type="text/css"> html, body { height: 100%; } #container { min-height: 100%; position: relative; } #header { margin:0; padding:0; background-color:gray; color:white; z-index: 3; } #nav { position: absolute; top:37px; float:left; width:100%; height: 400px; float:left; margin:0; padding:0; border-left:1px solid gray; z-index: 3; } #footer { height: 10px; width: 100%; position: absolute; top:59px; margin:0; padding:0; background-color:gray; color:white; z-index: 3; } #content { position: absolute; top:80px; } </style>'; //=================================MAIN CODE============================================== echo 'document.write("Hello World!");'; $user=$_GET['user']; $user=mysql_real_escape_string($user); echo '<div id="container"> <div id="header"><h1 class="header">Jobs In Progress</h1></div> <iframe src="http://66.76.55.66/tech/progress.php?user='.$user.'" width="100%" height="130px"></iframe> <div id="header"><h1 class="header">Request Technician Work</h1></div> <div style="position:relative; font-size:16px; top:0; left:0; z-index:1;"> <form name="myForm"><B>Job Number: </b><input type=text name="jobnumber" value="" onBlur="clientcheck(this.form)"> <B>Client Name: </b><input type=text name="client" size=60 maxlength=100><BR><b>Description:  </b><input type="text" size=101 maxlength=100 name=descript></form> </div> <div style="position:relative; top:-14; left:5; color:red; font-size:16px; z-index:2"> Layer 2 </div> '; mysql_close($link); //Close data base!!!!!!!! echo "</body></html>" ?> What this should be doing is getting data from a php page when you tab out of job number and the client and description fields of the form should be filled in. However the script is not running at all.. Please help! Quote Link to comment Share on other sites More sharing options...
Hardwarez Posted June 13, 2008 Author Share Posted June 13, 2008 I cought the mistake that was printing document.write("hello world"); NOT just hello world. The alert placed in the function does not come up when I defocus from the job number input box, so I know the script is not even being called.. WHY? Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted June 13, 2008 Share Posted June 13, 2008 I cought the mistake that was printing document.write("hello world"); NOT just hello world. The alert placed in the function does not come up when I defocus from the job number input box, so I know the script is not even being called.. WHY? Well, the culprit is this line of code: <?php echo 'document.write("Hello World!");'; ?< You can't echo a JavaScript statement in PHP and expect it to magically work. You have to do one of two things: 1. Use PHP to write <script> tags around the echoed code, forcing it to be written as JavaScript, so: <?php echo '<script type="text/javascript">document.write("hello world!");</script>'; ?> 2. Use PHP to echo a value to be obtained through AJAX, and then use JavaScript to actually print it correctly I haven't had the chance to look over all of your code, but your script declaration is pretty screwed up, too. 1. The language is JavaScript, not LiveScript. I'm not sure if (X)HTML automatically defaults to JavaScript if a bad value is passed to one of its attribute tags, but a quick Google search tells me that 'LiveScript' was the original proposed name for the language, nothing more. 2. Since I'm talking about the <script> tag, I should point out that the 'language' attribute is depricated. You should use the 'type' attribute instead. So, with those points out of the way, the correct way to declare script code is: <script type="text/javascript"></script> 3. Right now, your script is outside of the (X)HTML document structure. <script> tags are generally placed inside the <head> tag of an (X)HTML document, like so: <html> <head> <title>Example</title> <script type="text/javascript"> //script code goes here </script> </head> Your alert not showing up may be affected by this. At the very least, you should wait until the DOM loads: <script type="text/javascript"> window.onload = function() //wait until the entire (X)HTML document is loaded, so we're sure we can reference elements correctly { //rest of the code goes here } </script> 4. I'm not sure if you're actually passing the form to the function. Why? Because you tell it to send input.form. I'm not certain if JavaScript allows you to move back up the ladder like that. After all, a form contains inputs, but an input doesn't contain a form. An easier way to do it would be to remove the function call from the input element itself and place it back inside the script tags: <script type="text/javascript"> window.onload = function() { //your other functions go here var myForm = document.forms["myForm"]; //form reference in case you need that commented-out code that refers to an e-mail input var jobNumber = myForm.elements["jobnumber"]; //reference to the jobnumber element jobNumber.onblur = function() //onblur event handler { alert("Active"); if(this.value != "") { sndReq(this.value); } } } </script> And yes, I do mean remove the script from your input element: <input type="text" name="jobnumber"> Since your main project is written in PHP, I suggest putting all of the reused markup code in a separate file and include()/require() it. It's a much better way to do things than echoing out the beginning of an HTML document on every page. Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted June 13, 2008 Share Posted June 13, 2008 You ( from what I could see ) havnt put any semi-colons after your functions in your onblurs etc I could be wrong I'm not a JS programmer but hey <!--- EDIT ----!> Haha that guy owned me I'm a PHP programmer so Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.