Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if (!empty($_POST)) not working

Tags:

json

post

forms

php

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 code I am testing whether the form has been submitted using if (!empty($_POST)). My issue is that the JSON-events code doesnt seem to be recognising $_POST.

Heres the form side code section.

<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 part of the JSON...

if (!empty($_POST)) {

any help very much appreciated

like image 594
Mark Adams Avatar asked Sep 21 '12 10:09

Mark Adams


People also ask

How do you check if $post is empty?

You can check the existence of $ _POST with the empty() function. But, the empty() function will return true in the following cases: When all $_POST values are empty strings. The argument is zero.

Which function is used to check if $_ POST variable is empty?

The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.

How to check if variable is empty PHP?

Answer: Use the PHP empty() function You can use the PHP empty() function to find out whether a variable is empty or not. A variable is considered empty if it does not exist or if its value equals FALSE .

How to check if string is empty PHP?

A string is said to be empty, if it contains no characters. We can use empty() function to check whether a string is empty or not. The function is used to check whether the string is empty or not. It will return true if the string is empty.


1 Answers

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

like image 141
Vijay Joseph Avatar answered Sep 27 '22 17:09

Vijay Joseph