www.webdeveloper.com
Results 1 to 5 of 5

Thread: Passing value from JS to PHP and to SQL?

  1. #1
    Join Date
    May 2012
    Posts
    16

    Passing value from JS to PHP and to SQL?

    I am trying to get a value from a JavaScript variable and add it to a MySQL database using PHP, but my code does not seem to work. I know that this is a PHP forum, but it's kinda hard to ask this anywhere else.

    This is my code where I actually pass the value (variable countForPHP) using XHR:
    Code:
    xmlHttp.open("GET", "count.php?r=" + Math.random + "&count=" + countForPHP, async);
    Here's how I get the value sent from the XHR:
    Code:
    $counter = $_GET["count"];
    $pdo = new PDO('mysql:host=host;dbname=database;', 'username', 'password');
    $sql = "UPDATE tbl_click SET click = click + ($counter)";
    $pdo->exec($sql);
    Does anyone know how to solve this problem?

  2. #2
    Join Date
    Jan 2009
    Posts
    3,346
    I think you might have meant:
    Code:
    $_GET['count'];

  3. #3
    Join Date
    Apr 2010
    Posts
    227
    Hi

    Sorry for the lecture but here it is anyway.

    The key to writing good code is being easily able to debug said code.

    Youve provided yourself with zero means to do so, that means every line of code youll ever write will potentially be a huge chore to make work.

    You need to be able to know exactly where the code is breaking down.

    ANyway, nothing more from me, best of luck.

  4. #4
    Join Date
    May 2012
    Posts
    16
    Quote Originally Posted by criterion9 View Post
    I think you might have meant:
    Code:
    $_GET['count'];
    Aren't " and ' the same? Maybe my problem is where I add the variable in the SQL Query?

  5. #5
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    Are you receiving any output from the PHP file in question? I.e., is xmlhttp.responseText empty?

    Are you certain your file paths are correct? Remember that with AJAX calls you need to make sure the path of the PHP file is relative to the currently active page, not relative to the file on the server.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles