来自html选择列表的POST值?

时间:2017-05-18 10:54:48

标签: php

我正在尝试向insert-event.php发送一些信息。除了SELECT中列出的选项外,每一条数据都会通过。

     <form action="php/insert-event.php" method="post">
        <select id="skoleDropdown" name="skole"><option hidden>Velg skole</option>"' . $skoleAttributter . '" </select>
        <input type="text" name="Title" placeholder="Tittel" class="ico-title" required></input>
        <input type="text" name="description" placeholder="Beskrivelse" class="ico-title" required></input>
        <input type="text" name="pris" placeholder="Pris i NOK" class="ico-title" required></input>
        <input type="date" name="date" class="ico-title" required></input>
        <input type="text" name="img_url" placeholder="Bildelenke" class="ico-title" required></input>
        <input type="text" name="type" placeholder="Type arrangement" class="ico-title" required></input>
        <input type="submit" name="submit" value="LEGG TIL"></input>
     </form>
  </div>';

然后我有我的insert-event.php:

<?php
$title = $_POST['Title'];
$pris = $_POST['pris'];
$date = $_POST['date'];
$url = $_POST['img_url'];
$description = $_POST['description'];
$school = $_POST['skole'];
$schoolId = '';
$type = $_POST['type'];

switch($school) {
    case 'Campus Brennerviveien';
    $schoolId == 1;
    break;

case 'Campus Vulkan';
    $schoolId == 2;
    break;

case 'Campus Fjerdingen';
    $schoolId == 3;
    break;
}

// Connect and select DB
$connect =  mysqli_connect('localhost', 'chris', 'chris');
if (!$connect) {
    echo 'Not connected';
}

if (!mysqli_select_db($connect, 'eksamen')) {
    echo 'Database not selected';
}

// Submit
$sql = "INSERT INTO events (id, title, description, pris, img_url, date, type, skole_id)
  VALUES (NULL, '$title', '$description', '$pris', '$url', '$date', '$type', '$schoolId')";

if ($connect->query($sql) === TRUE) {
    header("Location:../index.php");
    exit;
} else {
    echo "Error: " . $sql . "<br>" . $connect->error;
}
$connect->close();

?>

我想也许这说明了一切。

感谢帮助。

谢谢!

3 个答案:

答案 0 :(得分:0)

您必须使用select属性的value属性在帖子页面上发送数据

  <select id="skoleDropdown" name="skole">
<option hidden value="Velg skole">Velg skole</option>"' . $skoleAttributter . '" 
</select>

然后这应该像

<form action="php/insert-event.php" method="post">
        <select id="skoleDropdown" name="skole"><option hidden value="Velg skole">Velg skole</option>"' . $skoleAttributter . '" </select>
        <input type="text" name="Title" placeholder="Tittel" class="ico-title" required></input>
        <input type="text" name="description" placeholder="Beskrivelse" class="ico-title" required></input>
        <input type="text" name="pris" placeholder="Pris i NOK" class="ico-title" required></input>
        <input type="date" name="date" class="ico-title" required></input>
        <input type="text" name="img_url" placeholder="Bildelenke" class="ico-title" required></input>
        <input type="text" name="type" placeholder="Type arrangement" class="ico-title" required></input>
        <input type="submit" name="submit" value="LEGG TIL"></input>
     </form>
  </div>';

然后这将是这样的

$skoleListe = Skole::all(); 
$skoleAttributter = ''; 
foreach($skoleListe as $skole) 
{
 $skoleAttributter.= '<option value="' .$skole['id'] . '">' . $skole['navn'] . '</option>'; 
} 

你已添加&#34;&#34;这个双倍时间从选项值attibute中删除一个

答案 1 :(得分:0)

额外双引号问题因此值设置为空

$skoleAttributter.= '<option value=""' .$skole['id'] . '">' . $skole['navn'] . '</option>';
                                   ^^^

更改为

 $skoleAttributter.= '<option value="' .$skole['id'] . '">' . $skole['navn'] . '</option>';
                                  ^^^^

1)并且不需要切换案例,因为您的设置ID为值属性。所以你可以直接用它作为schoolId

  

尝试使用预处理语句或pdo来避免sql注入。

答案 2 :(得分:0)

正确的选择方法是

<select name="skole" id="skole">
<option value="test"> option one text here </option>
<option value="test2"> option two text here </option>
</select>

当您的选择要求时

$school = $_POST['skole']; ====> $school = 'test'

如果选择了选项,那么它将为您提供选项的值,否则它不会给出任何值。

感谢