0

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.

9
  • so what have you tried in order to get a specific variable? Do you have any code that you can show us that you think should do the job, but doesn't work as you expect it to? Commented Sep 19, 2013 at 19:42
  • Rewrite your PHP script. Commented Sep 19, 2013 at 19:43
  • So, how do you expect anyone to help without showing them the information? Commented Sep 19, 2013 at 19:43
  • How you are fetching everything? show some code Commented Sep 19, 2013 at 19:45
  • that depends on what is the PHP script returning Commented Sep 19, 2013 at 19:45

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

6 Comments

first script you wrote on JSON, Do I need to put it in php page??
Yes, instead of echo "$scriptcount"; And remove the other echo inside the loop.
Would it be same for if I want to print array instead of variable?
Try document.getElementById("myDiv").innerHTML = data.script. You'll get a comma-separated strings with the value from the array.
Yes.. I am getting that. but, I have to separate it to put all that data into dropdownbox.. is there any way?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.