I did a form which refers to another one, and returns a variable
. I would to use this variable to select by default the <option>
with the ID
(or the name) equal to it in the <select>
tag that I put in the second form.
For example, if the form_1 returns type_option = option2, I want to put <option value="option2" selected>Option2</option>
.
I suppose that I have to use javascript, but I don't know anything about it.
I tried with this code but it doesn't work.
<?php
echo '<form id="form_1"><input type="hidden" name="type_option" value="option1"><br>';
echo '<input type="submit" value="Submit"></form>';
if (isset($_POST['type_option']))
{
echo '<form id="form_2">';
echo '<script>$(\'select[name="options"]\').find(\'option[value="'.$type_option.'"]\').attr("selected",true);</script>';
echo '<select id="type" name="type">';
echo '<option value="option1">Option1</option>';
echo '<option value="option2">Option2</option>';
echo '</select>';
echo '</form>';
}
?>
Thank you for your help.