简体   繁体   English

通过ajax将php变量转换为javascript

[英]php variable to javascript via ajax

Ok im tring to get PHP variable to javascript variable via ajax. 确定,通过ajax将PHP变量转换为javascript变量。

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) 我有一些php代码来使此变量看起来像这样:(我不会放完整的代码,因为它的工作原理只是该主题的相关代码。我有new_m变量,它是ARRAY,我想通过它)

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

then i have js file which should catch that echo and it look like this: 然后我有js文件,应该捕获该回显,它看起来像这样:

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). 当此代码不在此处的单独页面上,并且在函数内部使用myvar时,它将起作用(因为我已成功在另一页面上使用了此代码)。 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: 最后一段代码是应使用此myvar的一部分,它看起来像:

<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. 我仍然在这个ajax和javascript新。 thanks for you help in advance. 谢谢您的帮助。

Your problem is that when alert( myvar); 您的问题是当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: 您应该设置JS,以便在加载窗口时执行请求以检索数据,然后读取它:

<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. 然后,您可以摆脱<body>标记内的onload

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. 请注意,由于return值位于xmlhttp回调函数内,而不是函数内,因此我不能完全确定是否正确地从MakeRequest()函数返回了值。 You should investigate this and verify. 您应该对此进行调查并验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM