Jump to content

is this $_POST considered empty


ploppy

Recommended Posts

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:.

 

Would php see this as an empty post? What are these numbers etc after carton? carton is actually an array. If I do elseif(!empty($carton)) then the message is triggered. This is strange bearing in mind that the input values are empty at submit stage.  thanks

 

 

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>')
      }

 

sample php code

 

 

$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
}

Link to comment
Share on other sites

@Pandemikk

very simply, it is a empty array.  if I test for empty $_POST using the following (The elseif is there because it is in the middle of other elseif statements), then no error is triggered, but it should be because the input on the form is empty. But if I use !empty, then the error is triggered. Why is that happening. Thanks

 

elseif(empty($carton)) {

 

    //set the response

    $response_array['status'] = 'error';

    $response_array['message'] = 'You must enter a carton number';

 

}

Link to comment
Share on other sites

Arrays with keys but no values for those keys will not be considered "empty".

 

Depending on what you're trying to do, you could loop through each array and check if the value exists and, if not, trigger an error then.

 

 

But I think your issue lies with more than that. See what the above poster said^.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.