jwpryor Posted July 21, 2007 Share Posted July 21, 2007 I have a form with a list/menu in it that is populated with records from a database. The items in list#2 are based upon what is selected in list#1. I have been able to use a combination of php and javascript to get it to work, but the javascript reloads the page to get list #2 to refresh - and that clears out any other fields that have been filled in. Instead of javascript being called for the 'onchange' event for list #1, I'd rather it call php when list#1 is changed and so reset the values in list#2. OPTIONS: 1. I thought I could have a php function that is called (i.e. onchange="<?php reload();?>" but I can't figure out how to set the values of the list from within the php function. How to reference form objects with php? or 2. is there some better way to do this? Thank you Julia Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 21, 2007 Share Posted July 21, 2007 JavaScript is run client-side, PHP is run server-side. You cannot call/run PHP in the browser. You could use javascript to call a PHP page and return back the results to the page to repopulate the select list. This is called AJAX. However, if your lists are relatively small I would suggest doing it all client-side in the JavaScript. There's no reason that using JavaScript to repopulate a select list will reset all the fields on the form, unless you are doing something wrong. Perhaps it would help if you provided a link or a sample of the code. Quote Link to comment Share on other sites More sharing options...
jwpryor Posted July 22, 2007 Author Share Posted July 22, 2007 SITUATION: when 1 drop down list is selected, drop down list #2 is repopulated with new values (via javascript - see #2 below) PROBLEM: When the drop down list changes, any other fields that have already been filled in, are reset - instead of keeping the value the user just entered. ----------------------------------- I'm sure that I am probably doing something wrong - or least at this point don't know how to do it any differently. One thing there must be a different way of doing, is when the javascript is called, it then reloads the current page to reset list#2 (See #2 below). This is when the other fields that have been set are cleared out/reset. I have placed the primary code below (instead of placing all the code). I can include all the code if this is not enough to help me with my problem. Thank you, Julia ------------------------------- 1. at the beginning of the file, I have PHP code to connect to the database and query results for list#1 <?php //get the list of tier1 items mysql_select_db($database_connWIN, $connWIN); ///////// Getting the data from Mysql table for first list box////////// $quer2=mysql_query("SELECT tier1Name,tier1ID FROM tier1 order by tier1Name"); 2. Within the HTML <head> tag, I have the javascript functions that are called for the 'onChange' event for the list #1. <script language="JavaScript" type="text/JavaScript"> <!-- //function 'reload' called when list#1 has a change onchange="reload(this.form)" function reload(form) { var val=form.tier1.options[form.tier1.options.selectedIndex].value; self.location='enrollmentform.php?cat=' + val ; } //--> </script> 3. Within the HTML form, I have php code that sets the values of the two lists. This is the code for list #2 <select name='area' onchange=""><option value=''>Choose the assessment site</option> <?PHP while($noticia = mysql_fetch_array($quer)) { if($noticia['tier2ID']==@$cat3){echo "<option selected value='$noticia[tier2ID]'>$noticia[tier2Name] </option>";} else{echo "<option value='$noticia[tier2ID]'>$noticia[tier2Name]</option>";} } ?> </select> 4. I have a text field that gets reset each time the page is reloaded (via the javascript above). input name="firstName" type="text" id="firstName" value="<?php if (isset($_POST['firstName'])) echo $_POST['firstName'];?>" maxlength="20"> Quote Link to comment Share on other sites More sharing options...
Barand Posted July 22, 2007 Share Posted July 22, 2007 If you are using javascript there should be no need to resubmit the form - that's what is clearing your fields. Create javascript arrays to populate the second list depending on value in first. 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.