If (! Empty ($ _ POST)) does not work

I have a php form (code for later) with a submit button that fires JSON-events.php as its action ( method = POST ). In the JSON-events code, I check if the form was submitted using if (!empty($_POST)) . My problem is that the JSON event code does not seem to recognize $_POST .

Here is the section of the side code of the form.

 <div class="simple_overlay" id="searchform"> <form id="myform" class = "cols" action="json-events.php" method="post" > <h3>Search</h3> <p> <label>address/postcode *</label> <input type="address" id="searchaddress" /> </p> <p> <label for="amount">maximum distance:</label> <input type="text" id="amount" value="5 miles" style=" border:0; color:#43a8c4; font-weight:bold;" /> </p> <div id="slider-range-min" style="width:130px; margin-left:4px; margin-bottom:20px"></div> <p id="terms"> <label>Category 1</label> <input name="cat1" type="checkbox" value="1" checked="checked"/> </p> <p id="terms"> <label>Category 2</label> <input name="cat2" type="checkbox" value="1" checked="checked"/> </p> <p id="terms"> <label>Category 3</label> <input name="cat3" type="checkbox" value="1" checked="checked"/> </p> <p id="terms"> <label>Category 4</label> <input name="cat4" type="checkbox" value="1" checked="checked"/> </p> <p id="terms"> <label>Category 5</label> <input name="cat5" type="checkbox" value="1" checked="checked"/> </p> <p id="terms"> <label>Category 6</label> <input name="cat6" type="checkbox" value="1" checked="checked"/> </p> <p id="terms"> <label>Category 7</label> <input name="cat7" type="checkbox" value="1" checked="checked"/> </p> <p id="terms"> <label>Category 8</label> <input name="cat8" type="checkbox" value="1" checked="checked"/> </p> <p id="terms"> <label>Category 9</label> <input name="cat9" type="checkbox" value="1" checked="checked"/> </p> <p id="terms"> <label>Category 10</label> <input name="cat10" type="checkbox" value="1" checked="checked"/> </p> <p> <input type="hidden" name="searchlat" id="searchlat"/> </p> <p> <input type="hidden" name="searchlong" id="searchlong" /> </p> <input type="submit" name="search" id="search"/> <input type="button" value="Check Address" onclick="codeAddressSearch()"/> <button type="reset">Reset</button> </form> </div> 

and here is the top of the JSON ...

 if (!empty($_POST)) { 

any help is much appreciated

+4
source share
6 answers

empty() will work if $_POST has no values โ€‹โ€‹(empty array), but in your case, when you send without any values, you get an array as shown below and it is not empty:

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

empty() will return true only if $_POST returns

 Array ( ) 

But this will not happen, since each form will have one Sumbit button.

Just use

 if($_POST) { //php code } 

This will solve your problem.

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

+9
source

Do not use

 if (!empty($_POST)) { 

to check if there is a record made use this:

 if( $_SERVER['REQUEST_METHOD'] == 'POST') { 
+7
source

Conditions you can use with $ _POST

 if(count($_POST)>0){ //... } if(isset($_POST['field_name'])){ //... } if( $_SERVER['REQUEST_METHOD'] == 'POST') { //.. } 
+1
source

try it

  if(!empty($_POST['search']) && isset($_POST['search'])) 
0
source

it is much better to check the actual value in the $ _POST array. For instance.

 if (!empty($_POST['search'])) 
0
source

Using empty () will not be done if the value 0 is sent. Therefore, it is better to use isset($_POST['FIELD_NAME'])

0
source

Source: https://habr.com/ru/post/1435477/


All Articles