fartface Posted June 15, 2011 Share Posted June 15, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/239472-ajax-php-function-javascript/ Share on other sites More sharing options...
sunfighter Posted June 15, 2011 Share Posted June 15, 2011 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/239472-ajax-php-function-javascript/#findComment-1230245 Share on other sites More sharing options...
jcbones Posted June 16, 2011 Share Posted June 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239472-ajax-php-function-javascript/#findComment-1230267 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.