0

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?

1
  • 1
    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) <button type="submit" value="100">Attack</button>. Ajax would only be useful if you are trying to load/process data without wanting to reload the whole page. Commented Sep 16, 2016 at 18:08

3 Answers 3

2

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.

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

6 Comments

the dummy button is already in use. im trying to get the attack_btn to use the values that are $row['Defence'] but becuase it generates 5 buttons all with the same name this makes it difficult aswell as the values being differant every time
ive tried it out but becuase hidden 1 dosent exist until its generated i get the Undefined index: hidden1 error
?? so you want to generate the form after the button is posted?
it works but it will have an error until i load that page which is not ideal
oh no ok after reloding th page it does not appear to happen probably a one off thing. thankyou
|
0

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.

1 Comment

the dummy button was just an example of the variables i need to pass. i need to use the same functions but instead of passing 5 i pass $row['defence']
0

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

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.