Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从页面上选择下拉菜单的php中检索post值_Php_Html_Post - Fatal编程技术网

从页面上选择下拉菜单的php中检索post值

从页面上选择下拉菜单的php中检索post值,php,html,post,Php,Html,Post,在我的页面nice.php中有一个下拉选择,它是使用按钮提交的 <form action="nice.php" method="post"> <select name="CarList"> <option value="0"> - Select Car - </option> <option value="AM">Aston Martin</option> <option value="KG">Koenigseg

在我的页面
nice.php
中有一个下拉选择,它是使用按钮提交的

<form action="nice.php" method="post">
<select name="CarList">
<option value="0"> - Select Car - </option>
<option value="AM">Aston Martin</option>
<option value="KG">Koenigsegg</option>
<option value="MB">Mercedes Benz</option>
</select>

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

-选择汽车-
阿斯顿马丁
科尼赛克
奔驰
进行选择后,是否可以提交表单,但将下拉列表中的值重新加载到php变量
$SelectedCar

如果(!empty($\u POST['CarList')){
if(!empty($_POST['CarList'])){
    $SelectedCar = $_POST['CarList'];
    echo $SelectedCar;
}else{
    echo '<p>No Selection has been made and submitted yet</p>';
}
$SelectedCar=$_POST['CarList']; echo$SelectedCar; }否则{ echo“尚未做出选择并提交选择”

; }
将代码添加到php页面顶部并删除表单标记。这应该做一个职位回到同一页。(请记住检查$\u POST中是否设置了任何变量)

我还没有测试过这个建议,但它应该是有效的

**编辑

因此,您的HTML应该如下所示:

    <?php 
    if(isset($_POST['CarList']) && !empty(isset($_POST['CarList'])){
        $CarListValue = $_POST['CarList'];

        if($CarListValue != 0){ // a selection has been made using car list
            $SelectedCar = $CarListValue
            echo $SelectedCar  // should print either 'AM', 'KG', or 'MB'
        }
        else{
            echo '<p>No Selection has been made and submitted yet</p>';
        }
    }

    ?>

    <html>
    <head>
    <title>Nice.php</title>
    </head>
    <body>
        <select name="CarList">
        <option value="0"> - Select Car - </option>
        <option value="AM">Aston Martin</option>
        <option value="KG">Koenigsegg</option>
        <option value="MB">Mercedes Benz</option>
        </select>

        <submit>Submit Form</submit>
    </body>
    </html>

我用一个html应该是什么样子的示例编辑了这篇文章。这并没有回答他关于如何重新加载页面或重定向到该页面的问题
    <?php 

    $ValidCars = array('AM','KG','MB');

    if(isset($_POST['CarList']) && !empty(isset($_POST['CarList'])){
        $CarListValue = $_POST['CarList'];

        if(in_array($CarListValue,strtoupper($ValidCars),false) !== true){
            die('<b>The car selection you made is invalid</b>');
        }

        if($CarListValue != 0){ // a selection has been made using car list
            $SelectedCar = $CarListValue
            echo $SelectedCar  // should print either 'AM', 'KG', or 'MB'
        }
        else{
            echo '<p>No Selection has been made and submitted yet</p>';
        }
    }

    ?>