ploppy Posted September 14, 2011 Share Posted September 14, 2011 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 } Quote Link to comment https://forums.phpfreaks.com/topic/247148-is-this-_post-considered-empty/ Share on other sites More sharing options...
Pikachu2000 Posted September 14, 2011 Share Posted September 14, 2011 View the output of this. echo urldecode('carton%5B0%5D:carton%5B1%5D:'); Quote Link to comment https://forums.phpfreaks.com/topic/247148-is-this-_post-considered-empty/#findComment-1269344 Share on other sites More sharing options...
ploppy Posted September 14, 2011 Author Share Posted September 14, 2011 I get this carton[0]:carton[1]. From what I can see, it is just an array that can be processed using foreach. But there is no values to extract. So I guess my question would be: What do I test for as a condition for the error checking. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/247148-is-this-_post-considered-empty/#findComment-1269347 Share on other sites More sharing options...
Pandemikk Posted September 14, 2011 Share Posted September 14, 2011 You would test the array keys? I'm not sure what you're asking or what you're trying to accomplish. Are you suppose to get a specific number for that array? if (count($carton) < 2)) { // Error } Quote Link to comment https://forums.phpfreaks.com/topic/247148-is-this-_post-considered-empty/#findComment-1269360 Share on other sites More sharing options...
ploppy Posted September 14, 2011 Author Share Posted September 14, 2011 @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'; } Quote Link to comment https://forums.phpfreaks.com/topic/247148-is-this-_post-considered-empty/#findComment-1269362 Share on other sites More sharing options...
Pikachu2000 Posted September 14, 2011 Share Posted September 14, 2011 What output do you get from the code below? Something isn't adding up here. That looks like it's being sent via GET, not POST, or there's some sort of serialization being attempted before the data is sent. echo '<pre>'; print_r($_POST); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/247148-is-this-_post-considered-empty/#findComment-1269364 Share on other sites More sharing options...
Pandemikk Posted September 14, 2011 Share Posted September 14, 2011 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^. Quote Link to comment https://forums.phpfreaks.com/topic/247148-is-this-_post-considered-empty/#findComment-1269365 Share on other sites More sharing options...
ploppy Posted September 15, 2011 Author Share Posted September 15, 2011 With no value entered into the form, this is the result of the test you asked me to perofrm: Also, I am using jquery serialize() to collate form data. Thanks [carton] => Array ( [0] => [1] => ) Quote Link to comment https://forums.phpfreaks.com/topic/247148-is-this-_post-considered-empty/#findComment-1269571 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.