Results 1 to 2 of 2

Thread: # of clicks insert through OnCLick Jscript function

  1. #1
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Question # of clicks insert through OnCLick Jscript function

    Hi, I am trying to make a code (which is a mix of Jscript and PHP) where when a person clicks on a link, it inserts the amount of recorded clicks into mysql...the code looks like this:

    Code:
    <?php
    
    $query = "INSERT INTO clicks (click, date) VALUES('$click', '$date') ");
    
    echo "<script type='text/javascript'>";
    echo "function clicks(){"
    mysql_query( $query );
    echo "}";
    echo "</script>";
    
    ?>
    And here is an example hyperlink code:
    Code:
    <a href="click.php?url=http://www.google.com" onClick="clicks()">Google.com</a>
    But it isn't inserting the data into mysql. Why is this not working? :?

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Javascript doesn't use PHP.

    PHP is run server side, and processed to plain text (including css, html, javascript, etc.), then output as that. It doesn't run while you are looking at the page.

    PHP can't be used within a javascript function like that.

    The way you could do that would be to run the server side script again.

    For example, you could run that at the top of your page automatically, and have the link simply reload the page, running it each time.

    Or, use AJAX, which is a way of running a PHP script by calling it with Javascript, basically loading the page in the background (to javascript, not to your browser). That's more complex, though.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •