GregHBushnell GregHBushnell - 29 days ago 6x
PHP Question

inserting variables into php echo of a submit button

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

$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("<th>".$row['username']." </th>");
echo("<th>Level: ".$row['Level']." </th>");
echo("<th>Player Stats:".$row['Attack']."/".$row['Defence']." </th>");
echo("<th>Win Chance: ");
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 ("</table>");

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


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



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.