View Full Version : question about onchange=...

10-24-2007, 02:46 PM
I recently viewed an excellent (2 Part) tutorial by David J. regarding Drop Down Menus. It dealt with Car Makes and Car Models. I followed it through several times and tried setting it up like he said to. However, I am stuck on the onchange="document.forms[0].submit()" line...

David mentioned that it dealt with a javascript but didn't go over the code in the tutorial. I would like to know what I need to do to make the onchange= statement work. When I try and select an item from the Car Make drop down on my php page- www.wellmantechnologies.com/vehicles_search.php (http://www.wellmantechnologies.com/vehicles_search.php) it just hangs there and I assume it's b/c of the lack of a javascript...

BTW, I am new to php and mysql and have 0 javascript experience. Just trying to learn the basics and thought this tutorial was something I could build on...

Thanks in advance for any help.:grin: :grin:


P.S. Here is the entire code-

require_once("Connections/test.php"); // database connection

$Make = $_POST['Make'];
if ($Make) {

$query = sprintf("SELECT * FROM Model where Model_Id='$Make'");
$result = @mysql_query($query);
$rowModel = @mysql_fetch_array($result);


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>


<form id="form1" name="form1" method="post" action="vehicles_search.php">

<select name="Make" onchange="document.forms[0].submit()">
<option value="" selected>Select Make</option>
<option value="1">Audi</option>
<option value="2">BMW</option>
<option value="3">Chevrolet</option>
<option value="4">Dodge</option>
<option value="5">Honda</option>
<option value="6">Mazda</option>
<option value="7">Nissan</option>
<option value="8">Pontiac</option>

<select name="Model">
<option value="">Select Model</option>
<?php do { ?>
<option value="<?php echo $rowModel['Model_Id']; ?>"><?php echo $rowModel['Model']; ?></option>
<?php }while ($rowModel = mysql_fetch_array($result)); ?>



10-24-2007, 04:10 PM
welcome jewellman. I'm pleased your enjoying the vids

Back to your question

this is all you need in the select tag (drop down)


The onchange is an event like onclick or onmouseover

It tells the browser when to call a function and in this case I'm using onchange so when you change the default value from the drop down it submits the form.

Javascript uses a dot reference model to target individual elements or fire commands.

document is the current document
forms[0] is an array which looks for the first form it comes across because everything starts at [0].
submit() is a function built into the javascript model and can be called in a number of different ways. In this instance its going to submit form[0].By stringing them together you create little tasks or functions

10-24-2007, 04:37 PM

Thanks for the quick reply and the brief lesson on the onchange= command.

So no separate javascript is needed for this to work? If that is the case then I must be doing something else wrong...I'll hammer on it some more.


10-24-2007, 07:23 PM
let me know when you have no more hair to pull out and we will go over it together

10-24-2007, 09:37 PM

Good News, I got it working after banging on it for a while. I did have to add this statement in the code though-

mysql_select_db($database_test, $test);

It's almost like this statement makes the connection to the database which I thought was done by this statement- <?php require_once('Connections/test.php'); ?>

Anyway, thanks for the help. I am going to try and expand this further by adding new tables like Price, Mileage, Year, etc... and see what I can come up with.

If I want to add different pics of cars to make it even more authentic, would you suggest adding the pics into mysql (BLOB format) or just copy the pics up to the server and refer to the pics in the code?


10-25-2007, 05:46 AM
store the path of the image in the db

is this not in the connections file?

mysql_select_db($database_test, $test);