GregHBushnell
GregHBushnell

Reputation: 143

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?

Upvotes: 0

Views: 339

Answers (2)

Your error is most likely with the onclick...you need to escape quotes in the function argument here:

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

Upvotes: 3

aynber
aynber

Reputation: 23010

The string you're passing into your javascript function needs to be quoted, or else it thinks that it's a variable:

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

Upvotes: 4

Related Questions