• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

is this $_POST considered empty

I am trying to code a error callback using php and ajax.

What I cannot get past is, I am assinging a variable to a $_POST in PHP to check for an empty value. If empty, then trigger error message.

However, jquery is passing the post to php as carton%5B0%5D:carton%5B1%5D if no data is entered.

Would php see this as an empty post? What are these numbers etc after carton? carton is actually an array. thanks

Also, why is the error triggered when the statement becomes, !empty rather than empty?. This is the code that jquery uses to create an input from a slider change event.

for(var i = 0;i < $(this).val();i++) {
        $("#carton").append('<div data-role="fieldcontain"><label for="carton" class="ui-input-text">Enter box ' + (i + 1) + ' number:</label><input type="text" name="carton['+i+']" id="carton['+i+']" class="carton ui-input-text ui-body-null ui-corner-all ui-shadow-inset ui-body-c" /></div>')
      }

Open in new window


sample php

$carton = $_POST['carton'];

elseif(empty($carton)) {

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'You must enter a carton for retrieveal';

//if no errors
}

Open in new window

0
peter_coop
Asked:
peter_coop
  • 3
  • 3
1 Solution
 
hieloCommented:
%5B is urlencoded hex value of [
%5D is urlencoded hex value of ]

So what you are sending is:
carton[0],carton[1]

PHP turns that to:
$_POST['carton']=array('0'=>'', '1'=>'')

So $_POST['carton'] is NOT empty. It contains an array of TWO elements. Each of the elements in THAT array are actually empty, but not $_POST['carton'].

On another note, on your post you have:
carton%5B0%5D:carton%5B1%5D

Is it really sending a colon? The correct url should look more like:
carton%5B0%5D=&carton%5B1%5D=

IF you really want to verify that the server does get a value for carton, do:
var_dump($_POST);

at the beginning of your script. Does it  show carton? If yes, exactly what are its reported values?

Also:
>>$carton = $_POST['carton'];
>>elseif(empty($carton)) {

You cannot have an elseif without an if. So that should be:

$carton = $_POST['carton'];
if(empty($carton)) {
0
 
peter_coopAuthor Commented:
@hielo
This is the result of the var_dump with no data input. The elseif is correct because it is in the middle of other elseif statements. So on the basis that a user sends the form with empty inputs, how would I code a condition to check for to trigger the error message. Thanks

array(2) {
  [0]=>
  string(0) ""
  [1]=>
  string(0) ""
}
0
 
hieloCommented:
OK, based on that, carton is NOT empty. It contains TWO elements. What you can do is "implode/join" the values. If you end up with a zero-length string, then all of them were left empty:

elseif( '' == trim(implode('',$_POST['carton']) ) )
{
  echo 'You must fill in at least one item';
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
peter_coopAuthor Commented:
@hielo
halfway there. This only checks the first input. Based on what a user selects, there could be 10 inputs created by the slider. So if for example a user selects 2 inputs using the slider, then 2 inputs are genegrated, but using your code, it only checks the first input. So if a user filled the first input but ignored the second it is acting like it completed successfully when in fact it did not becuase 1 of the inputs was blank. Thanks
0
 
Ray PaseurCommented:
Suggest you create a test case at a public-facing URL so we can see the whole problem instead of incremental fragments.  When you have done that, you are ready to ask a question at EE.  Just post the link to the URL, post the code, and then we can show you how to diagnose and debug anything that might be going awry.
0
 
hieloCommented:
In that case you need to iterate through the array checking each element. So, forget about the elseif. Instead use:

//initialize an array to contain the indices of the missing items
$missing=array();
foreach($_POST['carton'] as $i=>$v){
    if( ''==trim(strval($v)) )
    {
        $missing[]=$i;
    }
}

//here, if missing is NOT empty then at least one item was not filled in:
if(!empty($missing))
{
  echo 'You did not fill the following items: ' . implode(', ', $missing) ;
}
0
 
peter_coopAuthor Commented:
perfect. thanks very much
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now