变量未定义,即使它是?

时间:2016-09-21 18:19:35

标签: javascript php ajax http

嗨我正在尝试按下按钮时调用php函数,但我一直在标题中收到错误。

我正在调用这样的函数:

echo("<th><input type='button' name = 'Attack_Btn' onclick = 'FightPlayer(".$row['username'].")' value ='Attack'></th>");

只要说出它从$ row ['用户...得到的用户名是James,错误就会显示

index.php:1未捕获的ReferenceError:未定义casualjames

这是它下次调用的代码

    function FightPlayer(enemyName){
    var xhttpe;
    if (window.XMLHttpRequest) {
        xhttpe = new XMLHttpRequest();
        } else {
        xhttpe = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttpe.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            BattlePlayers();
        }
    };
    xhttpe.open("GET", "FightPlayer.php?enemyname="+enemyName, true);
    xhttpe.send();
}

然后它调用我的php脚本传入变量enemyname以便使用

    <?php
    session_start();
    include 'Training.php';
    $link = mysqli_connect("","","","");

    if (isset($_SESSION['username'])) {
        $enemyname = $_REQUEST["enemyname"];
        echo $enemyname;
        $energyRemove = 1;
        $ExperienceGain = 1;
        $sql = "SELECT * FROM userstats WHERE username = '$enemyname'";
        $result = mysqli_query($link,$sql);
        $row = mysqli_fetch_assoc($result);
        $Defence = $row["Defence"];
        $winChance = CalculateWinChance($link,$Defence);
        $sql = "SELECT Energy FROM userstats WHERE username = '".$_SESSION['username']."'";
        $result = mysqli_query($link,$sql);
        $row = mysqli_fetch_assoc($result);
        $rand = rand ( 1 , 100 );
        if($row["Energy"] < 1 ){
                echo "<script type='text/javascript'>alert('Not enough energy to fight. please restore in character page');</script>";
        }else{
            if($rand < $winChance){
                $_SESSION['Battlemessage'] = "you won against ".$enemyname;
                $sql = "UPDATE userstats SET `Energy` = `Energy` - '$energyRemove' WHERE username = '".$_SESSION['username']."'";
                mysqli_query($link,$sql);
                $sql = "UPDATE userstats SET `Experience` = `Experience` + '$ExperienceGain' WHERE username = '".$_SESSION['username']."'";
                mysqli_query($link,$sql);
                $sql = "UPDATE userstats SET `Satoshi` = `Satoshi` + 2 WHERE username = '".$_SESSION['username']."'";
                mysqli_query($link,$sql);
            }else{
                $_SESSION['Battlemessage'] = "you lost against ".$enemyname;
                $sql = "UPDATE userstats SET `Energy` = `Energy` - '$energyRemove' WHERE username = '".$_SESSION['username']."'";
                mysqli_query($link,$sql);
                $sql = "UPDATE userstats SET `Satoshi` = `Satoshi` + 1 WHERE username = '".$enemyname."'";
                mysqli_query($link,$sql);
            }
            echo "";
        }
        calculateLevel($link);
    }
?>

我不确定错误实际发生在哪里我已将脚本放在在线代码检查器中,这一切都恢复正常。我在哪里错了?

2 个答案:

答案 0 :(得分:4)

您传递给javascript函数的字符串需要引用,否则它认为它是变量:

echo("<th><input type='button' name = 'Attack_Btn' onclick = 'FightPlayer(\"".$row['username']."\")' value ='Attack'></th>");

答案 1 :(得分:3)

您的错误最有可能是onclick ...您需要在函数参数中转义引号:

echo("<th><input type='button' name = 'Attack_Btn' onclick = 'FightPlayer(\"".$row['username']."\")' value ='Attack'></th>");