从表单发送选择值到php页面

时间:2016-01-06 08:46:42

标签: php html

我有一个带有固定值的选择框的html页面。我需要将这些值添加到我的php页面来更新我的数据库。它适用于带有输入字段的表单。你能看看我的脚本并告诉我我做错了吗?

html选择表单:

<form action="opendicht.php" method="post">
        <select name="opendicht[]">
            <option name="open" value="open">open</option>
            <option name="dicht" value="dicht">dicht</option>
        </select>

        <input type="submit" value="Doorvoeren">
</form> 

PHP页面:

<?php
error_reporting(E_ERROR);
include '../dbconnect.php';
//$connection = mysql_connect("", "", ""); // Establishing Connection with Server
//$db = mysql_select_db("cerar", $connection); // Selecting Database from Server
    if (isset($_POST['submit']))
    { // Fetching variables of the form which travels in URL
        $opendicht = $_POST['opendicht'];
        $open = $_POST['open'];
        $dicht = $_POST['dicht'];
        if ($open ='open' || $dicht ='dicht') {
            echo "it works";
        } else {
            echo "it won't work";
        }

    }   

    mysqli_close($con); // Closing Connection with Server
    //header( "refresh:5;url=device_toevoegen/device_toevoegen.php" );
?>  

ifelse下的所有回声都不会显示在opendicht.php上

4 个答案:

答案 0 :(得分:4)

您正在检查是否存在$ _POST ['submit']变量,但它不在您的表单上。您应该将提交按钮命名为使其存在:

<input type="submit" value="Doorvoeren" name="submit">

此外,您不必为您的选项命名,只需命名您的选择并获取值然后处理它:

<select name="open">
    <option value="1">Open</option>
    <option value="0">Dicht</option>
</select>

然后在php

if(isset($_POST['open'])) {
    $open = (int) $_POST['open'];
    if ($open) {
        ...
    } else {
       ...
    }
}

答案 1 :(得分:1)

只需替换此

if(isset($_POST['submit']))

if(isset($_POST['opendicht']))

检查您的输入是否存在

答案 2 :(得分:0)

这是您完整正确的代码。

HTML:

<form action="opendicht.php" method="post">
        <select name="opendicht" multiple>
            <option name="open" value="open">open</option>
            <option name="dicht" value="dicht">dicht</option>
        </select>
        <input type="submit" value="Doorvoeren" name="submit">
</form>

PHP:

<?php
error_reporting(E_ERROR);
include '../dbconnect.php';
//$connection = mysql_connect("", "", ""); // Establishing Connection with Server
//$db = mysql_select_db("cerar", $connection); // Selecting Database from Server
    if(isset($_POST['submit']))
    { // Fetching variables of the form which travels in URL
        $opendicht = $_POST['opendicht'];        
        if($opendicht ='open' || $opendicht ='dicht')
        {
            echo "it works";
        }
        else{
            echo "it won't work";
        }

    }   

    mysqli_close($con); // Closing Connection with Server
    //header( "refresh:5;url=device_toevoegen/device_toevoegen.php" );
?>

答案 3 :(得分:0)

另一种选择:

if(isset($_POST) && !empty($_POST) && count($_POST) > 0){
  //your code here
}