Solved

is this $_POST considered empty

Posted on 2011-09-14
7
379 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
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 strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

708 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

17 Experts available now in Live!

Get 1:1 Help Now