我在javascript中使用ajax将数据放入同一页面。 但是我需要来自我的php页面的特定变量,而不是整个响应。
现在我正在使用,
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
这是从该页面获取所有内容。
编辑的代码-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;
}
现在,我正在从$script和$scriptcount获取所有数据...我想要这两个,但以不同的方式。
使 PHP 输出 JSON:
echo json_encode(array(
'scriptcount'=> $scriptcount,
'script'=> $script
));
(不要回显你的 PHP 的任何其他内容)
然后在 JS 中:
var data = JSON.parse(xmlhttp.responseText);
document.getElementById("myDiv").innerHTML = data.scriptcount;