ajoo Posted August 9, 2020 Share Posted August 9, 2020 (edited) Hi all ! What would be the best way to identify and handle an empty array sent from JS to PHP? This rather simple looking problem has resulted in a lot of confusion to me. $_POST['myArray'] = ""; // an empty string -- this is how the empty array from JS is received in PHP Thanks ! Edited August 9, 2020 by ajoo Quote Link to comment https://forums.phpfreaks.com/topic/311299-handling-empty-string-returned-from-js-to-a-php-array/ Share on other sites More sharing options...
kicken Posted August 9, 2020 Share Posted August 9, 2020 Test for what you expect, and if you don't get what you expect then handle it however you see fit. For example: if (!is_array($_POST['myarray'])){ $_POST['myarray'] = []; } Quote Link to comment https://forums.phpfreaks.com/topic/311299-handling-empty-string-returned-from-js-to-a-php-array/#findComment-1580483 Share on other sites More sharing options...
ajoo Posted August 9, 2020 Author Share Posted August 9, 2020 (edited) Hi Kicken ! Thanks for the response. I have done something like what you are suggesting and it works. However I was wondering, since it would be a common issue with AJAx calls, that there would be maybe a "proper" way of handling blank strings that need to be interpreted as arrays on php side of things. Though my code works but does seem a bit of a hack. Thanks again. Edited August 9, 2020 by ajoo Quote Link to comment https://forums.phpfreaks.com/topic/311299-handling-empty-string-returned-from-js-to-a-php-array/#findComment-1580484 Share on other sites More sharing options...
requinix Posted August 9, 2020 Share Posted August 9, 2020 5 hours ago, ajoo said: Thanks for the response. I have done something like what you are suggesting and it works. However I was wondering, since it would be a common issue with AJAx calls, that there would be maybe a "proper" way of handling blank strings that need to be interpreted as arrays on php side of things. Send JSON. Even empty arrays are still identifiable as arrays. Quote Link to comment https://forums.phpfreaks.com/topic/311299-handling-empty-string-returned-from-js-to-a-php-array/#findComment-1580493 Share on other sites More sharing options...
ajoo Posted August 10, 2020 Author Share Posted August 10, 2020 Hi Requinix, Thanks for the response and the idea of JSON ! I did read that it's not a great idea to use JSON for smaller bits of data. Any comments on that ? I'll work it through though. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/311299-handling-empty-string-returned-from-js-to-a-php-array/#findComment-1580513 Share on other sites More sharing options...
requinix Posted August 10, 2020 Share Posted August 10, 2020 Citation needed. Quote Link to comment https://forums.phpfreaks.com/topic/311299-handling-empty-string-returned-from-js-to-a-php-array/#findComment-1580514 Share on other sites More sharing options...
ajoo Posted August 10, 2020 Author Share Posted August 10, 2020 It was in the comments somwehere on stackflow. I am not sure I would be able to find it again. If I do find it I will definitely send a link to it. But you may kindly tell if that would be incorrect or not. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/311299-handling-empty-string-returned-from-js-to-a-php-array/#findComment-1580517 Share on other sites More sharing options...
requinix Posted August 10, 2020 Share Posted August 10, 2020 I expect they were making their best effort to prove somebody else wrong in order to make themselves look better. You know. StackOverflow. JSON is fine. Quote Link to comment https://forums.phpfreaks.com/topic/311299-handling-empty-string-returned-from-js-to-a-php-array/#findComment-1580519 Share on other sites More sharing options...
ajoo Posted August 10, 2020 Author Share Posted August 10, 2020 Thanks for the clarification requinix. 👍 Quote Link to comment https://forums.phpfreaks.com/topic/311299-handling-empty-string-returned-from-js-to-a-php-array/#findComment-1580520 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.