Pauls74462 Posted December 29, 2008 Share Posted December 29, 2008 I have 2 drop down menus on one page. The first is to get the schoolid and school out of MySQL. This worksMy problem is populating the second box with the teachers of the school from the first box you choose.If possible I would like to know how to use AJAX to get rid on the submit button.Here is my code. <form action='test10.php' method='post'> <select name="Schools" size="1"><?PHPinclude ("config.php");// Make a MySQL Connection$link2 = mysql_connect( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());// select the databasemysql_select_db($dbname) or die ("Could not select database because ".mysql_error());echo '<br / ><br />' . "\n";// put schools in drop down menu$sql = "SELECT schoolid, school FROM schools";$result = mysql_query($sql,$link2);while ($schools = mysql_fetch_row($result)) { echo '<option value="' . $schools[0] . '">' . $schools[1]. '</option>';}echo '<br / ><br /><br / ><br /><br />' . "\n";$id = mysql_real_escape_string($_POST['Schools']);?><form action='test10.php' method='post'><select name="teachers" size="1"><?PHPecho '<br / ><br />' . "\n";// put schools in drop down menu// $sql = "SELECT lname, lname FROM teachers";$sql = "SELECT lname, lname FROM teachers WHERE schoolid = '$id'";$result = mysql_query($sql,$link2);while ($teachers = mysql_fetch_row($result)) { echo '<option value="' . $teachers[0] . '">' . $teachers[1]. '</option>';}echo '</select>' . "\n"; // Closing of list boxecho '<p><input name="Submit" type="submit">' . "\n";echo '</form>' . "\n";echo '<br / ><br /><br / ><br /><br />' . "\n";if (isset($_POST['Schools'])){ $sql = 'SELECT school FROM schools WHERE schoolid=\'' . $_POST['Schools'] . '\''; $result = mysql_query($sql); $row = mysql_fetch_array($result); echo 'The school is "' . $row['school'] . '" with the ID: "' . $_POST['Schools'] . '"'; } echo '<br / ><br /><br / ><br /><br />' . "\n";if (isset($_POST['teachers'])){//$sql = 'SELECT lname FROM teachers WHERE lname=\'' . $_POST['teachers'] . '\'';$sql = "SELECT lname, lname FROM teachers WHERE schoolid = '$id'"; $result = mysql_query($sql); $row = mysql_fetch_array($result);echo '"The teacher is "' . $row['lname'] . '"'; } mysql_close();?> Link to comment Share on other sites More sharing options...
jeffman Posted December 29, 2008 Share Posted December 29, 2008 If you're learning AJAX, I recommend setting up some test files where you just master the basics of 2-way communication. Send "hello" and have your server send it back to you. Do you need help with that stage?Keep the server part in a separate script. You want this thing as light as possible so it runs fast.With some AJAX projects, your PHP script can send back a big string that you incorporate as innerHTML. You can't do that with select options. The easiest thing is to have your script send back the list of teachers as a JSON array constructor.When your javascript receives the JSON text, it runs it through the eval() function, and now you have a real array. Then loop through the array, using select.add() to populate your select element. (You'll need to unpopulate your options first, though.) Link to comment Share on other sites More sharing options...
Pauls74462 Posted December 29, 2008 Author Share Posted December 29, 2008 If you're learning AJAX, I recommend setting up some test files where you just master the basics of 2-way communication. Send "hello" and have your server send it back to you. Do you need help with that stage?Keep the server part in a separate script. You want this thing as light as possible so it runs fast.With some AJAX projects, your PHP script can send back a big string that you incorporate as innerHTML. You can't do that with select options. The easiest thing is to have your script send back the list of teachers as a JSON array constructor.When your javascript receives the JSON text, it runs it through the eval() function, and now you have a real array. Then loop through the array, using select.add() to populate your select element. (You'll need to unpopulate your options first, though.)OK, I'm clueless as to how use AJAX. But in the mean while can you make this work using the submint button, that would be a big help while I learn the other. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.