0

I need to select an id depending on another id. I made by myself something but it is not working, so I am posting the CODE, maybe i have something wrong in it. Select the first option a vehicle, and in the second option select a model of the vehicle that I selected. Thanks!

//The config file

<?php

$host = 'localhost';         
$utilizator = 'stud';         
$parola = 'stud';         
$numebd = 'autodealer';      

$conn = mysql_connect($host, $utilizator, $parola);
if (!$conn) { echo '<h4>Connected!</h4>'; }

if (!mysql_select_db($numebd, $conn)) { 
   echo '<h4>Couldnt connect database : '. mysql_errno(). ' : '. mysql_error().'</h4>'; 
}

mysql_set_charset('utf8', $conn);
?>


//The HTML code

<h2><strong>Vehicle</strong> data</h2>
    <div class="select_wrapper">
    <form action="" method="post" name="anunt" id="f_anunt">
        <label><span>* </span><strong>Manufacturer:</strong></label>
        <select class="select_5" name="marca" id="marca">                           
            <?php
                $result = mysql_query("SELECT id_marca, denumire FROM marci ORDER BY ordine");
                while($row = mysql_fetch_array($result)) {
                    echo ("<option value='".$row['id_marca']."'>".$row['denumire']."</option>");
                }
            ?>
        </select>
        </div>
        <div class="select_wrapper">
        <label><span>* </span><strong>Model: </strong></label>
            <select class="select_5" name="model" id="model">
                <?php                                   
                    $result = mysql_query("SELECT id_model, denumire FROM modele WHERE id_marca=1");
                    while ($row = mysql_fetch_array($result)) {
                        echo ("<option value='".$row['id_model']."'>".$row['denumire']."</option>");
                    }
                ?>
            </select>
        </div>

//The PHP file

<?php
include_once("config.php");

$id_marca = $_POST['id_marca'];

$sir_sql="SELECT id_model, denumire FROM modele WHERE id_marca=$id_marca ";

if ($conn())
    {               
        $result=mysql_query($sir_sql,$conn);

        while ($row = mysql_fetch_array($result))       
           echo ("<option value='".$row['id_model']."'>".$row['denumire']."</option>");

    }
    else 
        die('Comanda incorecta:<br> ' . mysql_error()); 
?>

//The Javascript code

$("#marca").change(get_model);

    function get_model(e)
{
    $.post("ajax_get_model.php",{id_marca:$(this).val()},function(data,status)
        {   
            $("#model").html(data); // pun modelele 
        });
}
2
  • 1
    Beware, as it stands this contains a possible SQL Injection attack. Commented Feb 3, 2014 at 9:31
  • 1
    It is local, no worries! Commented Feb 3, 2014 at 11:59

1 Answer 1

1

There are several post on SO about this topic. Please, read these topics first, hope these will help-

  1. Populate one dropdown list based on the selection of other dropdown list
  2. jQuery show/hide drop-down options based on another drop-down option

Finally, you can check the tutorial here to populate dropdown based on dropdown selection...

Tutorial Here | Demo Here

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! That was very helpful. The problem was in the PHP file.. if ($conn) instead of if ($conn()) .
Yeah, sure. And I like your links. Made me understand much more about ajax.
As soon as I get 15 reputation, that is the condition I guess!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.