Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 09-22-2012, 10:15 PM   PM User | #2
d'Anconia
Regular Coder

 
d'Anconia's Avatar
 
Join Date: Jan 2010
Location: Tempe, AZ
Posts: 142
Thanks: 15
Thanked 5 Times in 5 Posts
d'Anconia is an unknown quantity at this point
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.
__________________
Powerful ideas for all lovers of personal and political freedom:
Freedomain Radio
Free Talk Live
d'Anconia is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, html, javascript, mysql, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:51 PM.


Advertisement
Log in to turn off these ads.