0

index.php Hello community. I am using a dropdown list populated from mySQL using AJAX. I got this script as my base from a website that a member of this community suggested and you can see the demo here

In the demo there are 3 dropdowns and I am trying to make it 4, without luck as you can see in the screenshot here

enter image description here

I can not get the 2010 and pass it to the year so the 4th dropdown to show the selected rows, so it is shown as undefined.

One note though, if from the file findEngine.php remove the AND model_year='$yearId' and leave it with only 1 AND, it shows all the rows found for AUDI, not with the selected model or year of course.

I added the source codes below and the index.php to an external service because of it's length.

Any help is appreciate for this, I guess it is a beginners problem but I can not see it..

index.php http://codepad.org/k3n7HJ4G

findModel.php

    <? 
    $make=$_GET['make'];
    mysql
    $query="SELECT distinct model_name FROM cars WHERE model_make_id='$make' order by model_name asc";
    $result=mysql_query($query);
    ?>
    <select name="model" onchange="getyear('<?=$make?>',this.value)">
    <option>Select Model</option>
    <? while($row=mysql_fetch_array($result)) { ?>
    <option value=<?=$row['model_name']?>><?=$row['model_name']?></option>
    <? } ?>
    </select>

findYear.php

<? $makeId=$_GET['make'];
$modelId=$_GET['model'];
echo $modelId;
mysql
$query="SELECT distinct model_year FROM cars WHERE model_make_id='$makeId' AND model_name='$modelId' order by model_year asc";
$result=mysql_query($query);
?>
<select name="year"  onchange="getEngine('<?=$makeId?>',this.value)">
<option>Select year</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$modelId?>><?=$row['model_year']?></option>
<? } ?>
</select>

findEngine.php

<? $makeId=$_GET['make'];
$modelId=$_GET['model'];
$yearId=$_GET['year'];
echo $yearId;
mysql
$query="SELECT distinct model_trim FROM cars WHERE model_make_id='$makeId' AND model_name='$modelId' AND model_year='$yearId' order by model_trim asc";
$result=mysql_query($query);
?>
<select name="engine">
<option>Select year</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value><?=$row['model_trim']?></option>
<? } ?>
</select>
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
  • You should follow the advice from your [own question](http://stackoverflow.com/questions/7175948/do-prepare-statements-secure-your-database) and use parameterized queries. You're completely vulnerable to SQL injection. – Mike Feb 13 '14 at 02:11
  • Also, trying to pinpoint where the error occurs would help a lot. Posting ~200 lines of code in total is not going to get your question answered very quickly. – Mike Feb 13 '14 at 02:13

1 Answers1

0

As per the definition of getEngine function, it takes 3 parameters but you are passing only 2.

Change below line in findEngine.php

<select name="year"  onchange="getEngine('<?=$makeId?>',this.value)">

To,

<select name="year"  onchange="getEngine('<?=$makeId?>', '<?=$modelId?>', this.value)">

Also, avoid using mysql extension as it is deprecated in PHP 5.5.0. Read http://www.php.net/manual/en/intro.mysql.php for the details and alternatives.

Ramesh
  • 4,223
  • 2
  • 16
  • 24