Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.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中用于if_Php_Html_Select_Post_If Statement - Fatal编程技术网

从标记中获取所选选项<;选择>;在php中用于if

从标记中获取所选选项<;选择>;在php中用于if,php,html,select,post,if-statement,Php,Html,Select,Post,If Statement,我尝试了以下代码: <select name="selectedOption"> <option value="select"> Select </option> <option value="option1"> option 1 </option> <option value="option2"> option 2 </option> </select> <

我尝试了以下代码:

<select name="selectedOption">
    <option value="select">  Select  </option>
    <option value="option1">  option 1  </option>
    <option value="option2">  option 2  </option>
</select>
<?php $selectedOption = $_POST['selectedOption']; ?>
<?php if($selectedOption == "option1") : ?>
    <a href="#">This will only display if option 1 selected</a>
<?php elseif($selectedOption == "option2") : ?>
    <a href="#">This will only display if option 2 selected</a>
<?php endif; ?>

挑选
选择1
选择2
但是当我在网上测试它时,我会出错。 apears的错误是“注意:第40行C:\wamp\www\vilaLuz\index.php中的未定义索引:selectedOption”

我想做的是从“选择”菜单中获取选项,如果选择了选项1,则在其下放置一个文本,如果选择了选项2,则在其下放置另一个文本,这可能吗?


<form action="" method="POST">
<select name="selectedOption">
    <option value="select">  Select  </option>
    <option value="option1">  option 1  </option>
    <option value="option2">  option 2  </option>
</select>
<input type="submit" value="post"/>
</form>
<?php if(isset($_POST['selectedOption'])) : ?>
<?php $selectedOption = $_POST['selectedOption']; ?>
<?php if($selectedOption == "option1") : ?>
    <a href="#">This will only display if option 1 selected</a>
<?php elseif($selectedOption == "option2") : ?>
    <a href="#">This will only display if option 2 selected</a>
<?php endif; ?>
<?php endif; ?>
挑选 选择1 选择2
如果您尚未发送表单,$\u POST['selectedOption']将不会设置为空

所以,


您需要首先使用
isset检查
$\u POST['selectedOption']
是否存在:

<?php
    $selectedOption = isset($_POST['selectedOption'])
                    ? $_POST['selectedOption']
                    : false;
?>


这是一个三元
if
语句,它将
$selectedOption
的值设置为
$\u POST['selectedOption']
的值(如果已设置),或者
false
(如果未设置)。

您应该检查$\u POST['selectedOption']是否已设置且不为空。Empty()为您执行这些测试

<?php
if (!empty($_POST['selectedOption'])) {
    $selectedOption = $_POST['selectedOption'];
    if($selectedOption == "option1") {
        echo('<a href="#">This will only display if option 1 selected</a>');
    }
    elseif($selectedOption == "option2"){
        echo('<a href="#">This will only display if option 2 selected</a>');
    }
}
?>


php不是这样工作的,你应该使用javascript,或者如果你坚持使用php,你需要Ajax,因为它的格式不正确。显然,你不能正确的作为文本,它必须在代码格式,否则它将不会出现。接受我的答案将不胜感激。单击向下投票按钮下的“v”按钮
<?php
if (!empty($_POST['selectedOption'])) {
    $selectedOption = $_POST['selectedOption'];
    if($selectedOption == "option1") {
        echo('<a href="#">This will only display if option 1 selected</a>');
    }
    elseif($selectedOption == "option2"){
        echo('<a href="#">This will only display if option 2 selected</a>');
    }
}
?>