带有提交按钮的动态下拉列表 - test.php?index = $ value

时间:2017-02-15 22:14:48

标签: javascript php jquery

我想要一种方法将用户选择的选项存储在变量中。

感谢

$results = $handle->query("SELECT * FROM id
 WHERE test  = '".$pull ."';");

echo "<html>";
echo "<body>";
echo "<select name='id'>";
   while($table = $results->fetchArray()){
      $table_= $table['id'] -1;
      $b = "1" . $table_ ;
      echo '<option value="'.$table_.'">'.$table_.'</option>';
   }
echo "</select>";
echo "</body>";
echo "</html>";
?>

1 个答案:

答案 0 :(得分:0)

您需要使用JavaScript来完成任务 首先,您必须在DOM中找到您的选择,例如:
const yourSelector = document.querySelector('<your_selector>'); //and perhaps we need to check form const form = querySelector('form');
当用户点击提交按钮时,你必须阻止事件,如下所示:
    const submitButton = document.querySelector('#<id_of_button>'); submitButton.addEventListener('click', (e) => { e.preventDefault(); const selectedValue = yourSelector.options[selector.selectedIndex].value form.action = 'test.php?index=${selectedValue}'}
而不是提交表格。请注意,在设置form.action时,我们在tylda`(非单引号)中使用符号的ES6语句。