2012-03-14 186 views
0

阵列JavaScript数组我想这个Ajax请求,填补了JavaScript的hintArray的结果。也就是说,我想hintArray包含相同的值$row。这可以用简单的方法完成吗? 应该怎样PHP文件输出进行? 是hintArray = JSON.parse(this);是否正确?PHP通过Ajax请求

非常感谢

的JavaScript

var hintArray = new Array(); 
postAjaxRequestFunktion(minFunktion, 'getHint.php', 'id =1') 
function minFunktion() 
{ 
    hintArray = JSON.parse(this); 

} 

function postAjaxRequestFunktion(minFunk,minUrl, mittArg) 
{ 
    var contenttype = 'application/x-www-form-urlencoded' 
    var minRequest  = new skapaAjaxObjekt(minFunk) 
    if (!minRequest) return false 
    minRequest.open('POST', minUrl, true) 
    minRequest.setRequestHeader('Content-type', contenttype) 
    minRequest.setRequestHeader('Content-length', mittArg.length) 
    minRequest.setRequestHeader('Connection',  'close') 
    minRequest.send(mittArg) 
    return true 
} 

function skapaAjaxObjekt(minFunk) 
{ 
    try  { var minRequest = new XMLHttpRequest()     } 
    catch(e1) { try { minRequest = new ActiveXObject("Msxml2.XMLHTTP") } 
    catch(e2) { try { minRequest = new ActiveXObject("Microsoft.XMLHTTP") } 
    catch(e3) { minRequest = false }}} 
    if (minRequest) minRequest.onreadystatechange = function() 
    { 
     if (this.readyState == 4 && this.status == 200 && 
      this.responseText != null) 
      minFunk.call(this.responseText) 
    } 

    return minRequest 
}  

getHint.php

$result = mysql_query("SELECT hint01,hint02 FROM main WHERE id = '00000001'"); 
if (!$result) { 
    echo 'Could not run query: ' . mysql_error(); 
    exit; 
} 
$row = mysql_fetch_row($result); 

回答

1

你应该编码您的PHP阵列

json_encode($your_array); 

,并在JavaScript与EVAL解码

your_js_array = eval('(' + yourJSONtext + ')'); 
+0

我已将此添加到PHP文件: 回声json_encode($行); 而这个JS: 函数minFunktion() { hintArray = eval('('+ this +')'); \t } 但它不工作。 JS数组是空的。我究竟做错了什么? 如果我代替它添加到JS: 功能minFunktion() 这是写在所述提示元素{ 的document.getElementById( '提示')的innerHTML =此 。}: [ “hintText1”,” hintText2" ] – Fred 2012-03-15 06:07:10

+0

有“本”的值?使用console.info(this);或者console.info(this.responseText);你收到什么? – 2012-03-15 14:34:40

+0

我犯了一个错误,但纠正了它,现在你的解决方案完美无缺。非常感谢您向我展示解决这个问题的简单方法! – Fred 2012-03-15 21:28:40