Jump to content

php mysql and javascript


nezbo

Recommended Posts

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>

Link to comment
Share on other sites

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'];
}
?>

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.