简体   繁体   中英

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. 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

    <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
    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 ! .

Change,

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

To,

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

Also

You input for your submit is incorrect 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.

HTML

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

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.

The js function just displays the value selected from the select menu when selected

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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