PDA

View Full Version : Problem with Dropdown tutorial


iainb
08-30-2007, 09:31 PM
Hi again.

I'm having a problem with this one. I have read other threads on this topic and cant get an answer. As far as I can see the code is the same as yours apart from another @ to stop a warning being echo'd...

The second dropdown is not being populated when the page is submitted, I will post the code so you can see. Any help is greatly appreciated.


<?php
require_once("Connections/connection.php");
$make = $_POST['make'];
if ($make){
/////////////////////////////////////////////////
$query = sprintf("SELECT * FROM car_model where car_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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<select name="make" onChange="document.forms[0].submit()">
<option value="" selected> Select Make</option>
<option value="1">Alfa Romeo</option>
<option value="2">Audi</option>
<option value="3">BMW</option>
<option value="4">Citroen</option>
</select>
<select name="model">
<option value="">Select Model</option>
<?php do { ?>
<option value="<?php echo $rowModel['car_model_id']; ?>"><?php echo $rowModel['car_model']; ?></option>
<?php } while ($rowModel = mysql_fetch_array($result)); ?>
</select>
</form>
</body>
</html>



Thanks for any help you can give.

iain

davidj
08-30-2007, 10:46 PM
does the form submit when you change the value

iainb
08-30-2007, 10:57 PM
hi,

yeah the form submits fine, but then both fields stay as they were.... car make dropdown populated, and nothing in the model dropdown

iain

davidj
08-31-2007, 09:20 AM
instead of this...
onChange="document.forms[0].submit()"
try this...

onChange="document.submit()"

iainb
08-31-2007, 11:05 AM
hi,

ok, I tried the ammended code you gave me... That didnt work either.

This time when I chose something from the "make" dropdown, it no longer submits the page as it did with the original code, so obviously doesnt populate the second dropdown.

iain

davidj
08-31-2007, 01:25 PM
ok lets debug this

where you have


$query = sprintf("SELECT * FROM car_model where car_model_id='$make'");

add an echo to the begining

echo $query = sprintf("SELECT * FROM car_model where car_model_id='$make'");

now run the script by selecting a dropdown value again

the browser will echo the SQL and may display a warning which is fine as its just reporting its done something wrong which we know about

paste the SQL in this thread

iainb
08-31-2007, 03:38 PM
hi

ok, done that and yes there is an error.....

Parse error: syntax error, unexpected T_ECHO in C:\wamp\www\Web Root\Test\dropdown.php on line 8

iain

iainb
08-31-2007, 04:29 PM
line 8 is the......

echo $query = sprintf("SELECT * FROM car_model where car_model_id='$make'");

by the way

davidj
08-31-2007, 05:28 PM
ok
thats weird

try this... Add the echo below and remove the one you had

where you have.....
$query = sprintf("SELECT * FROM car_model where car_model_id='$make'");
$result = @mysql_query($query);
$rowModel = @mysql_fetch_array($result);

echo $query;

iainb
08-31-2007, 07:40 PM
ok, tried that....

the page submits but the second dropdown is still not being populated... it returns the $query as...

SELECT * FROM car_model where car_model_id='1'

I just checked the database, and the fields are definately using those names (as in video)... and it only uses ID's 1 - 4

cheers

iain

davidj
08-31-2007, 07:46 PM
OK its passing the value to the select which is what that echo informed us so we know the form is submitting

try this...

where you have in your do while loop

<?php } while ($rowModel = mysql_fetch_array($result)); ?>

replace $rowModel with $row

<?php } while ($row = mysql_fetch_array($result)); ?>

iainb
08-31-2007, 08:56 PM
ok done that and its still echo'ing the same query back not populating

SELECT * FROM car_model where car_model_id='4'

cheers
iain

davidj
08-31-2007, 09:10 PM
yes try again but remove the echo

iainb
08-31-2007, 09:35 PM
yeah... tried that now but it just submits the form, doesnt populate the second dropdown and doesnt show any echo's

just to refresh... this is the code I have now


<?php
require_once("Connections/connection.php");
$make = $_POST['make'];
if ($make){
/////////////////////////////////////////////////
$query = sprintf("SELECT * FROM car_model where car_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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<select name="make" onChange="document.forms[0].submit()">
<option value="" selected> Select Make</option>
<option value="1">Alfa Romeo</option>
<option value="2">Audi</option>
<option value="3">BMW</option>
<option value="4">Citroen</option>
</select>
<select name="model">
<option value="">Select Model</option>
<?php do { ?>
<option value="<?php echo $rowModel['car_model_id']; ?>"><?php echo $rowModel['car_model']; ?></option>
<?php } while ($row = mysql_fetch_array($result)); ?>
</select>
</form>
</body>
</html>


cheers

iain

davidj
08-31-2007, 10:40 PM
i am going to have to build this myself and go over your code

replace

action="<?php $_SERVER['PHP_SELF']; ?>">

with the page name as the action

davidj
08-31-2007, 10:52 PM
have you followed the tutorial to the letter?

iainb
08-31-2007, 10:52 PM
same again unfortunately, submits but doesnt populate...

thanks for taking the time to help me with this, I really appreciate it.

iain

davidj
08-31-2007, 10:53 PM
post the code from your connections file

iainb
08-31-2007, 10:56 PM
<?php
///////////////////////////
$database = "iaintestdb";
$username = "iain";
$password = "passblah";
//////////////////////////
$link = @mysql_connect("localhost", $username, $password);
$db = mysql_select_db($database, $link);
?>



its strange, because I'm linking to the same connection file on all the other tutorials, including the user login one after this, and they all call the data fine.

davidj
08-31-2007, 11:49 PM
please compare the name of your database table and that of the table your querying in your SQL!!!!!

attention to detail is the key

iainb
08-31-2007, 11:58 PM
oops :roll: that fixed the problem....

Sorry about that... just trying to keep you on your toes obviously :wink:

Cheers

iain

davidj
09-01-2007, 12:00 AM
you have battered my head

i was bouncing it off the wall with that one

iainb
09-01-2007, 12:04 AM
yeah, I can imagine... been doing my head in a bit too.

I could have sworn I checked that, I'll bear this in mind.:-D

thanks again