tags:

views:

184

answers:

3

I'm an AJAX novice and I'm having major trouble trying to get data out of mySQL and into my javascript function.

What I want to do is loop through my data in php and somehow send that data into various named divs on the page.

Here's the code from my javascript page:

function loadPageContent(){

var projectID = getQuerystring('pid');
var templateID = getQuerystring('t');

xmlHttp=GetXmlHttpObject()

if (xmlHttp==null){
 alert ("Browser does not support HTTP Request")
 return
} 

var url="getImages.php"
url=url+"?projectID="+projectID
url=url+"&templateID="+templateID
xmlHttp.open("GET",url,true);
 xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 

document.getElementById("statusdebug1").innerHTML=xmlHttp.responseText;
            }
        }

xmlHttp.send(null);
} 

Here's the code for my php page:

    <?php 

$projectID = $_GET["projectID"];
$templateID = $_GET["templateID"];

include_once('includes/php/conn.php');

$sql ="select * FROM imageSel WHERE projectID='$projectID' AND templateName = '$templateID'";

$results=mysql_query($sql, $link);

if(!($mysql_rs = mysql_query($sql, $link)))
die("Error in executing query");

echo "<script language='JavaScript'>";

while($row =mysql_fetch_assoc($results) ){ 

$imageSelID = $row['imageSelID'];
$templateName = $row['templateName'];
$tNode = $row['box'];
$image = $row['image'];


$sql2 ="select * FROM products WHERE productid='$image'";

if(!($mysql_rs = mysql_query($sql2, $link)))
die("Error in executing query");

//Retrieve values
$row2 = mysql_fetch_array($mysql_rs);

$productname = $row2['productname'];
$subcategoryid = $row2['subcategoryid'];


    $sql3 ="select * FROM subcategory WHERE subcategoryid='$subcategoryid'";

    if(!($mysql_rs = mysql_query($sql3, $link)))
    die("Error in executing query");

    //Retrieve values
    $row3 = mysql_fetch_array($mysql_rs);

    $foldername = $row3['foldername'];
    $foldername = strtolower($foldername);


$theImage = '<img src="images/lowres/' . $foldername . '/' . $productname .'" />';

echo "document.getElementById(".$tNode.").innerHTML=".$theImage.";";

}

echo "</script>";

?>
A: 

That's typically not how I would implement AJAX.

AJAX, when done correctly, should send data to the browser, then let the browser decide what to do with it.

Try using json_encode to put the data you want to send to the browser in JSON format, then on the Javascript end use a JSON library to decode the data then handle it appropriately.

Good luck!

Matchu
I tried using json_encode and brought it into javascript with the_object = JSON.parse( xmlHttp.responseText ); Is there anyway though of looping this data through something like this:document.getElementById(tNode).innerHTML = theInnerHTML
Lindsay
Generally I'd be encoding JSON into the data, not HTML, but the general looping concept is this: given a var objects: for(var i in objects) { var object = objects[i]; /* do what you want with the object data */ }
Matchu
A: 

Dear bro,

please use this code :

xmlHttp.open("GET",url,false);

because if you are keeping its true then it will call asynchronize ajax.

Please correct if i am wrong.

chirag
No, not really, as per http://www.w3.org/TR/XMLHttpRequest/#the-xmlhttprequest-interface, `true` for the third argument means "do this asynchronously". Setting it to `false` would take the "A" out of AJAX :)
shylent
A: 

You are setting the Ajax response to the innerHTML of the div. But you output javascript in your PHP (which becomes your Ajax response). Try outputting just the img tag without the javascript wrapper. Or, even just outputting "Hello world";

EDITED TO ADD: You're using javascript getElementById and innerHtml on the client side and the server side. It's redundant. You probably want to keep it on the client side, ie, your html and javascript

I do want to keep it on the client side. I essentially want to run this javascript function with my data coming from the database to load my content into their respective divs.function loadimages(theItem, tNode, stackLoc){ theInnerHTML = document.getElementById('locationName' + theItem).innerHTML theRegEx = 'style="border: 1px dashed rgb(204, 204, 204); margin: 1px;"' theInnerHTML = theInnerHTML.replace(theRegEx, '') theInnerHTML = theInnerHTML.replace('thumbnails', 'lowres') document.getElementById(tNode).innerHTML = theInnerHTML + tNodeStack[stackLoc]}
Lindsay
Actually I really just need my database data looping through document.getElementById(tNode).innerHTML = theInnerHTML and out to my page. How do I get Ajax to spit this out?
Lindsay
Use the AJAX side to get the data in, but don't do any formatting of it with HTML and Javascript. Are you able to get any content in xmlHttp.responseText? If so, try taking that in as a string and parsing it. You want to format it on the PHP side as plain text, nothing fancy, and set it up so you can deal with it in javascript. For example:mydiv1::myimage.jpgmydiv2::yourimage.jpgThe "::" is meaningless -- just an example of giving yourself something easy to split the string on. That said, you may want to go with JSON as suggested above if you're dealing with lots of structured content.