CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Ajax and Design (http://www.codingforums.com/forumdisplay.php?f=55)
-   -   How can i get data from a mysql database with ajax (http://www.codingforums.com/showthread.php?t=273810)

gazaian1 09-22-2012 05:05 AM

How can i get data from a mysql database with ajax
 
Hello all i am trying to load random testimonials i have stored in my database but i can't seem to get it to work. it works like this random content shown without page refresh.

Code:

<script type="text/javascript">
function AJAX(){
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
return xmlHttp;
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
return xmlHttp;
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
return xmlHttp;
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}
}

function showUser(str)
{
if (str=="")
  {
  document.getElementById("cont_block_01").innerHTML="";
  return;
  }
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("cont_block_01").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","/wp-content/themes/speedy/featured_testimonial.php"+str,true);
xmlhttp.send();
}

$(document).ready(function() {

var refreshId = setInterval(function() {
$("#cont_block_01").fadeOut("slow").load("showUser(str)").fadeIn("slow");
}, 10000);

$.ajaxSetup({ cache: false });

});
</script>

My php code:

PHP Code:

<?php
$query 
mysql_query("SELECT * FROM testimonial WHERE active=1 LIMIT 1") or die(mysql_error());
$count=mysql_num_rows($query);

$uploadDir 'wp-content/themes/speedy/testimonial/'//Image Upload Folder
$path 'http://speedycarloans.ca/';
$fileName addslashes($_FILES['pict']['name']);

$filePath addslashes$path $uploadDir $fileName);

while (
$query_row mysql_fetch_array($query)){
$pic=$query_row['picture'];
$comm=$query_row['comment'];
$namee=$query_row['name'];

?>
<div class="block_photo">
<?php if($pic !=$uploadDir){?>
<img src="<?php echo $path $pic?>" width="71" height="104" />
<?php } else{?><img src="<?php bloginfo('template_url'); ?>/images/pic_02.jpg" width="71" height="104"/><?php ?>
</div>
<p class="para"><?php echo $comm?><br/>
<strong class="nami"><font color="#b37704"><?php echo $namee ?></font></strong></p>
                      
<a class="readmoree" href="testimonials"><img  src="<?php bloginfo('template_url'); ?>/images/read_more.png" width="95" height="29" align="right"/></a>
<?php }?>


d'Anconia 09-22-2012 10:15 PM

Call me crazy but I don't see anywhere that your PHP returns any response text at all. Ajax sends data to your server but your server will do the query and should send some text back. You can use the echo function to produce this response text (there are probably other ways as well).

There also might be other issues with your code, this is just what I saw first.


All times are GMT +1. The time now is 10:00 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.