Jump to content

Updates On Load Not On Click


beennn

Recommended Posts

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

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

<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

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

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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...