从php中的标签中选择选项以使用if

时间:2022-11-27 21:01:34

I tried this code:

我试过这段代码:

<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; ?>

But I get errors when I'm testing it on the web. The error that apears is "Notice: Undefined index: selectedOption in C:\wamp\www\vilaLuz\index.php on line 40"

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

What I am trying to do is get the option from the select and put a text under if option 1 is selected and a different text under if option 2 is selected, is that possible?

我想要做的是从选择中获取选项,如果选择了选项1,则在下面放置一个文本,如果选择了选项2,则选择不同的文本,这可能吗?

4 个解决方案

#1


1  

<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; ?>

#2


0  

If you have not send your form, $_POST['selectedOption'] will be not set and empty!

如果您还没有发送表单,$ _POST ['selectedOption']将不会被设置为空!

So;

<?php $selectedOption = (!empty($_POST['selectedOption']) ? $_POST['selectedOption'] : ''); ?>

#3


0  

You need to first check whether $_POST['selectedOption'] exists using isset:

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

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

This is a ternary if statement which sets the value of $selectedOption to either the value of $_POST['selectedOption'] if it is set, or false if it isn't.

这是一个三元if语句,它将$ selectedOption的值设置为$ _POST ['selectedOption']的值(如果已设置),如果不是,则设置为false。

#4


0  

You should check to see if $_POST['selectedOption'] is set and is not empty. Empty() does these tests for you.

您应该检查$ _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>');
    }
}
?>

#1


1  

<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; ?>

#2


0  

If you have not send your form, $_POST['selectedOption'] will be not set and empty!

如果您还没有发送表单,$ _POST ['selectedOption']将不会被设置为空!

So;

<?php $selectedOption = (!empty($_POST['selectedOption']) ? $_POST['selectedOption'] : ''); ?>

#3


0  

You need to first check whether $_POST['selectedOption'] exists using isset:

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

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

This is a ternary if statement which sets the value of $selectedOption to either the value of $_POST['selectedOption'] if it is set, or false if it isn't.

这是一个三元if语句,它将$ selectedOption的值设置为$ _POST ['selectedOption']的值(如果已设置),如果不是,则设置为false。

#4


0  

You should check to see if $_POST['selectedOption'] is set and is not empty. Empty() does these tests for you.

您应该检查$ _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>');
    }
}
?>