简体   繁体   中英

get specific variable from ajax response using javascript

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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