将变量值传递给URL

时间:2014-11-22 11:53:44

标签: php

我正在使用以下代码从网址获取参数。

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" role="search">
    <select name="seme" id="seme">
        <option value="sem1">Semester-1</option>
        <option value="sem2">Semester-2</option>
        <option value="sem3">Semester-3</option>
        <option value="sem4">Semester-4</option>
    </select>
    <input type="text" name="find" id="find" placeholder="Enter worksheet / file name..." />
    <button type="submit" class="btn btn-theme">Search</button>
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $name = $_REQUEST['find'];
    $sem = $_REQUEST['seme']; 
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name; echo $seme;
    }
}
?>

但问题是我只获得了文本框的价值。我也希望获得选择框的价值。我该怎么办?或者我缺少哪里???

谢谢!!!

3 个答案:

答案 0 :(得分:0)

为什么不使用$ _POST []方法来获取值而不是$ _REQUEST []?试试$ _POST ['seme']

答案 1 :(得分:0)

您正在为 $sem 中获得价值的选择框值打印不同的变量名称 并打印 $seme

使用它:

if ($_SERVER ["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $name = $_REQUEST ['find'];
    $seme = $_REQUEST ['seme'];
    if (empty ( $name )) {
        echo "Name is empty";
    } else {
        echo $name . "<br/>";
        echo $seme;
    }
}

答案 2 :(得分:0)

尝试这样的事情:

<?php
if (isset($_POST["find"])&&isset($_POST["seme"])) {
    // collect value of input field
    $name = $_POST["find"];
    $sem = $_POST["seme"]; 
    echo $name; echo $seme;
}else{
    if (!isset($_POST["find"])){
       echo "Name is empty";
    }
    if (!isset($_POST["seme"])){
       echo "Semester type is empty";
    }
}
?>