【问题标题】:Select tag not able to collect all values in HTML选择标签无法收集 HTML 中的所有值
【发布时间】:2016-06-16 13:37:56
【问题描述】:

I have an HTML form where select tag is used for the inputs, with 4 options where in the last one is named as other.When selected other a text field comes and the input can be given:

HTML 表单:

<html>
<head>
    <script type="text/javascript">
    $('select').on('change', function(){
    if($(this).val()=='Other:'){
    $('#other').show().focus();
    }else{
    $('#other').val('').hide();
    }
    })
    </script>
</head>
<body>
<tr>
  <td>
    <label>Options : </label>
  </td>
  <td>
    <select name="Options">
    <option value="Option-1">Option-1</option>
    <option value="Option-2">Option-2</option>
    <option value="Option-3">Option-3</option>
    <option value="Other:">Other</option>
    </select>
    <input type="text" id="other" style="display:none" name="other"  style="display:none"/>
  </td>
</tr>
</body>
</html>

在数据库中插入时,我只能插入在文本字段中添加的值。在选项 1、2、3 中没有被收集并插入到 DB 中的位置。

在 mysql DB 中插入值的 PHP 代码:

<?php
$link = mysqli_connect("localhost", "root", "", "database");
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
$Options = mysqli_real_escape_string($link, $_POST['Options'], $_POST['other']);
$sql = "INSERT INTO Table (Options) VALUES ('$Options');
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>

我想收集曾经被选中并插入数据库的选项。目前只能插入通过文本字段输入数据的其他选项。

提前致谢。

【问题讨论】:

  • 您在寻找选项列表数组吗??如果是,codepen url -codepen.io/nagasai/pen/QEEqbx
  • 尝试使用单独的转义字符串 $Options = mysqli_real_escape_string($link, $Options); $other = mysqli_real_escape_string($link, $other);
  • @Gopal..如果我使用两个单独的转义字符串,插入语句将如何......?
  • @Naga Sai...我希望收集曾经选择的选项。但在转义字符串中,除了其他选项(文本字段)之外,它不会被收集。
  • 对于选定的选项,使用 document.getElementById('optionsList').value....如果您将属性 id 及其值添加为 "optionsList"- 到选择标签

标签: php html mysql


【解决方案1】:

根据通过选择列表给出的输入,使用 if else 在值之间切换。

if($_POST["Options"] !== "Other:"){
    $Options = mysqli_real_escape_string($link, $_POST['Options']);
}else{
    $Options = mysqli_real_escape_string($link, $_POST['other']);
}
$sql = "INSERT INTO Table (Options) VALUES ('$Options')";
// ...

【讨论】:

    猜你喜欢
    • 2022-06-11
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多