PDA

View Full Version : question about onchange=...


jewellman
10-24-2007, 03: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:

Jimmy

P.S. Here is the entire code-


<?php
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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<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>

<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)); ?>
</select>

</form>

</body>
</html>

davidj
10-24-2007, 05: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)

onchange="document.forms[0].submit()"

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.forms[0].submit()

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

jewellman
10-24-2007, 05:37 PM
David-

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.

Jimmy

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

jewellman
10-24-2007, 10:37 PM
David-

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

<?php
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?

Thanks,
Jimmy

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

is this not in the connections file?

mysql_select_db($database_test, $test);