Jump to content

AJAX-->PHP function-->javascript?


fartface

Recommended Posts

Hello,

 

I'm a total noob, so preemptive apologies for my ignorance -- and thanks for your help!

 

I'm trying to create a simple inventory database to track four computer hardware 'types', using php/mysql/apache. There's an html index page which draws a table with a row for each hw type. Each type links to a table with five or six columns -- make, model, serial, etc. The user chooses a type from the left most column, and a <select> pops up in the next column over with the available options for the next entry in the db table (e.g., for 'make' there's 'ibm' and 'ncr').

 

In other words, if it worked, you'd start with something like this (obviously not actual code, but a crudely "drawn" picture of the browser window as I want it to look):

 

<button>Click a hardware type</button> |

 

When you clicked a type, you'd see this:

 

<button>Click a hardware type</button> | <select>Select a make</select>

 

When you select a make, you'd get this:

 

<button>Click a hardware type</button> | <select>Select a make</select> | <select> Select a model </select>

 

And on and on until you'd selected a value for each column. The first column is in the html index page, and clicking a hardware type uses AJAX to fire a php function which writes in the select and queries the database, etc. The problem is that it I can't call javascript from the php file. Here's the php function:

 

	function pop_make($type)
{
	// connection to db
	$con = select_db(connect_to_db());

	// sql query
	$sql = "select distinct make from resource_details where type_name = '".$type."'";

	// select distinct makes of specified hardware type
	//if query returns something, display makes in an html select list
	if ($result = mysql_query($sql, $con))
	{
		$len = mysql_num_rows($result);

		// when option is selected, call handle_request function from js file
		echo "<select name='select_make' onchange='handle_request('model_div', this.options[this.selectedIndex].value)'><option>---</option>";
		while ($row=mysql_fetch_array($result))
		{
			if ($row['make'] != NULL)
				echo "<option>".$row['make']."</option>";
			else
				echo "<option>NULL</option>";
		}
		echo "<option>Add new entry</option></select>";
		mysql_free_result($result);
	}

 

And here's the javascript that I'm trying to call with it:

 

function handle_request(target, action)
{
//alert("yes");
xmlhttp = get_request_object();
xmlhttp.onreadystatechange=function()
{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		document.getElementById(target).innerHTML=xmlhttp.responseText;
	}
}
xmlhttp.open("GET","handle_request.php?action="+action,true);
xmlhttp.send();
}

 

The select populates perfectly, but the onclick does not call the handle_request function. I threw in a simple alert() for testing, and even that does nothing. What's going on? Is it possible to use AJAX in a php function?

Link to comment
Share on other sites

Your javascript goes between these tags:

<script type="text/javascript">

 

</script>

 

And yes The PHP makes your HTML page that you look at and the button does (or should) call the js function.

 

Try using this line:

echo "<select name='select_make' onchange=\"handle_request('model_div', this.options[this.selectedIndex].value)\"><option>---</option>";

Link to comment
Share on other sites

You need to give us the full scripts.  The PHP function listed, is just a function, you aren't specifying how you are calling that function. You also don't show how you are getting the value of action, in the query string (end of the url).

I threw in a simple alert() for testing, and even that does nothing.

If you are using firefox, you can pull up your error log (Tools->Error Console), clear it, then try to make the function cycle.  If there are javascript errors, it will show up in the error console.

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.