Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6381049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:20:47+00:00 2026-05-25T02:20:47+00:00

Apologies if this explanation isn’t clear, it’s hard for me to understand too. How

  • 0

Apologies if this explanation isn’t clear, it’s hard for me to understand too. How can I use PHP & Ajax to send an array to Javascript? I’m using Ajax to get an array of photos, which I’m then looking to append to an empty <div> on my page.

The jQuery looks as follows:

$.ajax({
    url: "<?php echo site_url('demo/getPhotos/'); ?>",
    type: 'POST',
    data: form_data,
    success: function(data) {
        alert(data);
   }

And the PHP function getPhotos looks like this:

<?php

$photos = array();

foreach ($data as $photo) {
    array_push($photos,$photo['source']);
    }

// echo json_encode($photos); How should I be returning $photos?

If I simply echo $photos; the data is sent to the success callback, but it doesn’t appear to be in a usable format.

If I do a var_dump($photos) in PHP, the result looks something like:

array(4) {
  [0]=>
  string(14) "some_image.jpg"
  [1]=>
  string(14) "some_image.jpg"
  [2]=>
  string(14) "some_image.jpg"
  [3]=>
  string(14) "some_image.jpg"
}

I’ve tried various combinations of json_encode and the like but really I am guessing and not sure of the theory behind it. What’s the best way to pass data from PHP to Javascript in this context?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:20:48+00:00Added an answer on May 25, 2026 at 2:20 am

    Try:

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

    On the PHP side, you’ll be wanting to print:

    print json_encode($photos);
    

    Another thing you might try in order to better encapsulate your code, and as an example of further JSON gooodness, would be:

    print json_encode(array("photolist"=>$photos,"photo_owner"=>"Me!"));
    

    Then on the server, you’d access these with:

    data.photolist[0]; //First photo
    data.photo_owner;  //The owner of the photo set
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies if this is too ignorant a question or has been asked before. A
Apologies if this is a very simple question; I don't use XSLT very much
I am having a couple of problems involving PHP & AJAX using jQuery. To
Apologies if this is a too basic question but I couldn't find any satisfactory
I apologize for syntax errors, this is my simple explanation of the problem. I've
Apologies if this is a duplicate, 20 minutes of searching didn't yield this exact
Apologies if this doesn't make sense, i'm not much of an experienced programmer. Consider
Apologies if this is answered elsewhere; couldn't find enough information to convince myself of
Apologies if this question is a bit obscure, I've been banging my head against
Apologies if this is a repost, I could not find the search terms to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.