如何将文本框值而不是“其他”提交到数据库

时间:2021-08-08 09:37:54

The following html code is part of a form, which sends the information to a java servlet and updates sql database. It is a dropdown menu. When the user selects "Other", javascript makes a textbox appear so that the user can specify more detail. However, when user presses submit button, in the database, it simply says "Other" instead of what the user entered in the text box. Help is appreciated, thanks!

以下html代码是表单的一部分,它将信息发送到java servlet并更新sql数据库。这是一个下拉菜单。当用户选择“其他”时,javascript会显示一个文本框,以便用户可以指定更多详细信息。但是,当用户按下提交按钮时,在数据库中,它只是显示“其他”而不是用户在文本框中输入的内容。感谢帮助,谢谢!

<script type="text/javascript">
    function showfield(name){
        if (name == 'Other')
            document.getElementById('div1').innerHTML = 'Please Specify: <input type="text" name="other" />';
        else 
            document.getElementById('div1').innerHTML = '';
    }
</script>

<select id="description" name="description" onchange="showfield(this.options[this.selectedIndex].value)" required>
    <option disabled selected value> ---Select--- </option>
    <option value="Accommodation">Accommodation</option>
    <option value="Travel">Travel</option>
    <option value="Meal Allowanced">Meal Allowances</option>
    <option value="Flat Rate">Flat Rate Expenses</option>
    <option value="Other">Other</option>
</select>
<div id="div1"></div>

2 个解决方案

#1


0  

I think the field you're checking on the server is "description" which is the name of your select. Which is indeed set to "Other" (this is the value of the selected option).

我认为您在服务器上检查的字段是“description”,这是您选择的名称。确实设置为“其他”(这是所选选项的值)。

I would have written it a bit differently but with the current code you need to name the input with some other name and check that one on your server, or change the selected option value.

我会写得有点不同但是使用当前代码,您需要使用其他名称命名输入并检查服务器上的那个,或更改所选的选项值。

Example change for client code:

客户端代码的示例更改:

<select id="description" name="description" required>
    <option disabled selected value> ---Select--- </option>
    <option value="Accommodation">Accommodation</option>
    <option value="Travel">Travel</option>
    <option value="Meal Allowanced">Meal Allowances</option>
    <option value="Flat Rate">Flat Rate Expenses</option>
    <option value="Other">Other</option>
</select>
<div id="div1">
    Please Specify: <input type="text" name="otherDescription" />
</div>

<script>
document.querySelector('#description').addEventListener('change', function (evt) {
    var inputWrapper = document.getElementById('div1'),
        input = inputWrapper.querySelector('input);
    if (evt.target.value == 'Other') {
        document.getElementById('div1').style.display = '';
    } else {
        document.getElementById('div1').style.display = 'none';
        input.value = '';
    }
});
</script>

The you need to check 'otherDescription' on the server.

您需要检查服务器上的“otherDescription”。

Hope it helps

希望能帮助到你

#2


0  

Assuming that all of this markup is contained within a <form> element that will be submitted to the server, you should be able to simply check to see if the value posted for description is "Other" and then read the posted value for other.

假设所有这个标记都包含在将提交给服务器的

元素中,您应该能够简单地检查为描述发布的值是否为“Other”,然后读取其他的发布值。

The code will vary depending on your server-side implementation.

代码将根据您的服务器端实现而有所不同。

#1


0  

I think the field you're checking on the server is "description" which is the name of your select. Which is indeed set to "Other" (this is the value of the selected option).

我认为您在服务器上检查的字段是“description”,这是您选择的名称。确实设置为“其他”(这是所选选项的值)。

I would have written it a bit differently but with the current code you need to name the input with some other name and check that one on your server, or change the selected option value.

我会写得有点不同但是使用当前代码,您需要使用其他名称命名输入并检查服务器上的那个,或更改所选的选项值。

Example change for client code:

客户端代码的示例更改:

<select id="description" name="description" required>
    <option disabled selected value> ---Select--- </option>
    <option value="Accommodation">Accommodation</option>
    <option value="Travel">Travel</option>
    <option value="Meal Allowanced">Meal Allowances</option>
    <option value="Flat Rate">Flat Rate Expenses</option>
    <option value="Other">Other</option>
</select>
<div id="div1">
    Please Specify: <input type="text" name="otherDescription" />
</div>

<script>
document.querySelector('#description').addEventListener('change', function (evt) {
    var inputWrapper = document.getElementById('div1'),
        input = inputWrapper.querySelector('input);
    if (evt.target.value == 'Other') {
        document.getElementById('div1').style.display = '';
    } else {
        document.getElementById('div1').style.display = 'none';
        input.value = '';
    }
});
</script>

The you need to check 'otherDescription' on the server.

您需要检查服务器上的“otherDescription”。

Hope it helps

希望能帮助到你

#2


0  

Assuming that all of this markup is contained within a <form> element that will be submitted to the server, you should be able to simply check to see if the value posted for description is "Other" and then read the posted value for other.

假设所有这个标记都包含在将提交给服务器的

元素中,您应该能够简单地检查为描述发布的值是否为“Other”,然后读取其他的发布值。

The code will vary depending on your server-side implementation.

代码将根据您的服务器端实现而有所不同。