Passing a PHP Array through AJAX

If you are creating dynamic elements on your page, you will need to be able to send information to and from the browser. There’s lots of documentation on how to POST data using AJAX to the server side. What if you want to return an array of data back to the browser?

$.ajax({
    url: "<?php echo site_url('test/urlGoesHere/'); ?>",
    type: 'POST',
    data: form_data,
    dataType:"json",
    success: function(data) {
        alert(data[0]);
   }

Marking the dataType as json here is key.

In your PHP file, use json_encode to send your array:

print json_encode($my_array);

If you don’t want to save the array into a variable, you can also just write the array between the json_encode parantheses:

print json_encode(array("name"=>$name,"title"=>"Ms."));

Then in your AJAX success, you can access the array elements this way:

data.name[0]; //the first name element
data.title;  // "Ms."
data[0];

If this was your array: array("Jack", "Mr."), then your AJAX data can be handled like this:

$.ajax({
    url: "",
    type: 'POST',
    data: form_data,
    dataType:"json",
    success: function(data) {
        var name = data[0];
        var title = data[1];
        alert('Name: ' + name + ', Title: ' + title);
   }
Advertisements

Linking Snippets in Codeigniter – PHP

Since I have been attending LAMP Camp, I have been learning PHP and MySQL and using MVC frameworks to build web applications. I’m about to start my 6th week tomorrow, so I’m already halfway through! It’s a free 12-week immersive program in Atlanta, that you can read more about here.

To get your certificate of completion, you have to create a personal portfolio site using what you’ve learned. Since we have to use an MVC framework for our project, I’ve chosen Codeigniter since they have a tutorial that comes with the download. Their tutorial is very easy to follow and they teach you how to also connect to a database.

Today I spent my time on the views and getting the Bootstrap Carousel feature to work. I also learned how to link pages together. If you are using the MVC framework Codeigniter for your PHP web application, you can link page views without touching your controller file.

I saved my ‘snippet’ views in their own folder in ‘views/templates’ and just linked to them where I wanted them to appear. Since I worked on the carousel feature today, I put the carousel code in its own file called carousel.php. Then, I added the following code to my home.php view file where I wanted the carousel to appear:

 <?php $this->load->view("templates/snippets/carousel.php"); ?>

This is my controller file that loads the home.php view:

public function view($page = 'home')
{
  if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
  {
  // Whoops, we don't have a page for that!
  show_404();
  }

  $data['title'] = ucfirst($page); // Capitalize the first letter

  $this->load->view('templates/header', $data);
  $this->load->view('pages/'.$page, $data);
  $this->load->view('templates/footer', $data);
}