Viorel WWI
Viorel WWI

Reputation: 81

php dynamic dropdown menu get value

i am trying to do a dynamic dropdown menu, i manage to retrieve the first menu value but i can't manage to retrieve the second menu value.

HTML part

<div>
        <label for="marca">Marca </label>
        <select type="text" id="marca" name="marca" onChange="getModel()">
        <option value="">Alege Marca</option>

        <?php    while($row = mysqli_fetch_assoc($resultMarca)){ ?>

            <option value="<?php echo $row["id"]  ?>"> <?php echo $row["nume_marca"]  ?> </option>

            <?php  } ?>


        </select>


    </div>

    <div id="model_masina">
        <label for="model">Model </label>
        <select id="model" nume="model">

        <option value="">Alege Model</option>

        </select>

    </div>

Ajax Part

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script type ="text/javascript">

    function getModel(){

       var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET","get_model.php?marca="+document.getElementById("marca").value, false);
        xmlhttp.send(null);
        document.getElementById("model_masina").innerHTML=xmlhttp.responseText;

    }

    function model_schimba(){
         $modelSc = (document.getElementById("model").value);


       }

</script>

PHP

?>
        <label for="model">Model </label>
        <select id="model" nume="model" onchange='model_schimba()'>
        <option value="">Alege Model</option>

<?php
    while($row = mysqli_fetch_array($res)){ ?>


            <option value="<?php echo $row["id"] ?>"> <?php echo $row["name"]  ?> </option>

            <?php  } 
        ?> </select> <?php 


} 

i mange to take the variable here $modelSc = (document.getElementById("model").value);

but when i push the submit button i can't reach the variable $model = $_POST["model"];

Upvotes: 0

Views: 168

Answers (2)

bowl0stu
bowl0stu

Reputation: 350

First, code separation is important for readability. Second, I think your AJAX return

document.getElementById("model_masina").innerHTML=xmlhttp.responseText;

is mistakenly pointing at a <div> container instead of the <select> list. Should be,

document.getElementById("model").innerHTML=xmlhttp.responseText;

because you are outputting select menu <option>

Upvotes: 0

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

"but when i push the submit button i can't reach the variable $model = $_POST["model"];"

nume="model"

PHP syntax is English-based, not in your language.

You need to change it to name="model".

The "name" attribute is the same in any language.

Having use PHP's error reporting, it would have thrown you an undefined index notice.

Upvotes: 1

Related Questions