Passing a value through a button in PHP

时间:2019-03-19 15:11:02

标签: php button

I want to pass $record['userId'] value through delete button into an another php function. How can I do this? I want to delete the column respond to the current userId.

if($result_set){
  $table = '<table class="table">';
  $table .='<tr><th>Record No.</th><th>Race ID</th><th>User ID</th><th>Message</th><th>Date &</th><th></th><th></th></tr>';
  $count = 1;
  while($record = mysqli_fetch_assoc($result_set)){
    $table .= '<tr>';
    $table .= '<td>' . $count . '</td>';
    $table .= '<td>' . $record['userId'] . '</td>';
    $table .= '<td>' . $record['raceId'] . '</td>';
    $table .= '<td>' . $record['message'] . '</td>';
    $table .= '<td>' . $record['createdAt'] . '</td>';
    $table .= '<td>
      <div class="btn-group" role="group" aria-label="Record Actions" style="float:left" >
        <a href="inc/edit.php" class="btn btn-success">Edit</a>                                        
      </div>
    </td>';  

    $table .= '<td>
      <div class="btn-group" role="group" aria-label="Record Actions" style="float:left" >                                        
        <a href="inc/delete.php" class="btn btn-danger">Delete</a>
      </div>
    </td>';  
    $table .= '</tr>';
    $count = $count+1;
  }

  $table .='</table>';
  echo $table;
} else {
  echo "Query Failed"; 
}

1 个答案:

答案 0 :(得分:1)

I think you have to use this button;

<a href="inc/delete.php?userId="'.$record['userId'].'" class="btn btn-danger">Delete</a>

And now at inc/delete.php use $_GET['userId'] to receive the userId

Hope I helped you

== EDIT ==

So, total will be;

 if($result_set){

                        $table = '<table class="table">';
                        $table .='<tr><th>Record No.</th><th>Race ID</th><th>User ID</th><th>Message</th><th>Date &</th><th></th><th></th></tr>';
                        $count = 1;
                        while($record = mysqli_fetch_assoc($result_set)){
                            $table .= '<tr>';
                            $table .= '<td>' . $count . '</td>';
                            $table .= '<td>' . $record['userId'] . '</td>';
                            $table .= '<td>' . $record['raceId'] . '</td>';
                            $table .= '<td>' . $record['message'] . '</td>';
                            $table .= '<td>' . $record['createdAt'] . '</td>';
                            $table .= 
                            '<td>
                                <div class="btn-group" role="group" aria-label="Record Actions" style="float:left" >
                                    <a href="inc/edit.php" class="btn btn-success">Edit</a>                                        
                                </div>
                            </td>';  

                            $table .= 
                            '<td>
                                <div class="btn-group" role="group" aria-label="Record Actions" style="float:left" >                                        
                                    <a href="inc/delete.php?userId="'.$record['userId'].'" class="btn btn-danger">Delete</a>
                                </div>
                            </td>';  
                            $table .= '</tr>';
                            $count = $count+1;
                        }

                        $table .='</table>';

                        echo $table;
                    }
                    else{
                        echo "Query Failed"; 
                    }