reading php-mysql data through xmlhttprequest

Discussion in 'Javascript' started by foraci@gmail.com, Jan 11, 2007.

  1. Guest

    Dear group,
    I have tried for the whole week to get the following done. But all I
    face is failure. My task is to fetch data from mysql through php and
    display the fetched row in javascript with xmlhttprequest object
    without using xml. Directly using the open function with GET and POST.
    What I get is all the data are displayed. But I want to process
    each of mysql in the javascript with the help of xmlhttprequest object.
    Is it possible or I am going in a wrong direction. Any help is
    appreciated. Since I am a beginner I could not able to do more. If this
    is not the group then pls direct me.

    Following is the code:

    The .html file: (I did not chk for IE and onreadystatechange)
    ---------------
    <html>
    <head>

    <script language="javascript">

    var xmlhttp, tmp;

    function start() {
    var row_0,row_2,row_1;
    xmlhttp = new XMLHttpRequest();

    xmlhttp.open("GET",'noxml.php',false);
    xmlhttp.send(null);
    tmp=xmlhttp.responseText;

    var mybody = document.getElementsByTagName("body")[0];
    mytable = document.createElement("table");
    mytablebody = document.createElement("tbody");

    for(var j = 0; j < 3; ++j)
    {
    mycurrent_row = document.createElement("tr");
    for(var i = 0; i < 3 ; ++i)
    {

    mycurrent_cell = document.createElement("td");

    currenttext = document.createTextNode(tmp) ;

    mycurrent_cell.appendChild(currenttext);
    mycurrent_row.appendChild(mycurrent_cell);
    }
    mytablebody.appendChild(mycurrent_row);
    }
    mytable.appendChild(mytablebody);
    mybody.appendChild(mytable);
    mytable.setAttribute("border","2");
    }



    </script>
    </head>

    <body onLoad="start()">

    </body>

    </html>

    The .php file:
    -------------

    <?php
    $user = "sathya";
    $passwd = "sathya";
    $dbname = "ajax";
    $conn = mysql_connect("localhost",$user,$passwd);
    global $row_0 ,$row_1,$row_2;
    if(!$conn)
    die("error in mysql connection".mysql_error());
    $db = mysql_select_db($dbname);
    $quere = "select * from product";
    $countris = mysql_query($quere);
    if(!$db)
    die("error in mysql db".mysql_error());
    if(!$countris)
    die("error table".mysql_error());

    while($row = mysql_fetch_row($countris))
    {

    $row_0 = $row[0];
    $row_1 = $row[1];
    $row_2 = $row[2];

    $_POST['row_0'] = $row_0;
    $_POST['row_1'] = $row_1;
    $_POST['row_2'] = $row_2;

    echo $row_0.' ', $row_1.' ', $row_2.'<br>';
    }


    ?>

    The .sql has 3 col

    Thanks for any help.
     
    , Jan 11, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajive Narain
    Replies:
    0
    Views:
    2,084
    Rajive Narain
    Sep 18, 2009
  2. neha shena
    Replies:
    1
    Views:
    281
    Dennis Lee Bieber
    Oct 29, 2010
  3. Allen Marshall
    Replies:
    1
    Views:
    215
    Gunnar Hjalmarsson
    Jan 24, 2004
  4. fool
    Replies:
    0
    Views:
    120
  5. VK
    Replies:
    24
    Views:
    1,170
Loading...

Share This Page