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

  • Home
  • SEARCH
  • 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 9129843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:50:24+00:00 2026-06-17T07:50:24+00:00

Ok im tring to get PHP variable to javascript variable via ajax. i have

  • 0

Ok im tring to get PHP variable to javascript variable via ajax.

i have some piece of php code to make this variable it look like this: (i wont put entire code because its working so only relevant code for this topic. i have new_m variable which is ARRAY and i want to pass it)

shuffle($new_m); 
echo json_encode($new_m);

then i have js file which should catch that echo and it look like this:

function getXMLHttp() 
{
  var xmlHttp

  try
  {
    //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    //Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("Your browser does not support AJAX!")
        return false;
      }
    }
  }
  return xmlHttp;
}

function MakeRequest() 
{
  var xmlHttp = getXMLHttp();

  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      var myvar = new Array();
    var myvar=JSON.parse(xmlHttp.responseText);


return myvar;


    }

  }
   xmlHttp.open("GET", "showimage.php", true);
  xmlHttp.send(null);
}

When this code is not on separate page like here and when is myvar is used inside function it works (because i have used this code on another page successfully). So i think my problem is not returning correct variable or not returning it on correct way.

and final piece of code is part where this myvar should be used it looks like:

<script type="text/javascript" src="js/shuffle.js"></script>
<title>undf</title>
</head>
<body onload="MakeRequest()">


<script type="text/javascript">
alert(myvar);
var pos = 0;
var imgs = myvar;
</script>

and nothing happens. im still new at this ajax and javascript. thanks for you help in advance.

  • 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-06-17T07:50:26+00:00Added an answer on June 17, 2026 at 7:50 am

    Your problem is that when alert( myvar); is executed, the request to the server hasn’t happened yet, and the variable is undefined (not to mention that I believe the variable is out of scope, so you can’t access it).

    You should set up the JS so that when the window loads, you execute the request to retrieve the data and then read it:

    <script type="text/javascript">
    window.onload = function() {
        var myvar = MakeRequest();
        alert( myvar);
    }
    </script>
    

    You can then get rid of the onload within the <body> tag.

    Note that I’m not entirely sure that you’re returning the value from the MakeRequest() function correctly, since the return is within the xmlhttp callback and not in the function. You should investigate this and verify.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a PHP array variable into a JavaScript variable. This
ok so im trying to get a javascript variable into a php script, this
I'm trying to get PhantomJS to run via PHP. When I run the JavaScript
I am trying to get a variable from the php code and put it
Im having troubles sending a variable from php to javascript. I pull javascript (Ajax)
I am trying to get php's namespace technique down but having some issues here:
I'm trying to get php to automatically pass the session ID via url, even
Using php I am trying to get some data from a .csv file which
I was trying to get PJAX working with my PHP site, this is the
I am trying to get the requested URL from PHP. The URL is like

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.