Anon
Anon

Reputation: 3

How to find out if a button with an unknown ID is pressed php

Would anyone be able to help me with the following code...

$query = "SELECT * FROM table ORDER BY dateTime";
$sqldata=mysqli_query($conn,$query) or die( 'Error Retrieving data');
echo"<form>";
$var=0;
        while($row=mysqli_fetch_array($sqldata, MYSQLI_ASSOC))
        {
            var++; 
            echo"<fieldset>
            <legend>"; echo $row['Name']; echo"</legend>";
                echo"<table style='width:100%'>                     
                    <tr>
                        <td>Location</td>
                        <td>"; 
                        echo $row['EventLocation']; echo"</td> 
                    </tr>
                    <tr>
                        <td>Subject</td>
                        <td>"; 
                        echo $row['eventSubject']; echo"</td>  

                    </tr>
                    <tr>
                        <td>Description</td>
                        <td>"; 
                        echo $row['EventDescription']; echo"</td> 

                    </tr>
                    <tr>
                        <td>Time</td>
                        <td>"; 
                        echo $row['dateTime']; echo"</td> 
                    </tr>                                                           
            </table> <br>
            <input type='submit' class='btn btn-info' value='Sign Me Up!' name='btnSignUp'></input>           
        </fieldset> <br>";
        }

echo"</form>";

When I loop through my while loop, a button will be created for each record in my table, then when the button is clicked, a user will sign up for an event. However, I am not sure how to check which button has been clicked? I normally do it like this...

    if(isset($_POST['btnSignUp']))
{
    $insert="INSERT INTO tblEventGuests (fkEvent, fkUser) VALUES ('". $row['pkEvent'] . "', '".$_SESSION['user']."'";

    if ($conn->query($insert) === TRUE) 
    {
        echo "You have successfully signed up to this event";
    } 
    else 
    {
        echo "Error: " . $insert . "<br>" . $conn->error;
    }

    $conn->close();
}

But because I am not sure of the name of the button or how many events there are (while coding) I have no clue how to go about doing what I need to do. Does anyone have any suggestions? Or even an alternative method? Thanks!

Upvotes: 0

Views: 83

Answers (2)

David
David

Reputation: 218798

Create the form inside the loop instead of outside it. That way each form would individually contain exactly the information the server needs, instead of sending everything to the server and trying to figure out which subset of information was actually being selected.

while($row=mysqli_fetch_array($sqldata, MYSQLI_ASSOC))
{
    //...
    echo "<form method='POST'>
          <table style='width:100%'>                     
            <tr>
              <td>Location</td>
              <td>"; 
    echo $row['EventLocation'];
    echo "    </td> 
            </tr>
            <!--- etc... --->
          </table><br>
        </form>
        <input type='submit' class='btn btn-info' value='Sign Me Up!' name='btnSignUp'></input>           
    </fieldset> <br>";
}

Edit: As pointed out in a comment on the question, you also would need to define the method attribute on the form if you expect the values to be in the $_POST collection. Note the updated code above.

Upvotes: 1

Barmar
Barmar

Reputation: 780673

Instead of <input type="submit">, use <button type="submit">. This will allow you to give the button a different label than the value, and the value will indicate which submit button they clicked on.

<button type='submit' name='btnSignup' value='{$row['eventId']}'>Sign me up!</button>

Then in the PHP that processes the form you can use the value of $_POST['btnSignup'] to know which event they signed up for.

Upvotes: 1

Related Questions