Jump to content

Recommended Posts

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!

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:

 

  1. Uncaught ReferenceError: array is not defined init.js:11
    1. $.ajax.successinit.js:11
    2. p.fireWithjquery.js:4
    3. r

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----------*/			
});

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 by Darkranger85

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.

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.

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.

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.