Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    52
    Thanks
    24
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder d'Anconia's Avatar
    Join Date
    Jan 2010
    Location
    Tempe, AZ
    Posts
    149
    Thanks
    16
    Thanked 6 Times in 6 Posts
    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.
    Datagonia Web (My Portfolio)

    Powerful ideas for all lovers of personal and political freedom:
    Freedomain Radio
    Free Talk Live


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •