php if语句来自html选项值错误

时间:2018-03-06 05:35:50

标签: php html forms

我试图复制以下内容:

php if statement html option value 并遵循建议的指示:

为您的选择命名。

Example:

switch($_POST['selectedValue']) {
    case 'Newest':
        // do Something for Newest
    break;

    case 'Best Sellers':
        // do Something for Best seller
    break;

    case 'Alphabetical':
        // do Something for Alphabetical
    break;

    default:
      // Something went wrong or form has been tampered.
}

在php中

{{1}}

但是,我收到以下错误:

  

注意:未定义的索引:第7行的C:\ xampp \ trial.php中的selectedValue

请帮忙

3 个答案:

答案 0 :(得分:2)

如果您要发送表单数据,那么您必须在表单标签中设置方法发布,或者如果您使用ajax,请按类型发送它,并在您的php文件中将代码放入内部,如果条件发布后像isset($) _POST [ '了selectedValue'])

<form method="post" action="url to your php file">
   <select name="selectedValue">
      <option value="Newest">Newest</option>
      <option value="Best Sellers">Best Sellers</option>
      <option value="Alphabetical">Alphabetical</option>
   </select>
   <button type="submit">Submit</button>
</form>

答案 1 :(得分:0)

在HTML

的表单标记中添加提交按钮后,我的问题得以解决
<form  action="#" method="post">
    <select name="selectedValue">
        <option value="Newest">Newest</option>
        <option value="Best Sellers">Best Sellers</option>
        <option value="Alphabetical">Alphabetical</option
    </select>
    <input type="submit" style="float:left; margin-left: 2%;" name="submit" value="Search"/>
</form>

答案 2 :(得分:0)

之所以发生这种情况,是因为您的selectedValue数组中始终没有密钥POST

if(isset($_POST['selectedValue'])){
   // your code
}