eleven0 Posted November 22, 2008 Share Posted November 22, 2008 <?PHP $quotes = array( "This is my quote", "To be or not to be", "<b>You can</b> <u>use html</u>"); $count = count($quotes); $random = (rand()%$count); echo $quotes[$random]; ?> I have this script and it shows the array on random. everytime page reloads, it shows something different. I want to do the same thing with a button(like a link) without reloading the page. I pretty much don't know anything about Ajax or javascript. Can someone point me to the right direction? so i can do more search on it. Thanks. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted November 23, 2008 Share Posted November 23, 2008 Use a framework, like jQuery or Prototype. Here is a simple jQuery example, not tested thought: $("#request-new").click(function(){ $.ajax({ type: "GET", url: "./headline.php", error: function(xml, status, error){ alert("Sorry but we encountered an error."); }, success: function(data, status){ $("#latest-headline").html(data); } }); }); Then, all you need is a file called headline.php that has your above code in it. You also need to surround the area where the headline is appearing in a div with an id attribute of latest-headline. And your button or link needs an id attribute of request-new. Like this: <div id="latest-headline"> <!-- You can include the PHP script here to start with, so that a headline gets shown --> </div> <a id="request-new">New Random Headline</a> Good luck. Quote Link to comment Share on other sites More sharing options...
eleven0 Posted November 23, 2008 Author Share Posted November 23, 2008 Thanks for the quick reply. That didn't work though. "New Random Headline" is not clickable. Here is what i did; I added the script to my page. I placed following <script type="text/javascript"> $("#request-new").click(function(){ $.ajax({ type: "GET", url: "./headline.php", error: function(xml, status, error){ alert("Sorry but we encountered an error."); }, success: function(data, status){ $("#latest-headline").html(data); } }); });</script> between <head> and </head>. Then; <div id="latest-headline"> <?PHP $quotes = array( "This is my quote", "To be or not to be", "<b>You can</b> <u>use html</u>"); $count = count($quotes); $random = (rand()%$count); echo $quotes[$random]; ?> </div> <a id="request-new">New Random Headline</a> New Random Headline is not even clickable. What am i doing wrong? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted November 23, 2008 Share Posted November 23, 2008 Do you have jQuery? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.