nezbo Posted March 19, 2008 Share Posted March 19, 2008 hi all i have a problem that i want to beable to run a javascript from an html form that i have working perfictly , if i change a select box it calls a javascript function. my problem is that i want to call a mysql database, i am not to sure how to go about this, i have been using php throught the project and i am not to shure how to get a javascript var in to a php mySql SELECT command. This is the code i have so far : <script type="text/javascript"> function getStaffHours() { var selectedStaffName = document.getElementById("nameSelected").value; <?php $getTheStaffHorusFromDatabase = mysql_query("SELECT * FROM person WHERE CallID = '" ?>selectedStaffName<?php "'"); while ($getTheStaffHorusFromDatabase2 = mysql_fetch_array ($getTheStaffHorusFromDatabase)) { $theTotalHours1 = $getTheStaffHorusFromDatabase2['contHours']; } ?> var theTotalHours1 = <?php echo $theTotalHours1; ?>; document.getElementById("staffHours1").value = theTotalHours1; document.getElementById("staffHours2").value = theTotalHours2; document.getElementById("staffHours3").value = theTotalHours3; document.getElementById("staffHours4").value = theTotalHours4; } </script> Quote Link to comment Share on other sites More sharing options...
trq Posted March 19, 2008 Share Posted March 19, 2008 Javascript runs on the client, while php runs on the server. You can't simply run php within Javascript, you need to make a request to the server. You might want to look at some Ajax tutorials. Quote Link to comment Share on other sites More sharing options...
nezbo Posted March 19, 2008 Author Share Posted March 19, 2008 Cheers, i guess ajax is a whole difrent ballgame??? dose any one know of a good tutirial? Quote Link to comment Share on other sites More sharing options...
nezbo Posted March 19, 2008 Author Share Posted March 19, 2008 I have looked at the aJax and i have found this but i can not get it to work properly. Any ideas why here are the functions : <script type="text/javascript"> function ajaxFunction(AJAXpage) { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.getElementById("staffHours1").value=xmlHttp.responseText; } } xmlHttp.open("GET","AJAXpage",true); xmlHttp.send(null); } function getStaffHours() { var theSelectedUser = document.getElementById("nameSelected").value; ajaxFunction("aJaxUserQ.php&nameSelected =" . theSelectedUser); } </script> and here is the aJaxUserQ.php <?php include ("include/dbcon.php"); $getTheUser = mysql_query("SELECT * FROM person WHERE CallID = '" . $_REQUEST['nameSelected'] . "'"); while ($getTheUser2 = mysql_fetch_array($getTheUser)) { echo $userHours = $getTheUser2['contHours']; } ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 19, 2008 Share Posted March 19, 2008 "but i can not get it to work properly" what is not working properly? it doesn't look like you've added any echo's or alerts to debug. that's where i would start. Quote Link to comment Share on other sites More sharing options...
lemmin Posted March 19, 2008 Share Posted March 19, 2008 What exactly are you trying to do with javascript and mysql? It might be possible to do it without ajax if you would rather. Quote Link to comment Share on other sites More sharing options...
nezbo Posted March 19, 2008 Author Share Posted March 19, 2008 by proberly i mean it brings back a page but i am geting a 404 error page... i.e. i get the following text in the 'returning' text box. <?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><title>Object not found!</title><link rev="made" href="mailto:admin@localhost" /><style type="text/css"><!--/*--><![CDATA[/*><!--*/ body { color: #000000; background-color: #FFFFFF; } a:link { color: #0000CC; } p, address {margin-left: 3em;} span {font-size: smaller;}/*]]>*/--></style></head><body><h1>Object not found!</h1><p> The requested URL was not found on this server. The link on the <a href="http://localhost/call_Log/staffHours.php">referring page</a> seems to be wrong or outdated. Please inform the author of <a href="http://localhost/call_Log/staffHours.php">that page</a> about the error. </p><p>If you think this is a server error, please contactthe <a href="mailto:admin@localhost">webmaster</a>.</p><h2>Error 404</h2><address> <a href="/">localhost</a><br /> <span>03/19/08 17:21:52<br /> Apache/2.2.4 (Win32) DAV/2 mod_ssl/2.2.4 OpenSSL/0.9.8d mod_autoindex_color PHP/5.2.1</span></address></body></html> and also if i run the aJaxUserQ.php it seems to be working and giving me back 37 that is what i was expecting. "but i can not get it to work properly" what is not working properly? it doesn't look like you've added any echo's or alerts to debug. that's where i would start. Quote Link to comment Share on other sites More sharing options...
nezbo Posted March 19, 2008 Author Share Posted March 19, 2008 the latest code that i have posted is aJax, as i was told by thorpe, so i have had a look around and this is what i have come up with so far... <script type="text/javascript"> function ajaxFunction(AJAXpage) { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.getElementById("staffHours1").value=xmlHttp.responseText; } } xmlHttp.open("GET","AJAXpage",true); xmlHttp.send(null); } function getStaffHours() { var theSelectedUser = document.getElementById("nameSelected").value; ajaxFunction("aJaxUserQ.php&nameSelected =" . theSelectedUser); } </script> and aJaxUserQ.php is : <?php include ("include/dbcon.php"); $getTheUser = mysql_query("SELECT * FROM person WHERE CallID = '" . $_REQUEST['nameSelected'] . "'"); while ($getTheUser2 = mysql_fetch_array($getTheUser)) { echo $userHours = $getTheUser2['contHours']; } ?> hope this helps you to help me. What exactly are you trying to do with javascript and mysql? It might be possible to do it without ajax if you would rather. Quote Link to comment Share on other sites More sharing options...
lemmin Posted March 19, 2008 Share Posted March 19, 2008 You quoted me but didn't answer my question. I'm so confused. Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted March 19, 2008 Share Posted March 19, 2008 you have an "&" where it is supposed to be a "?" after aJaxUserQ.php function getStaffHours() { var theSelectedUser = document.getElementById("nameSelected").value; ajaxFunction("aJaxUserQ.php&nameSelected =" . theSelectedUser); } </script> do this function getStaffHours() { var theSelectedUser = document.getElementById("nameSelected").value; ajaxFunction("aJaxUserQ.php?nameSelected =" . theSelectedUser); } </script> Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted March 19, 2008 Share Posted March 19, 2008 also take out the space between nameSelected and = like so ajaxFunction("aJaxUserQ.php?nameSelected=" . theSelectedUser); Quote Link to comment Share on other sites More sharing options...
nezbo Posted March 19, 2008 Author Share Posted March 19, 2008 sorry i thought my first post would be clear. what i am trying to do is populate a text box from a mysql, i have setup the file called aJaxUserQ.php, this gives me a number, in this case it is 37 (this is as an echo). so i am using javascript/ajax to call this php. You quoted me but didn't answer my question. I'm so confused. Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted March 19, 2008 Share Posted March 19, 2008 ok... did you make the changes? Quote Link to comment Share on other sites More sharing options...
nezbo Posted March 19, 2008 Author Share Posted March 19, 2008 Cheers for your last 2 replys but i still cant get it to work... any more ideas, i think that the problem is with calling the php. ??? also take out the space between nameSelected and = like so ajaxFunction("aJaxUserQ.php?nameSelected=" . theSelectedUser); Quote Link to comment Share on other sites More sharing options...
nezbo Posted March 19, 2008 Author Share Posted March 19, 2008 Yep, still no luck. ok... did you make the changes? Quote Link to comment Share on other sites More sharing options...
nezbo Posted March 19, 2008 Author Share Posted March 19, 2008 is it posible to call a .php file and put the results of the HTML in to a text box? Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted March 19, 2008 Share Posted March 19, 2008 you cant stll be getting the 404 error if you made those changes. or are you? Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted March 19, 2008 Share Posted March 19, 2008 yeah its possible 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.