Biscuit128
Biscuit128

Reputation: 5408

Linking html button click

$result = mysql_query('SELECT * FROM teams');

while($row = mysql_fetch_array($result))
{
  $team= $row['team'];
  $goals= $row['goals'];
  $squadsize= $row['squadsize'];
  $league= getTheirLeague($team);
  echo $team. "</br>";
  echo "Goals Scored " . $goals. "</br>";
  echo "League " . $league. "</br>" . "</br>";

  <form method="POST" action="football.php">
    <button type="button">Edit</button>
    <button type="button">Remove</button>
  </form>
}

I am trying to add a remove and edit feature to my teams, for every team i print out i have a form being printed with 2 buttons. What i am unsure of is how i can tie the button clicks up to the definitive team that the button belongs too.

Upvotes: 0

Views: 118

Answers (1)

Phil
Phil

Reputation: 164980

As each row (team) gets its own form, simply add a hidden field with the team identifier (assuming $row['team'] for this example).

Please note that IE has terrible <button> support in forms. I'd advise using submit inputs...

<form method="POST" action="football.php">
    <input type="hidden" name="team"
        value="<?php echo htmlspecialchars($row['team']) ?>">
    <input type="submit" name="edit" value="Edit">
    <input type="submit" name="remove" value="Remove">
</form>

You can then tell which team form was submitted by checking $_POST['team'] and which button was pressed using...

if (isset($_POST['edit']) { 
    // edit clicked
}

if (isset($_POST['remove']) {
    // remove clicked
}

Upvotes: 1

Related Questions