Solved

is this $_POST considered empty

Posted on 2011-09-14
7
385 Views
Last Modified: 2012-05-12
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
Comment
Question by:peter_coop
  • 3
  • 3
7 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 36539503
%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
 

Author Comment

by:peter_coop
ID: 36539555
@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
 
LVL 82

Expert Comment

by:hielo
ID: 36539604
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:peter_coop
ID: 36542130
@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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36542236
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
 
LVL 82

Accepted Solution

by:
hielo earned 250 total points
ID: 36542916
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
 

Author Closing Comment

by:peter_coop
ID: 36543551
perfect. thanks very much
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now