gpt4 book ai didi

javascript - 当我的 html 中的一列有两个不同的名称时,如何在数据库中插入数据?

转载 作者:行者123 更新时间:2023-11-30 09:50:21 24 4
gpt4 key购买 nike

我知道我的标题有点令人困惑,所以我会在这里进一步解释。我知道如何在数据库中插入数据,但是当我的 HTML 中有两个不同的名称应该插入到我的数据库中时,我不知道该怎么做。

我有一个国家/地区下拉列表,选择名称country1。但是当用户选择“其他”时,文本框将出现在名称为 country2 的一侧,他应该输入他的国家。我希望当他选择“其他”并输入数据时,数据将插入到数据库中,或者当他从下拉列表中选择时,数据也会插入。

当用户选择“其他”时,我希望他在文本框中输入的数据将是要插入到数据库中的数据,而不是“其他”一词,当他从列表中选择“其他”以外的数据时,我希望他选择的数据将被插入到数据库中。

请帮帮我。如果我的问题不够清楚,请告诉我。谢谢。

我的javascript代码

function ValidateCountry(val){
var element=document.getElementById('country');
if(val==='Other')
element.style.display='block';
else
element.style.display='none';
}

HTML代码

<form action="send.php" method="post">
<select name="country1" onchange='ValidateCountry(this.value);'>
<option> Select Country</option>
<option value="USA"> Lawyer </option>
<option value="Japan"> Nurse </option>
<option value="Other"> Other </option>
</select>
<input type="text" name="country2" id="country" style='display:none;'/>
<input type="submit" name="submit" />
</form>

还有我的 PHP 代码:

include 'dbcontroller.php';

if(isset($_POST['submit']))
{
$country = $conn->real_escape_string(trim($_POST['country1']));
}
$query = "INSERT INTO info(occupation) VALUES ('$occupation')";
mysqli_query($conn,$query);

最佳答案

很简单,使用 if/else

if($_POST['country1'] == 'Other')
{
$country = $_POST['country2'];
} else {
$country = $_POST['country1'];
}

或者用一行:

$country = $_POST['country1'] == 'Other' ? $_POST['country2'] : $_POST['country1'];

然后您可以在查询中使用 $country

关于javascript - 当我的 html 中的一列有两个不同的名称时,如何在数据库中插入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36894455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com