Darkranger85 Posted June 4, 2013 Share Posted June 4, 2013 Hey all, I'm trying to get a php array using AJAX in order to load it into a JavaScript array. I've managed to retrieve the PHP arrays information but I can't seem to figure out how to get it into a JavaScript array. When I run the code and just have it use 'result' in the success function it shows all the data from the PHP array, so I know the information is there. Here is my code so far. JavaScript: $(document).ready(function(){ /*-Click Button-*/ $('#button').click(function(){ /*-AJAX Request-*/ $.ajax({ type: 'POST', url: 'core/content.php', data: '', dataType: 'json', cache: false, success: function(result){ var errors = new array(); for(i=0;i<studentArray.length;i++){ errors[i] = Array[i]; } console.log(errors); $('#content').html(errors[0]); } }).error(function(){ alert('Some error occured!'); }); /*----------END AJAX----------*/ }); /*----------End Button Click----------*/ }); And the PHP: <?php $errors = array('An error has been detected!', 'And stuff'); echo json_encode($errors); ?> I appreciate any and all help! Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/ Share on other sites More sharing options...
Irate Posted June 4, 2013 Share Posted June 4, 2013 What do you get when you alert success: function(data){ alert(data); }? Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1433957 Share on other sites More sharing options...
cpd Posted June 4, 2013 Share Posted June 4, 2013 Log in the console the result before you do anything and I suggest you test it in Chrome as it'll show you in detail what it contains. Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1433964 Share on other sites More sharing options...
Darkranger85 Posted June 4, 2013 Author Share Posted June 4, 2013 Irate: I assume you mean 'alert(result);' inside of the success function. If so, it alerts 'An error has been detected,And stuff'. So I know the information from the PHP script is in there. cpd: The console says: Uncaught ReferenceError: array is not defined init.js:11 $.ajax.successinit.js:11 cjquery.js:4 p.fireWithjquery.js:4 kjquery.js:6 r Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1433986 Share on other sites More sharing options...
cpd Posted June 4, 2013 Share Posted June 4, 2013 What's "studentArray" its not defined anywhere. You definitely logged the result on the first line of your success function? Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1433988 Share on other sites More sharing options...
Darkranger85 Posted June 4, 2013 Author Share Posted June 4, 2013 Crap, the studentArray was left behind from a tutorial I was following. Here is the current code. Still gives me the same error and I'm sure called console.log on the fist line of the success function. $(document).ready(function(){ /*-AJAX Request-*/ $.ajax({ type: 'POST', url: 'core/content.php', data: '', dataType: 'json', cache: false, success: function(result){ console.log(result); var errors = new array(); for(i=0;i<result.length;i++){ errors[i] = result[i]; } $('#content').html(result[0]); } }).error(function(){ alert('Some error occurred!'); }); /*----------END AJAX----------*/ }); Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1433993 Share on other sites More sharing options...
cpd Posted June 4, 2013 Share Posted June 4, 2013 Now I can see your error. Line 11 should be errors = new Array(); not lower-case "a". Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1433997 Share on other sites More sharing options...
Darkranger85 Posted June 4, 2013 Author Share Posted June 4, 2013 Ah! That would do it wouldn't it lol. Thanks man, I really appreciate the help! I'm just getting into JavaScript and I'm finding it a lot less forgiving than PHP was lol. Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1433999 Share on other sites More sharing options...
Irate Posted June 4, 2013 Share Posted June 4, 2013 You would laugh, JavaScript is non-strict mode throws as many warnings instead of fatals like PHP does. And the == operator attempts a whole lot of type conversions before comparing. Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1434003 Share on other sites More sharing options...
Darkranger85 Posted June 4, 2013 Author Share Posted June 4, 2013 (edited) Well, hopefully I can get it lol. On a side note, do you guys know if I can mimic this functionality with jQuery: EDIT: Specifically the include part. <?php if(!empty($errors)){ include 'includes/modules/error.mod.php'; }else if(!empty($success)){ include 'includes/modules/success.mod.php'; } ?> Edited June 4, 2013 by Darkranger85 Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1434005 Share on other sites More sharing options...
cpd Posted June 4, 2013 Share Posted June 4, 2013 if(errors.length > 0) { // XHR for errors page } else { // XHR for success page } Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1434006 Share on other sites More sharing options...
Darkranger85 Posted June 4, 2013 Author Share Posted June 4, 2013 Awesome! And I just thought of another thing lol. Hope ya don't tar and feather me lol. What if I want to get two separate arrays from my php file? For instance I have the $errors array but I also have a $success array. I'd need to put them in separate JavaScript arrays. Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1434009 Share on other sites More sharing options...
cpd Posted June 4, 2013 Share Posted June 4, 2013 $errors = array("Error 1", "Error 2", "Error 3"); $success = array("Success message"); $output = array("errors" => $errors, "success" => $success); echo json_encode($output); Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1434014 Share on other sites More sharing options...
Darkranger85 Posted June 4, 2013 Author Share Posted June 4, 2013 But I'm assuming that would effect how I would have to setup the for loop in order to put them into their own arrays on the JS side right? Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1434017 Share on other sites More sharing options...
Irate Posted June 4, 2013 Share Posted June 4, 2013 You can try returning an object with PHP, like this... <?php $obj = (object) [array1,array2]; json_encode($obj); ?> Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1434019 Share on other sites More sharing options...
cpd Posted June 4, 2013 Share Posted June 4, 2013 You can encode it either way, the encoding is irrelevant, just make a decision and stick with it. Regarding your for loop, you can just use result["errors"].length. I think you need to start researching as you're firing questions off without thinking about things yourself. Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1434021 Share on other sites More sharing options...
Darkranger85 Posted June 4, 2013 Author Share Posted June 4, 2013 I apologize, I do tend to do that. I assure you, it's not because of laziness, it's because of excitement. Stuff starts working and I just start rapidfire. I'll try to improve. I am having another issue. If you would like feel free to make me work for my answer, I'm all for working things out I just need some guidance. The script before was a mock up that I was using to get the hang of using the ajax function. Now, I hooked it up to my real php file that has the actual arrays I'm working with in my project. The thing is, it doesn't work even though it seems to be setup exactly the same. if I change the url in the ajax function back to the test file, it works fine. This is the real file: <?php session_start(); include 'configs/starting.config.php'; require 'connection/connect.php'; require 'functions/general.lib.php'; require 'functions/users.lib.php'; require 'functions/chatbox.lib.php'; $errors = array('Error'); $success = array('Success'); /*-Export PHP Arrays to jQuery (init.js)-*/ echo json_encode($export = array( 'errors'=>$errors, 'success'=>$success )); /*----------*----------*/ ?> And this is the original test file: <?php $errors = array('Error'); $success = array('Success'); echo json_encode($export = array( 'errors'=>$errors, 'success'=>$success )); ?> I don't see a difference. The arrays are the same and I copy and pasted the json function so it has to be the same. The two files are in the same directory, so it shouldn't be a file path issue. Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1434037 Share on other sites More sharing options...
cpd Posted June 5, 2013 Share Posted June 5, 2013 Firstly check the output of your PHP file by going to it in the browser. If that checks out, you can assume its your AJAX that's screwing up. Try testing if the success/error callbacks are triggered using an alert(). Quote Link to comment https://forums.phpfreaks.com/topic/278737-problems-transferring-a-php-array-to-javascript/#findComment-1434143 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.