如何用ajax从php文件获取多个数据?

时间:2022-01-04 03:37:10

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.

我想从submit获得数据。php页面,把它们放到我页面的一些标签中。在下面的示例中,我可以将数据放在具有id:“shortcutTitle”的标记中。我从php文件(从数据库中)获得了多个数据,我希望它们位于页面的各种标记上。

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();
}

1 个解决方案

#1


3  

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

您可能希望使用json_encode将一个javascript对象发送回XML请求

<?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);
}

#1


3  

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

您可能希望使用json_encode将一个javascript对象发送回XML请求

<?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);
}