beennn Posted September 27, 2011 Share Posted September 27, 2011 i hope this is the correct forum, sorry if its not; <a href="#" onclick="count();"> <script type="text/javascript">function count() {<?mysql_query("UPDATE table SET downloadCounter = downloadCounter + 1");?>return false;}</script> Each time you refresh the page it carries out the count function, any idea how i can stop this from happening? Link to comment Share on other sites More sharing options...
Ingolme Posted September 27, 2011 Share Posted September 27, 2011 Javascript and PHP don't mix. To the PHP parser, your code looks like this: Some irrelevant text<?mysql_query("UPDATE table SET downloadCounter = downloadCounter + 1");?>Some more irrelevant text If you want to update the database without reloading the page, you will need AJAX. Link to comment Share on other sites More sharing options...
beennn Posted September 27, 2011 Author Share Posted September 27, 2011 <a href= "#" onclick="count();">click me</a> <script type="text/javascript">function count() {<? mysql_query("UPDATE table SET downloadCounter = downloadCounter + 1");?>return false;}</script> This works however it executes mysql_query("UPDATE table SET downloadCounter = downloadCounter + 1"); when the page loads and instead i want it to only execute when the user presses the button, how can i fix this? any help would be great Link to comment Share on other sites More sharing options...
Synook Posted September 27, 2011 Share Posted September 27, 2011 If you think about it, you will realise that the execution of PHP and JavaScript are not related in any way - on the server, your page is treated as a normal program and is run through the PHP interpreter. Then the result is sent as a complete page to the client, and then the browser is responsible for executing the JavaScript. If you look at the source code in your browser you will see that the PHP code does not appear. To do what you want you will need to use AJAX. P.S. moved to the JavaScript forum. Java is not related to JavaScript. Merged. Please don't double post. Link to comment Share on other sites More sharing options...
Ingolme Posted September 27, 2011 Share Posted September 27, 2011 I already replied to your other thread. I'll lock this one, since it's duplicate. Link to comment Share on other sites More sharing options...
beennn Posted September 27, 2011 Author Share Posted September 27, 2011 sorry, i know java and javascript arent related, missread java and i double posted because i wasnt sure which forum to post this in, sorry. Iv read through the AJAX tutorial on w3 and i cant see anything which says about updating a database, only reading from one, could some plese explain how i do this? Link to comment Share on other sites More sharing options...
Ingolme Posted September 27, 2011 Share Posted September 27, 2011 You use AJAX to execute a PHP file. The PHP file that it executes can write to the database. Link to comment Share on other sites More sharing options...
beennn Posted September 27, 2011 Author Share Posted September 27, 2011 oooh, thank you after you said that, iv changed to: <script type="text/javascript">function count() {xmlhttp.open("GET","count.php,true);xmlhttp.send();} created a seperate php file, which contains: <? mysql_query("UPDATE table SET downloadCounter = downloadCounter + 1");?> is there something im missing? Link to comment Share on other sites More sharing options...
Ingolme Posted September 27, 2011 Share Posted September 27, 2011 Yes, you're missing one character: "See if you can find where. It shouldn't be so hard, your code is really short. Actually, you also have to declare what xmlhttp is. It's an XMLHTTPRequest object. Link to comment Share on other sites More sharing options...
thescientist Posted September 27, 2011 Share Posted September 27, 2011 a closing quote after "count.php if that was a typo, then are you checking for errors? or is it or isn't it working? Link to comment Share on other sites More sharing options...
beennn Posted September 27, 2011 Author Share Posted September 27, 2011 ok iv closed "count.php" Actually, you also have to declare what xmlhttp is. It's an XMLHTTPRequest object. Iv done this, im not sure whether its correct because its still not working: xmlhttp=new XMLHTTPRequest(); all together: <script type="text/javascript">function count() {xmlhttp=new XMLHTTPRequest();xmlhttp.open("GET","count.php",true);xmlhttp.send();}</script> with count.php: <? mysql_query("UPDATE table SET downloadCounter = downloadCounter + 1");?> Link to comment Share on other sites More sharing options...
thescientist Posted September 27, 2011 Share Posted September 27, 2011 it be worth your time to study the tutorialshttp://www.w3schools.com/ajax/ajax_xmlhttprequest_create.asp and learn how to check for errors in the console of your preferred web browser. Currently you are not creating an instance of the XHR object. (incorrect casing) Link to comment Share on other sites More sharing options...
beennn Posted September 27, 2011 Author Share Posted September 27, 2011 ooooh, Http not HTTP, thank you very much, all working. and sorry mods for being a pain ^.- thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted September 27, 2011 Share Posted September 27, 2011 Just as a note, that code won't work in old versions of IE. It's good for all modern browsers, but there are other things you need to do to keep old versions of IE happy. IE6 doesn't support the XmlHttpRequest object, it uses something else. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.