Let me start by saying I am not a programmer, so please don't answer with something like "well, you just need to plug the AJAX query into the PHP structure with a circular reference to the wimblebobble", because I will have no clue what that means I'm hoping for answers that even a Bear of Very Little Brain like myself will be able to decipher.
I have a navbar containing the days of the week in the central part of my Joomla website. When a visitor clicks one of the days, a PHP script submits a hidden HTML form, and retrieves and displays a list of radio shows that are scheduled to take place on that day.
The problem I'm trying to solve is that the entire page refreshes when the user clicks one of the days. I'd rather that just the schedule refresh, not the entire page.
Now, I've searched the web for days, and tried things like adding "onSubmit='return false;" to the form element, and tried adding "return false;" to the onClick statement belonging to each day of the week. Nothing seems to work--the entire page still refreshes.
Does anyone have any insight into what might work in this situation?
You are going to have to either use AJAX or you could iframe it (I recommend AJAX) If the form is submitted and AJAX is not used, the page has no choice but to refresh. This is what I used when I was first learning AJAX, maybe it will help you: http://www.tizag.com/ajaxTutorial/
I see, thanks--I'm not averse to using AJAX or something else, if that's what's needed. I had a look at the AJAX tutorial (thanks for the link), but as I said I'm really not a programmer, and this looks like gobbledygook to me
Based on the code snippet I posted above, would anyone be able to write out the appropriate code to prevent the page from refreshing? I'm sure it's just a line or two, but again--I'm not a programmer
A few tweaks to make this work:
Give the main div that has class="cmschedule" the ID of "viewSchedule"
Change your link generation to look like this: <a href="#" onclick="getPrograms();" rel="<?php echo date('Y-m-d', strtotime('monday')); ?>">
(See how much cleaner that is?)
include this in the header or footer:
(jQuery would have been better but Im lazy and this is for free. XD)
Code:
function getPrograms(){
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
alert("Your browser does not support AJAX!");
return false;
}
}
}
var qstring = "date="+document.getElementById(this).rel;
ajaxRequest.open("POST", "programs.php", true);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", qstring.length);
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.getElementById("viewSchedule").innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.send(qstring);
return false;
}
And programs.php needs to contain the script that gets the schedule.
Wow, thank you for taking the time to come up with that! It didn't work, unfortunately, but I'm sure that's more due to something on my end than any fault in the code.
I'm embedding the getPrograms function in a PHP script; does that make a difference?
Right now this is how my default.php looks in my Joomla component (I just changed the Mon Tues Wed links to test):
I've just spent some time knocking together an example of how you might do it using AJAX and jQuery;
it took me slightly longer than I thought it would, as I had to re-acquaint myself with how jQuery handles AJAX and JSON.
Assuming that the information about your various shows is going to be dynamic - i.e. subject to change week on week - it makes sense to have the data stored in an external file on your server - in this case 'showdata.json'. JSON is essentially an information interchange format using JavaScript's object notation. (Apologies if this is already turning into unreadable gobbledygook for you...)
However for the purpose of this demo I just created an object with data for Monday and Tuesday:
Code:
var __myNamespace = [];
__myNamespace['showdata'] = {
"monday":"Insert content for Monday here",
"tuesday":"Insert content for Tuesday here"};
$(document).ready(function() {
$('.weekday').click(function() {
weekday = $(this).attr('id');
$('#shows').html(__myNamespace['showdata'][weekday]);
})
});
And the jQuery function triggers when an element of class 'weekday' is clicked, ascertains the unique id of the element, fetches the corresponding data from our data object and adds it to the 'shows' div.
Bookmarks