dongwu3596 2013-08-07 13:36
浏览 32
已采纳

AJAX PHP提交无效

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! :)

  • 写回答

3条回答 默认 最新

  • douju2014 2013-08-07 13:55
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装