通过java脚本存储表单变量

Storing form variables through java script

本文关键字:表单 变量 存储 脚本 java 通过      更新时间:2023-09-26

嗨,我已经通过javascript制作了一些文本字段。但是我不知道如何在提交表单时将它们存储到数据库中。下面是代码

<script type="text/javascript">
    function showfield(name){
      if(name=='Other') {
        document.getElementById('div1').innerHTML='Other: <input type="text" name="other" />';
        document.getElementById('div2').innerHTML='';
      }
      else if(name=="School") {
        document.getElementById('div1').innerHTML='Name of the School : <input type="text" name="school" />'; 
        document.getElementById('div2').innerHTML='Contact Number : <input type="text" name="contact" />'; 
      }
      else {
       document.getElementById('div1').innerHTML='';
       document.getElementById('div2').innerHTML='';
     }
   }
 </script>
<html>
<select name="how" class="subtitle" id="select" onchange="showfield(this.options[this.selectedIndex].value)" style="width: 200px; height:30px;">>
                          <option selected="true" style="display:none;">Please select</option>
                          <option>School</option>
                          <option>Consultant</option>
                          <option>WhatApp</option>
                          <option>Brochure / Poster</option>
                          <option>Internet</option>
                          <option>Other</option>
                        </select></td>
</html>

在上面的html中,当选择'School'或'Other'选项时,它将显示更多来自javascript的文本字段。但是我不能存储文本字段的值。但所有其他选项都被插入到数据库中。

从html字段中获取值可以通过多种方式完成,这只是其中之一:

<form onsubmit='onSubmit();'>
<input type="text" id="variableName" />
<input type="submit" value="submit" />
</form>
<script type='text/javascript'>
function onSubmit(){
var val = document.getElementById('variableName').value;
console.log(val);
}
</script>

这样可以得到用户输入的内容。

这是你想要达到的目标吗?