2

Hi I'm looking to insert a varible into a submit buttons name when I echo it via a loop so that each button has a unique name

   $x=0;
            $sql = "SELECT * FROM userstats ORDER BY RAND() LIMIT 5; ";
            $result = mysqli_query($link,$sql);
            echo ("<table>");
            echo ("<tr>");
            echo ("<th>Name</th>");
            echo ("<th>Level</th>");
            echo ("<th>Stats</th>");
            echo ("<th>Win Chance</th>");
            echo ("<th>Action</th>");
            echo ("</tr>");
            while($row = mysqli_fetch_assoc($result)){
                if($row['username'] !== $_SESSION['username']){//add so it dosent put duplicates
                    echo("<tr>");
                    echo("<th>".$row['username']." </th>");
                    echo("<th>Level: ".$row['Level']." </th>");
                    echo("<th>Player Stats:".$row['Attack']."/".$row['Defence']." </th>");
                    echo("<th>Win Chance: ");
                    echo(CalculateWinChance($link,$row['Defence']));
                    echo("<input type='hidden' name='".$x."hidden1' value='".$row['Defence']."' />");
                    echo("<input type='hidden' name='".$x."hidden2' value='".$row['username']."' />");
                    echo("<th><input type='submit' name = 'Attack_Btn".$x."' onclick = 'BattlePlayers()' value ='Attack'></th>");
                    echo("</tr>");
                    $x=$x+1;
                }
            }
            echo ("</table>");

I tried the above code but it does not change the name attribute? What am I doing wrong here?

18
  • 1
    post the complete code ... where is $x assigned? Commented Sep 21, 2016 at 15:59
  • 3
    Maybe $x is empty and please use better names for your variables. Commented Sep 21, 2016 at 16:00
  • 1
    what does the CalculateWinChance() function do, or do you feel it's not relevant to the question/problem? and did you start the session? Check for errors via PHP and MySQL, run a var_dump and look at your HTML source. Commented Sep 21, 2016 at 16:02
  • 1
    why the ajax tag? Commented Sep 21, 2016 at 16:03
  • 2
    "$ is not empty its prining as expected 01234" - The leading zero is treated as an octal, that's why it's failing. Commented Sep 21, 2016 at 16:10

1 Answer 1

3

you can put that as an answer can ill accept it :) – GregHBushnell

Posting from comments:

"$ is not empty its prining as expected 01234" - The leading zero is treated as an octal, that's why it's failing. . – Fred -ii

thank you very much that solved it :) what is an octal ? – GregHBushnell

References:


Footnote:

echo is a language construct and not a "function" per se. So, you can safely omit all of the (), since that's just more code than needed really.

Reference:

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

Comments

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.