5

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

Leri
  • 12,367
  • 7
  • 43
  • 60
Mark Adams
  • 57
  • 1
  • 1
  • 6
  • 1
    Wait....you don't actually mean that the string literal `"if (!empty($_POST)) {"` is part of the JSON object in question, right? –  Sep 21 '12 at 10:14
  • 'top part of the JSON' makes no sense, that's a line of php. Try a var_dump of your $_POST array as the first line in your script to see if anything is coming through – Asciiom Sep 21 '12 at 10:14
  • 2
    What JSON are you talking about – Baba Sep 21 '12 at 10:14
  • actually I don't see any JSON evidence here, seems like an ordinary HTML form tag with ordinary submit button, but that's not affecting the problem, try `var_dump($_POST);` to see what contains the array – haynar Sep 21 '12 at 10:16
  • apologies for being a bit vague... the `if (!empty($_POST)) {` is a line in JSON-events.php, it aims to ensure the Submit button has been pressed to determine which JSON to run. I have left out the JSON to try to keep it brief. – Mark Adams Sep 21 '12 at 10:32
  • Update - I tried `var_dump($_POST);` and it showed that `header('location: index.php');` was causing an issue - having removed that the code does indeed work, but now I need to know how to stop the ECHO being displayed, and get back to the index page... – Mark Adams Sep 21 '12 at 10:35

6 Answers6

9

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

Farzad
  • 842
  • 2
  • 9
  • 26
Vijay Joseph
  • 492
  • 3
  • 14
7

Do not use

if (!empty($_POST)) {

to check if there is a post done use this:

if( $_SERVER['REQUEST_METHOD'] == 'POST') {
JvdBerg
  • 21,777
  • 8
  • 38
  • 55
1

Conditions that you can use with $_POST

if(count($_POST)>0){
    //...
}
if(isset($_POST['field_name'])){
    //...
}
if( $_SERVER['REQUEST_METHOD'] == 'POST') {
    //..
}
Alaa Gamal
  • 1,135
  • 7
  • 18
  • 28
0

Try this

  if(!empty($_POST['search']) && isset($_POST['search']))
Harinder
  • 1,257
  • 8
  • 27
  • 54
  • 2
    The second `isset()` clause is redundant, and `empty()` will return `false` if `$_POST['search']` does not exist any way. – Martin Bean Sep 21 '12 at 10:25
  • @MartinBean $_POST['search'] does not exist ?? its in your form ... check it will work – Harinder Sep 21 '12 at 10:33
  • `search` is the name of my submit button (I already have another called submit... – Mark Adams Sep 21 '12 at 10:37
  • @Harinder Not sure why you're telling me, it's not my form. I was just saying the second clause is redundant, because `empty()` will return `false` any way if `search` is not present in `$_POST`. – Martin Bean Sep 21 '12 at 10:55
  • @MarkAdams i was saying its in your form .. and its not empty() , its !empty() – Harinder Sep 21 '12 at 11:11
0

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

if (!empty($_POST['search']))
user410932
  • 2,915
  • 4
  • 22
  • 23
0

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

Chirag
  • 41
  • 6