Jump to content

Call php files using JQuery and Ajax


thereaper87

Recommended Posts

Hello there,

 

I have a problem, that I am trying to find some literature about, or maybe an example, or a demo; or just plain help.

 

What I am trying to do, is to have a query ran every 2 seconds, to display a number.

 

Here is what I am trying to implement.

//index.php

<script>
function updateShouts(){
    // Assuming we have #shoutbox
    $('#shoutbox').load('latestShouts.php');
}
setInterval( "updateShouts()", 2000 );
</script>

// to merge that with
// latestShouts.php



$total = mysql_query("SELECT COUNT(*) FROM $table_and_query"); 
        $total = mysql_fetch_array($total); 
        return $total[0]; 

 

I know that will not work, because I really don't know much about ajax. I read all of the tutorials on w3, but they didn't really have an example I was looking for.

 

Any help is appreciated.

 

 

 

Link to comment
Share on other sites

This is the kind of jQuery AJAX call you're going to want:

 

var data = "&settingone=1&settingtwo=2";
$.ajax({
        url: "index.php?option=dothis",
type: "POST",
data: data,
success: function(string, variable){
	if(variable === "success"){	
	    alert(string);
	}
	else{
	    alert(string);
	}
}
});

 

It's a fairly straightforward piece of code. Data is anything you want to POST (or GET if you change type: to equal GET) to the Url.

The function returns a string and a variable. The variable tells whether or not the AJAX call was successful or not and the string is whatever the Url returned.

 

On it's own this will continuously make AJAX calls to that page.

Link to comment
Share on other sites

Awesome! Just to be sure, I wrap that in script tags correct?

 

Yeah, or include it in an external JS file. Also, make sure you call it inside of:

$(document).ready(function(){

    // jQuery code goes here

});

 

It's just a jQuery thing to make sure it doesn't try to run before the page is finished loading.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.