Solved

is this $_POST considered empty

Posted on 2011-09-14
7
393 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
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!

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

737 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