Avatar billede sibga Nybegynder
25. april 2011 - 16:56 Der er 6 kommentarer

Ajax php and XML

I need some help in AJAX as i'n new to ajax.
I have 3 different files.

index.php
database-xml.php
Script.js

index.php - code is here.

<html>
<body onload="GetAllProducts()">
  <div id="Products"></div>

  <span id="GettingProductId"></span>
  <span id="GettingProductName"></span>
  <span id="GettingProductDetail"></span>
  <span id="GettingProductImage"></span>
 
  <script type="text/javascript" src="script.js"></script> </body>
</html>

database-xml.php - Code

if($_GET['function'] == 'GetAllProducts')
{
$query = "    SELECT    *
    FROM product, brand, category, size, colors
    WHERE product.size_id = size.size_id
    AND product.color_id = colors.color_id
    AND product.brand_id = brand.brand_id
    AND product.category_id = category.category_id   
    ";
    $query = mysql_query($query);
   
    header('content-type: text/xml');
    $xml =     "<?xml version='1.0' encoding='UTF-8' ?>";
    $xml .= "<Products>";

    while($result = mysql_fetch_array($query))
    {
    $xml .=    "<Product>";
    $xml .=    "<Id>".$result['product_id']."</Id>";
    $xml .=    "<Name>".$result['product_name']."</Name>";
    $xml .=    <Description>".$result['product_description']."</Description>";
    $xml .=    "</Product>";
    }
   
    $xml .= "</Products>";
    echo $xml;
}

Script.js - Code


function GetAllProducts()
{
    if (window.XMLHttpRequest)
    {
        myAjax = new XMLHttpRequest();
    }
    else
    {
        myAjax = new ActiveXObject("Microsoft.XMLHTTP");
    }   
   
    var random = Math.random();   
    var path = "database-xml.php?function=GetAllProducts";
    myAjax.open("GET", path, false);
    myAjax.send("");

    var myResponse= myAjax.responseXML;   
    var container = document.getElementById("Products");
    var numberOfProducts = myResponse.getElementsByTagName("Id","Name","Description","Image","Price","Quantity","Size","Color","Brand","Category");

    for(var i = 0; i < numberOfProducts.length; i++)
    {
        var productId = myResponse.getElementsByTagName("Id")[i].childNodes[0].nodeValue;
        var productName = myResponse.getElementsByTagName("Name")[i].childNodes[0].nodeValue;
        var productDescription = myResponse.getElementsByTagName("Description")[i].childNodes[0].nodeValue;

var myDiv = document.createElement('div');
myDiv.innerHTML = productId +' '+ productName +' '+ productDescription;   
container.appendChild(myDiv);   
}
}

Code is working fine.. its show me all products which are in the table let say 11 rows of products. It show all 11 rows name, id and description inside in index page on this <div id="Products"></div>.

But I want that all the value means id, name and description inside of those span id i have made in index page.
I tried a lot but i couldt find the solution.
When i use the simple way like.
document.getElementById("GettingProductId").innerHTML = productId;
its show me value in the span but only one value but i want all products id inside of this "GettingProductId" not one.

please help me i really need it.. please hope i get answer fast.
thanks in advance.
Avatar billede wanze Nybegynder
25. april 2011 - 17:32 #1
If you want to make it easy on yourself, you should use JSON instead of XML and familiarize yourself with the jQuery JavaScript library.
Avatar billede sibga Nybegynder
25. april 2011 - 18:15 #2
yes I know but I have to make in AJAX.
There is no wrong with code this code is working but i just want to know how can i get all id or name in to thos html tag.

I know i have to change code of ajax but i tried and it gives only one id not all ids from table.
can you point to someone who can help me... thanks for ur answer
Avatar billede wanze Nybegynder
25. april 2011 - 18:59 #3
I'm not sure I understand what you're trying to accomplish. You will probably have a better chance of getting help in a forum where they speak your native language. I'm afraid your English is a bit of a language barrier.
Avatar billede sibga Nybegynder
25. april 2011 - 19:17 #4
ok ... thanks for your help...
For those who like to help me please try to answer.

Those who understand ajax they know what im talking about.

right now all data from database show in one place in index page.
which is <div id="Products"></div>

And my question was how can i put the data into those span id's.
  <span id="GettingProductId"></span>
  <span id="GettingProductName"></span>
  <span id="GettingProductDetail"></span>

Means GettingProductId going to show all product_id from tabel
and the same with name and detail.

just that was question. Its a small changes in ajax code and then it will work i know but i cound't make it to work.

please help me...
Avatar billede tjens Nybegynder
25. april 2011 - 21:48 #5
This line replaces the content each time, so you only see the last product:

document.getElementById("GettingProductId").innerHTML = productId;

Try this:

document.getElementById("GettingProductId").appendChild( document.createTextNode( productId + ' ') );
Avatar billede tjens Nybegynder
26. april 2011 - 11:48 #6
And this line

  var numberOfProducts = myResponse.getElementsByTagName("Id","Name","Description","Image","Price","Quantity"... ...

must be

  var numberOfProducts = myResponse.getElementsByTagName("Id");

Since counting one type of tag is sufficient for getting number of products, and because getElementsByTagName() only takes one parameter.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester