0

I'm trying to execute a PHP script that updates a MySQL DB on click of an image. I'm using a snippet I found online to do so:

function execute(filename,var1,var2)
{
    var xmlhttp;
    if(window.XMLHttpRequest)
    {
        //Code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
        //Code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        alert("Your browser does not support AJAX!");
    }

        var url = filename+"?";
        var params = "id="+var1+"&complete="+var2;

        xmlhttp.open("POST", url, true);

    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState==4)
        {
            //Below line will fill a DIV with ID 'response'
            //with the reply from the server. You can use this to troubleshoot
            //document.getElementById('response').innerHTML=xmlhttp.responseText;

            xmlhttp.close;
        }
    }

        //Send the proper header information along with the request
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length);
        xmlhttp.setRequestHeader("Connection", "close");

        xmlhttp.send(params);
}

With this link: <a href="javascript:void(0);" onclick="execute(games_do.php,<?=$game['appid']?>,<?=$complete?>)" > </a>

And games_do.php contains:

$appid = $_GET['id'];
$complete = $_GET['complete'];

    mysql_query("UPDATE ownedgames SET complete='".$complete."' WHERE steamid='".$steamid."' AND appid='".$appid."'") or die(mysql_error());

However, nothing seems to happen on click. Any suggestions would be greatly appreciated! :)

2
  • haven't tested your code, but by the looks of it shouldn't you be adding the values of the execute function (in the <a> tag) within quotes? check for the error in console. Commented Aug 7, 2013 at 13:49
  • Bingo! Additionally I noticed the request was a POST request, so having GET in the PHP file wasn't going to help me very much! It's all sorted! Commented Aug 7, 2013 at 13:52

3 Answers 3

1

The parameter values for the execute function in the <a> tag should be enclosed within quotes as the function expects a string as the value.

In addition, the point mentioned in D. Schalla's answer should also be considered.

Sign up to request clarification or add additional context in comments.

Comments

0

There are several prolems with your code:

First of all, you should always escape or type-cast your code, because you have SQL Injections made possible in your code:

$appid = $_GET['id'];
$complete = $_GET['complete'];

to:

$appid = intval($_GET['id']);
$complete = mysql_real_escape_string($_GET['complete']);

Also I would change the mySQL Driver from mysql_ to PDO later, since it might be that it will be unsupported in a later version of PHP.

But however, to find the problem in your code I would debug the request using Firebug (an Firefox Addon) or the Chrome Developer Console. Check what the Request Returns, it might be an mySQL Error relating to your Database Design.

To do so, check in Chrome under the Tab Network in the Console the "Answer" of the AJAX Request, when there is an error, it will be displayed there.

I would also switch to jQuery if you plan to work more heavy with AJAX, since it handles the fallback solutions for some browsers and offers an more easy integration, you can find a doc relation this there: http://api.jquery.com/jQuery.ajax/

3 Comments

Didn't even think to check the console - the data in the function should have been quoted and additionally it was a POST request not a GET so changing that has sorted it!
Glad to hear this! The console always comes handy when it's about AJAX Problems.
It solved the problem completly or the advice of Harry was the problem?
0

Change mysql_query("UPDATE ownedgames SET complete='".$complete."' WHERE steamid='".$steamid."' AND appid='".$appid."'") or die(mysql_error());

to this:

mysql_query("UPDATE ownedgames SET complete='$complete' WHERE steamid='$steamid' AND appid='$appid'") or die(mysql_error());

Further, you can make your ajax call easier with jQuery, if you're not using it, I strongly suggest you do. It would make it as this:

function execute(filename,var1,var2){
$.ajax({
type: "POST",
url: filename,
data: {id:var1, complete: var2}
  }).done(function( result ) {
   //do whatever you want to
  });
}

as for your link, you should try this:

<?php

$id=$game['appid'];
echo('<a onclick=execute("games_do.php","'.$id.'","'.$complete.'")>Click Here </a>');

?>

3 Comments

Sorry, I should have specified that I've tested the page by going directly to the URL and it works - there's no problem with the MySQL query. :)
You are not sending anything back for the XHR to display and your XHR isn't going to display anything because you haven't told it to.
I have limited JS/AJAX knowledge, could you explain that in more detail, Jeff?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.