0

what my goal is to use AJAX to call my php function:

function getAns($mysqli)
{
    $stmt = $mysqli->prepare('
    SELECT `user_id`,`user_name`, `user_ans`
    FROM `tbl_user`
    WHERE `user_ans` != ""');
    $stmt->execute();
    $stmt->bind_result($id, $user, $ans);
    $O ="";
    $x = 1;
    $O.="<table><form action=\"#\" method=\"POST\">";
    while($stmt->fetch())
    {
        if($x%2)
        {
            $O.= "<tr>
                    <td>".$user."</td><td>".$ans."</td><td><input id=".$id." type=\"submit\" name=\"pts\" href=\"#\" value=\"+\"></td>
                </tr>";
            $x++;
        }
        else
        {
            $O.= "<tr>
                    <td>".$user."</td><td>".$ans."</td><td><input id=".$id." type=\"submit\" name=\"pts\" href=\"#\" value=\"+\"></td>
                </tr>";
            $x++;
        }
    }
    $O.= "</form></table>";
    // close statement
    $stmt->close();
    return $O;
}

at a set time interval,say every 5 seconds, using AJAX/jQuery. I am trying to have an answer section, which is in a div, auto grab things from my database and echo them onto the page.

My HTMLthat i was trying to have them put into looks like this:

 <div id="ans" class="box3"><!--PHP Students answers -->
    <form id="ans" action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
        <?php echo getAns($mysqli);?>
    </form>
</div>

I understand a little bit how it works but I don't understand the code it takes to get there. I am new to JavaScript/jQuery/AJAX but I am trying to use more of it in my code, so if anyone could elaborate it would be much appreciated, thanks!

7
  • "so if anyone could elaborate it would be much appreciated" I was thinking the same thing. What is the issue here? Are you getting errors? What's happening? Commented Apr 12, 2016 at 17:51
  • 2
    There are many tutorials on using AJAX with jQuery. If you want it to run every 5 seconds, look at Javascript's setInterval. Commented Apr 12, 2016 at 17:52
  • 1
    And get in the habit of creating variable names that make sense/are descriptive of what they're holding. $O isn't a good variable name. Instead, use something like $html_output Commented Apr 12, 2016 at 17:52
  • My issue is i can't seem to find a way to reload a div , and the php function inside of it so it will execute again, without reloadin the page, everything i have found on the stackoverflow doesnt really show what everything does and just has code, not an explanation. Commented Apr 12, 2016 at 17:53
  • @Barmar i wasn't able to find any that made a lot of sense, makes it hard since im new to AJAx and JQuery, is there on eyou can recommend maybe? Commented Apr 12, 2016 at 17:56

2 Answers 2

2

with this function you can run ajax after every 5 minutes. You just need to pass user id value in ID variable, which will fetch all answer of that user after every 5 minutes.

And run your sql query in get_data.php file.

$(document).ready(function(){
var timer, delay = 300000; //5 minutes counted in milliseconds.
var ID = $( "td :submit" ).val();
var info = 'userID=' + ID ;
timer = setInterval(function(){
   $.ajax({
            url: "get_data.php",            
            type: "POST",
            data: info,
            success:function(data){
                $("#ans").html(data);
            }
            });
}, delay);
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can use ajax to call a php file that includes your php code it will execute it in the server then it will returns the result back to you

$.ajax({
        url:"your_php_file_path.php",
        success: function(result){
               //result is a variable that stores the value sent back from your php 
              // in your case $O
        }
 });

You can put this code in a js function, finally you will have to include Jquery library so you can use ajax

4 Comments

how would i mak eit call every X seconds? just setInterval();? and for the result do i need to put anything in the , or is that done when the php executes?
It depeneds, if you are going to use the output of your php in the calling function or not. We usually use the output to display some date retrieved from the database. How to call the function every X seconds i have no idea actually and seems some guys already answered your question above
yes i want to use the ouput from the new php function call, to replace the current contents of the form as seen in my original question.
You can write the result variable to the div using $("ans").val(result); you might need to delete the form ans using Dom functions before you write the new result to the div. Just in case the $().val() duplicates your form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.