Solved

is this $_POST considered empty

Posted on 2011-09-14
7
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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 110

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

718 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