简体   繁体   English

在PHP中选择选项

[英]Select Option within PHP

Hi I am trying to get the value for 3 different option select menu's and process them via a php script. 嗨,我正在尝试获取3个不同选项选择菜单的值,并通过php脚本处理它们。 The problem I am having is getting the value's to be sent to the server. 我遇到的问题是将值发送到服务器。 When I run tests it is showing that the option is not 'set'. 当我运行测试时,表明该选项未设置。 Here is my code - 这是我的代码-

HTML 的HTML

    <div class='buy'>
            <form method="post">
            <select name="season1" id="purchasequantity" onchange="displaysub(this)">
            <option value="">Quantity</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            </select>
            </input type="submit">
            </form>

PHP 的PHP

    <?php
    if(!isset($_POST['season1'])){
            echo "hello";

            $select1 = $_POST['season1'];
            switch ($select1) {
                    case '1':
                           echo 'this is value1<br/>';
                           break;
                    case '2':
                           echo 'value2<br/>';
                           break;
                    default:
                           # code...
                           break;
            return true;
}

} ?> }?>

You condition is incorrect. 您的条件不正确。

You need to check if the field isset yet your if statement is checking if it is NOT set because of the rouge ! 您需要检查现场isset但您if语句检查,如果没有设置,因为胭脂! .

Change, 更改,

if(!isset($_POST['season1'])) {

To, 至,

if(isset($_POST['season1'])) {

Also

You input for your submit is incorrect HTML, 您输入的提交的HTML不正确,

Change, 更改,

</input type="submit">

To, 至,

<input type="submit">

Personally... 亲身...

I'd change the code so the PHP checks if the submit button has been pressed as that makes for sense. 我将更改代码,以便PHP检查是否已按下“提交”按钮,因为这很有意义。

HTML 的HTML

<input type="submit" name="submitButton">

PHP 的PHP

if (isset($_POST['submitButton'])) {
    /** Your code here now. **/

}
   if(!isset($_POST['season1'])){

I made the above piece of code for debugging purposes. 我将以上代码用于调试目的。 The ! is meant to be there. 应该在那里。

There are 3 optional select menus that are all submited via one button. 一共有3个可选的选择菜单。

The js function just displays the value selected from the select menu when selected 选中时,js函数仅显示从选择菜单中选择的值

Maybe if you are using both the codes in same page : 也许如果您在同一页面同时使用两个代码:

 <div class='buy'>
            <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <select name="season1" id="purchasequantity" onchange="displaysub(this)">
            <option value="">Quantity</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            </select>
            <input type="submit">
            </form>
            <?php
    if(isset($_POST['season1'])){
            echo "hello";

            $select1 = $_POST['season1'];
            switch ($select1) {
                    case '1':
                           echo 'this is value1<br/>';
                           break;
                    case '2':
                           echo 'value2<br/>';
                           break;
                    default:
                           # code...
                           break;
            return true;
}
}else

echo 'value not set';   ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM