使用 POST 从 HTML 表单中获取 select 选项值

Using POST to get select option value from HTML form

我有一个表格,可以从中获取一些不同的信息。但是后来我有一个 Select 有多个选项,我希望能够以 1 或 0 / true 或 false 将其发送到数据库。但它不起作用。到目前为止,这是我的代码:

              <select name="multipleSelect[]" multiple>
                <option value="" disabled selected>Choose your option</option>
                <option name="esea" value="esea">ESEA</option>
                <option name="faceit" value="faceit">FaceIT</option>
                <option name="matchmaking" value="matchmaking">Matchmaking</option>
              </select>
              <label>What are you looking to play?</label>

这是我的 php:

$esea = 0;
$faceit = 0;
$matchmaking = 0;
foreach ( $_POST['multipleSelect'] as $value ) {
    if ( $value == 'esea' )        { $esea = 1;       }
    if ( $value == 'faceit' )      { $faceit= 1;      }
    if ( $value == 'matchmaking' ) { $matchmaking= 1; }
}   


  // Sätt in dataN
  $sql = "INSERT INTO users ( steamid, profilename, profileurl, avatar, region, age, ranks, esea, faceit, matchmaking, textarea1 ) VALUES ( 
    '{$mysqli->real_escape_string($_POST['steamid'])}', 
    '{$mysqli->real_escape_string($_POST['profilename'])}',
    '{$mysqli->real_escape_string($_POST['profileurl'])}',
    '{$mysqli->real_escape_string($_POST['avatar'])}',
    '{$mysqli->real_escape_string($_POST['region'])}',
    '{$mysqli->real_escape_string($_POST['age'])}',
    '{$mysqli->real_escape_string($_POST['ranks'])}',
    $esea,
    $faceit,
    $matchmaking',
    '{$mysqli->real_escape_string($_POST['textarea1'])}')";
  $insert = $mysqli->query($sql);

我知道这段代码不起作用,但我不知道该怎么做才能让它起作用。我不想发送 1 或 0,具体取决于是否选择了替代方案。

您的 select html 标记需要具有 name 属性,以便表单 onSubmit 发送到 "backend"。

像这样:

<select name="enterNameThatMakesSense" multiple>  // <----- HERE
  <option value="" disabled selected>Choose your option</option>
  <option name="esea" value="1">ESEA</option>
  <option name="faceit" value="1">FaceIT</option>
  <option name="matchmaking" value="1">Matchmaking</option>
</select>
<label>What are you looking to play?</label>

您的 <select> 多个 select 可用下拉列表需要是一个数组,由 name="NameThatMakesSense[]"

定义
<select name="NameThatMakesSense[]" multiple>

这将为您 return 一个名为 $_POST['NameThatMakesSense'] 的字段,它本身是一个包含 1 个或多个值的数组,指示下拉列表中的哪些项目被 selected。来自 <option> 标签的 value="" 属性

然后您将需要预处理 NameThatMakesSense 数组以从多 select 下拉列表中选取哪些值实际上是 selected。

$esea = 0;
$esea = 0;
$esea = 0;
foreach ( $_POST['NameThatMakesSense'] as $value ) {
    if ( $value == 'esea' )        { $esea = 1;       }
    if ( $value == 'faceit' )      { $faceit= 1;      }
    if ( $value == 'matchmaking' ) { $matchmaking= 1; }
}    

$sql = "INSERT INTO users 
             ( profilename, profileurl, avatar, region, 
               age, esea, faceit, matchmaking, textarea1 ) 
        VALUES ( 

'{$mysqli->real_escape_string($_POST['profilename'])}',
'{$mysqli->real_escape_string($_POST['profileurl'])}',
'{$mysqli->real_escape_string($_POST['avatar'])}',
'{$mysqli->real_escape_string($_POST['region'])}',
'{$mysqli->real_escape_string($_POST['age'])}',
'{$mysqli->real_escape_string($_POST['ranks'])}',
$esea,
$faceit,
$matchmaking,
'{$mysqli->real_escape_string($_POST['textarea1'])}')";