Javascript 通过java脚本存储表单变量

Javascript 通过java脚本存储表单变量,javascript,html,Javascript,Html,嗨,我通过javascript制作了一些文本字段。但我不知道在提交表单时如何将它们存储到数据库中。这是密码 <script type="text/javascript"> function showfield(name){ if(name=='Other') { document.getElementById('div1').innerHTML='Other: <input type="text" name="other" />';

嗨,我通过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>

函数显示字段(名称){
如果(名称=='Other'){
document.getElementById('div1').innerHTML='Other:';
document.getElementById('div2')。innerHTML='';
}
否则,如果(名称==“学校”){
document.getElementById('div1').innerHTML='学校名称:';
document.getElementById('div2')。innerHTML='Contact Number:';
}
否则{
document.getElementById('div1')。innerHTML='';
document.getElementById('div2')。innerHTML='';
}
}
>
请选择
学校
顾问
什么
小册子/海报
互联网
其他

在上面的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>

函数onSubmit(){
var val=document.getElementById('variableName')。值;
控制台日志(val);
}
这种方法可以获取用户在输入字段中键入的内容


这就是你想要实现的吗?希望这对您有所帮助。

您首先需要表单、服务器、服务器端语言和数据库。我只看到了您提供的客户端代码,我不能用提供的信息说明任何关于后端的内容。您缺少了写入数据库的部分,正如您所说的“但是所有其他选项都被插入数据库”。这部分在哪里?首先,这是无效的html代码(例如),其次,这里没有表单元素和提交按钮,这是你的全部代码吗?表单标签在哪里?提交发生在哪里?是的,我拥有一切,我使用php mysql,我只想知道如何从javascript生成的文本字段中获取值。这不是全部代码