php if statement html option value

后端 未结 4 912
粉色の甜心
粉色の甜心 2021-02-06 14:49

Suppose you have the following html select statement


                        
    
提交评论

  • 2021-02-06 15:22

    I think the read-friendly version would be:

    switch($option){
        case 'Newest':
            runNewestFunction();
        break;
        case 'Best Sellers':
            runBestSellersFunction();
        break;
        case 'Alphabetical':
            runAlphabeticalFunction();
        break;
        default:
            runValidationRequest();
    }
    

    also, please add the name attribute <select name="option">

    0 讨论(0)
  • 2021-02-06 15:25

    First put a name on your select:

    <select name="demo">
    <option value="Newest">Newest</option>
    <option value="Best Sellers">Best Sellers</option>
    <option value="Alphabetical">Alphabetical</option>
    </select>
    

    Then

    if ($_POST['demo'] === 'Newest') {
    // Run this
    }
    elseif ( $_POST['demo'] === 'Best Sellers' ) {
    // Run this
    }
    

    or

    switch($_POST['demo']){
        case 'Newest' : 
            //some code;
            break;
        case 'Best Sellers':
            //some code;
            break;
        default:
            //some code if the post doesn't match anything
    }
    
    0 讨论(0)
  • 2021-02-06 15:35

    The <select> should have a name attribute, e.g <select name="sortorder">. You could then say

    if ($_REQUEST['sortorder'] == 'Newest') {
      // TODO sortorder 'Newest' was selected.
    }
    

    If you know whether the form data is comming in via HTTP GET or HTTP POST, you could use $_GET['sortorder'] or $_POST['sortorder'] respectively.

    0 讨论(0)
  • 提交回复
    热议问题