0

Hi Guys!
I'm currently trying to refresh a DIV on my page that is linked to MYSQL (changes if database has 1 or more rows) without refreshing the entire page, I've tried using javascript although it didn't seem to work and so Im back to the drawing board, Im not very experienced with Jquery or Javascript to do it on my own, I only know PHP, CSS, HTML the easier ones :P

So I was hoping someone could shed some light on how I could possibly go about this... - I don't want to use Iframes either as the the div is a button which opens a hidden div on the page (parent page) so I don't think iframes would be best either

Any help would be great!
Thank you in advance :)

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by rizvihaider72
0

Hi, I would do it using JQuery.

I just didn't understood if the refresh will be trigerred by some user event or if it should refresh automatically.

function requestDivContent()
{
	var url = "http://myPage.com"; // URL to get the data
	var data = // Paramters, PHP Ex.: $name = $_POST["name"];
	{
		name : "name", 
		id : 20 
	};
	
	$.post(url, data, showDivContent); // Send the request
	
	// if you don't need to pass any data
	// $.post(url, showDivContent);
}

function showDivContent(divContent)
{
	$("#divContent").html(divContent); //Replace any existing content
}

// -------------------------------------------
// IF it should do automatically

var oTimer;

function initMySQLLink()
{
	var time = 5000; // 5 seconds
	oTimer = setInterval(requestDivContent, time); // Start the timer and save the it's reference
}

function stopMySQLLink()
{
	clearInterval(oTimer); // Cancel the timer
}

Links:
$.post()
setInterval()

It should work (I didn't test it), but there is no error handling. To handle errors you should use $.ajax()

Here is a condensed version:

var oTimer;

function refreshDivContent()
{
	oTimer = setInterval
	(
		function()
		{
			$.post
			(
				"http://myPage.com", 
				{name : "name", id : 20}, 
				function(divContent)
				{
					$("#divContent").html(divContent);
				}
			); 
		},
		5000
	);
}

Hope it helps.

Edited by AleMonteiro: Explaning that I didn't test the code

0

You can do it using AJAX

Edited by Ezzaral: Snipped url. Please restrict personal links to your user signature.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.