dounan9070
2013-09-19 19:41
浏览 69

使用javascript从ajax响应中获取特定变量

I am using ajax in javascript to get data into same page. but I need specific variables from my php page rather then whole response..

right now I am using,

document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

that is fetching everything from that page.

Edited Code-PHP:

$currentscript = $clients->curr_group_content($_POST['group']);
$scriptscount = sizeof($currentscript);
                   for($i=0;$i<$scriptscount;$i++){
                        $script[] = $currentscript[$i]['subject'];
                        echo $script[$i];
                   }
echo "$scriptcount";

JAVASCRIPT:

if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
  document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}

Right now, I am getting all the data from $script and $scriptcount... I want both of these but in separate manner.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongwu8653 2013-09-19 19:50
    已采纳

    Make your PHP output JSON:

    echo json_encode(array(
        'scriptcount'=> $scriptcount,
        'script'=> $script
    ));
    

    (do not echo anything else from your PHP)

    Then in JS:

    var data = JSON.parse(xmlhttp.responseText);
    document.getElementById("myDiv").innerHTML = data.scriptcount;
    
    打赏 评论

相关推荐 更多相似问题