View Single Post
Old 09-22-2012, 05:05 AM   PM User | #1
gazaian1
New Coder

 
Join Date: Aug 2011
Posts: 52
Thanks: 24
Thanked 0 Times in 0 Posts
gazaian1 is an unknown quantity at this point
Question 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 }?>

Last edited by gazaian1; 09-22-2012 at 05:07 AM..
gazaian1 is offline   Reply With Quote