2

I have a problem with creating an script which will fill one combobox on base of what is selected in the other combobox.

I am newbie in the field of PHP / JS so please be patient with me. Here it is what I have done so far:

js script which is on click event

<script type="text/javascript">
    function selectDropdown(){
        var dropdownValue=document.getElementById("dropdown").value;
        $field=dropdownValue;
    }
</script>

Base combobox filled with values from database:

<select id="dropdown" name="dropdown" onchange="selectDropdown()">
    <option value="dd">--take an option--</option>
    <?
    $standard = Env::value('standard');
    if (!$status)
        $statement = "select code from standard";
    foreach ($db->query($statement )->fetchall() as $row)
        echo "<option".($row == $standard ? ' selected ' : '').">$row[0]</option>";



    ?>
</select>

and now I want to put the selected option into other combobox - actually I will execute another SQL query with the value chosen previously but the fact that I will have value in variable is more then enough for me to finish the task.

<select name='st' class='txt'>
  <option value="st"><? echo $field; ?></option>
</select>

Posted code is created based on one of the articles I found, I have remake it according to my needs but this does not work. Could anyone point me where I am doing mistake?

1 Answer 1

2

You can't set php variable from javascript, this will not work

var dropdownValue=document.getElementById("dropdown").value;
$field=dropdownValue;
...
<option value="st"><? echo $field; ?></option>

you can do two things use ajax to generate other select or use pure javascript, using jQuery it will be something like:

$('option[value=st]').text(dropdownValue);
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome jcubic! It looks that I have so much to learn about js! Thanks for your help! (i will make this as an answer as soon as stackoverflow let me!)

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.