H Emad
H Emad

Reputation: 327

How can I get multiple data, from a php file, with ajax?

I want to get data from submit.php page and put them in some tags in my page. in the example below I can put just on data in tag that has id: "shortcutTitle". I get more than one data from my php file(from database) and I want them to sit on various tags in my page.

function submit(shortcutid,shortcuttitle,shortcutlink,icon)
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
             document.getElementById("shortcutTitle").value=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","submit.php?shid="+shortcutid+"&shtitle="+shortcuttitle+"&shlink="+shortcutlink+"&shicon="+icon,true);
    xmlhttp.send();
}

Upvotes: 1

Views: 536

Answers (1)

Menztrual
Menztrual

Reputation: 41587

You would want to use json_encode to send a javascript object back to the XML request

<?php
  echo json_encode( array('name' => 'tehlulz', 'id' => '1') );
?>

So from the results, you can then call the output by:

var ajaxResuts = {};

if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
  ajaxResuts = JSON.parse( xmlhttp.responseText );
  alert("Returned: " + ajaxResults.name);
}

Upvotes: 3

Related Questions