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

时间:2016-04-19 20:23:11

标签: php html mysql select option

我有一个表单,我可以获得一些不同的信息。但后来我有一个选择多个选项,我希望能够以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,具体取决于是否选择了替代方案。

2 个答案:

答案 0 :(得分:2)

您的select html代码需要具有name属性才能将表单onSubmit发送到“后端”。

像这样:

<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>

答案 1 :(得分:1)

您的<select>多重可选下拉列表必须是由name="NameThatMakesSense[]"定义的数组

<select name="NameThatMakesSense[]" multiple>

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

然后您需要预处理NameThatMakesSense数组以从多选下拉列表中选择实际选择的值。

$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'])}')";