GregHBushnell
GregHBushnell

Reputation: 143

php repeating submit buttons different values

Hi using the code below i generate a table with a submit button for each row

    function GetPlayer($link){
    if (isset($_SESSION['username'])) {
    $x = 0; 
        $sql = "SELECT * FROM userstats ORDER BY RAND() LIMIT 5; ";
        $result = mysqli_query($link,$sql);

        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 ("<th><input type ='submit' name = 'Attack_Btn' value ='Attack'></th>");
                echo ("</tr>");
            }
        }
    }
}

now i need this button to call 2 functions and passing in certain values to these functions

an example of a button that is not generated this way but does use the same functions is below

if(isset($_POST['Dummy_Btn'])){
    $winChance = CalculateWinChance($link,5);
    Train($link,1,1,$winChance);
}

is calls two functions passing in 5 to return a different value that then gets passed into another function

how am i able to get the data such as $row['Attack'] and it be different from each of the other generated submit buttons.

ive been reading about ajax could this be used in this situation?

Upvotes: 0

Views: 210

Answers (3)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

The solution is,

  • First encapsulate your submit input element inside a <form> and append $row['Attack'] value to the action attribute of the form element, like this:

    function GetPlayer($link){
        if (isset($_SESSION['username'])) {
        $x = 0; 
            $sql = "SELECT * FROM userstats ORDER BY RAND() LIMIT 5; ";
            $result = mysqli_query($link,$sql);
    
            while($row = mysqli_fetch_assoc($result)){
                if($row['username'] !== $_SESSION['username']){//add so it dosent put duplicates
                    ?>
                    <tr>
                        <th><?php echo $row['username']; ?></th>
                        <th>Level: <?php echo $row['Level']; ?></th>
                        <th>Player Stats:<?php echo $row['Attack'] . "/" . $row['Defence']; ?></th>
                        <th>Win Chance:</th>
                        <?php echo CalculateWinChance($link,$row['Defence']); ?>
                        <form method="post" action="your_page.php?attack=<?php echo $row['Attack']; ?>">
                            <th><input type ='submit' name = 'Attack_Btn' value ='Attack'></th>
                        </form>
                    </tr>
                    <?php
                }
            }
        }
    }
    

NOTE: Don't forget to change your_page.php in the action attribute of your form.

  • And then process the form in the following way,

    if(isset($_POST['Attack_Btn'])){
        $attack = $_GET['attack'];
    
        // call those two functions
        $winChance = CalculateWinChance($link, $attack);
        Train($link,1,1,$winChance);
    }
    

Upvotes: 0

user6535042
user6535042

Reputation:

You could make each row its own form and include a hidden input value on each form. You could use a button type submit and make the value different than the text (I think) Attack. Ajax would only be useful if you are trying to load/process data without wanting to reload the whole page. – Leeish 2 mins ago

This.

You need to rethink your approach. In your example, you don't even use the value of "Dummy_Btn" so I'm unsure as to what exactly you want to accomplish...

If you can provide a more thorough explanation of what data you are trying to pass via form and the handling you wish to perform on said data, I'd be happy to help you get this up and running.

Upvotes: 0

Kervon Ryan
Kervon Ryan

Reputation: 684

The best way to go about doing this is as Leeish says.

Create a hidden field in the form. Also you can reduce the amount of echos you use by assigning them to a variable and echoing that variable.

    function GetPlayer($link){
    if (isset($_SESSION['username'])) {
    $x = 0; 
        $sql = "SELECT * FROM userstats ORDER BY RAND() LIMIT 5; ";
        $result = mysqli_query($link,$sql);

        while($row = mysqli_fetch_assoc($result)){
            if($row['username'] !== $_SESSION['username']){//add so it dosent put duplicates
                $toEcho  = "<tr>";
                $toEcho .= "<th>".$row['username']." </th>";
                $toEcho .= "<th>Level: ".$row['Level']." </th>";
                $toEcho .= "<th>Player Stats:".$row['Attack']."/".$row['Defence']." </th>";
                $toEcho .= "<th>Win Chance: ";
                $toEcho .= CalculateWinChance($link,$row['Defence'];
                $toEcho .= "<input type='hidden' name='Hiiden1' value='YOUR NUMBER' />";
                $toEcho .= "<th><input type ='submit' name = 'Attack_Btn' value ='Attack'></th>";
                $toEcho .= "</tr>";

                echo $$toEcho;
            }
        }
    }
}

Then you can get the value of the hidden variable by..

if(isset($_POST['Dummy_Btn'])){

    $hidden = $_POST["hidden1"];

    $winChance = CalculateWinChance($link,5);
    Train($link,1,1,$winChance);
}

And now you can use it as you wish.

Upvotes: 2

Related Questions