December 7, 201015 yr Hey, Basically I have a .txt file with a sentence on each line, and every time I press the a button it chooses a new one... The Button: <input type="button" value="Change Article" id="articleswap" onclick='$("#articalinfo").load("newarticle.php")'> The php (in a newarticle.php file): <?php $arr = explode( "\n", file_get_contents( "artic.txt" ) ); echo $arr[ rand( 0, count( $arr ) - 1 ) ]; ?> Works fine in FF, every time the button is clicked it randomly chooses a new line and displays it. However in IE I can only click it, a line is displayed and then it wont work again :s Anyone know why? Also: Does anyone know how I could display the lines one after the other instead of randomly?
December 7, 201015 yr Try <input type="button" value="Change Article" id="articleswap" onclick="$('#articalinfo').load('newarticle.php', function() { return true; }); return true;" />
December 7, 201015 yr If that doesn't work, try specifying the full URL to the newarticle.php file, eg. http://www.mysite.com/newarticle.php
December 7, 201015 yr Author Do you mean: onclick="$('#articalinfo').load(' http://www.elliothesp.co.uk/assignment/newsarticle.php', function() { return true; }); return true;" Do you mean that? If so it doesnt load it at all
December 7, 201015 yr That should work (as long as its on the same domain). Can you post the full HTML code?
December 8, 201015 yr Author Ah well i put the jQuery in an new file Dont want any more kittens to perish now D: Basically all it is: index.php; the button <div id="articalbutton"><input type="button" value="Change Article" id="articleswap"/> </div> the space: <div id="articalinfo"> <!-- DUMMY ARTICLE --> HTML:<br /> HTML, which stands for HyperText Markup Language, is the predominant markup language for web pages. A markup language is a set of markup tags, and HTML uses markup tags to describe web pages. HTML is written in the form of HTML elements consisting of "tags" surrounded by angle brackets (like ) within the web page content. HTML tags normally come in pairs like and . The first tag in a pair is the start tag, the second tag is the end tag (they are also called opening tags and closing tags). The purpose of a web browser is to read HTML documents and display them as web pages. The browser does not display the HTML tags, but uses the tags to interpret the content of the page. HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms. It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. </div> ^^ This is so that when the page loads there is already something there, and when the button is clicked it changes it. .js file: $('#articleswap').click(function() { $("#articalinfo").load("http://localhost/assignment/newarticle.php"); }); Running it through xampp thus the local host atm. Then in a seperate newarticle.php file: <?php $arr = explode( "\n", file_get_contents( "artic.txt" ) ); echo $arr[ rand( 0, count( $arr ) - 1 ) ]; ?>
December 9, 201015 yr Is IE caching the response? Try this code... $('#articleswap').click(function(e) { $("#articalinfo").load("http://localhost/assignment/newarticle.php?t=" + Math.random()*99999); e.preventDefault(); }); If that doesn't work, you could try ditching the load function and use getJson() or post() or get() to get the random article and use html() to update the div.
December 9, 201015 yr Author Ahh thank you sir! Never even thought about that Stupid IE, wonder why it has to do that :s Thanks so much anyway!
Create an account or sign in to comment