if (!empty($_POST)) not working

前端 未结 6 1864
悲哀的现实
悲哀的现实 2020-12-18 09:28

I have a php form (code to follow) with a submit button that runs JSON-events.php as its action (method = POST). In the JSON-events co

相关标签:
6条回答
  • 2020-12-18 10:03

    Do not use

    if (!empty($_POST)) {
    

    to check if there is a post done use this:

    if( $_SERVER['REQUEST_METHOD'] == 'POST') {
    
    0 讨论(0)
  • 2020-12-18 10:03

    Conditions that you can use with $_POST

    if(count($_POST)>0){
        //...
    }
    if(isset($_POST['field_name'])){
        //...
    }
    if( $_SERVER['REQUEST_METHOD'] == 'POST') {
        //..
    }
    
    0 讨论(0)
  • 2020-12-18 10:04

    Using empty() will fail if value 0 is posted So it is better to use
    isset($_POST['FIELD_NAME'])

    0 讨论(0)
  • 2020-12-18 10:14

    empty() will work if $_POST does not have any values (Empty array), but in your case when you submit without any values still you get an array like below and it's not empty:

        Array
        (
            [searchlat] => 
            [searchlong] => 
            [search] => Submit Query
        )

    empty() will return true only if $_POST returns

    Array (
    
    )
    

    But it's not going to happen as every form will have one Sumbit button.

    Just use

    if($_POST) {
    //php code
    }
    

    It will solve your problem.

    Learn more about empty() by visiting http://php.net/manual/en/function.empty.php

    0 讨论(0)
  • 2020-12-18 10:17

    Try this

      if(!empty($_POST['search']) && isset($_POST['search']))
    
    0 讨论(0)
  • 2020-12-18 10:22

    much better to check for an actual value in the $_POST array. Eg.

    if (!empty($_POST['search']))
    
    0 讨论(0)
提交回复
热议问题