简体   繁体   中英

Variable is not defined even though it is?

Hi I'm trying to call a php function when a button is pressed but I keep getting the error in the title.

I'm calling the function like so:

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

just say the username that it gets from $row['user... is James the error will display

index.php:1 Uncaught ReferenceError: casualjames is not defined

This is the code that it calls next

    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();
}

and then it calls my php script passing in the variable enemyname for it to use

    <?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);
    }
?>

I'm not sure where the error is actually happening I've put my scripts through online code checkers and it all returns normal. Where am I going wrong here?

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

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

您的错误最有可能与onclick ...有关,您需要在此处的函数参数中转义引号:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM