Cem Aytekin
Cem Aytekin

Reputation: 109

Selecting a specific button in a specific row in php

I print some information from my database using php.I show it in a table format by using html.At each row there are 3 buttons where user can make changes about that course in that specific row.The problem is I can not define all of the buttons in that table.If it was a single submit button I would give it a name and then get the data by $_POST['name'] but with multiple rows I am confused. Here is my code:

for($i=0;$i<$myarray_lenght;$i++) 
{
    $row_id=$i;
    echo "<tr>";
    echo "<td><form action='private_page.php' method='POST'> <input type='radio' name=$row_id value='edit'>  </form> </input></td>";
    echo "<td>". $myarr[$i]['thesis_name'].  "</td>".
                "<td>" .$myarr[$i]['student_id']."</td>"
    echo "<td><form action='private_page.php' method='POST'> <input type='submit' name='more_info' value='Get more info of the Student !'>  </form></input></td>";
    echo "<td><form action='private_page.php' method='POST'> <input type='submit' name='remove' value='Remove the Student !'>  </form></input></td>";                                   
   echo "</tr>";                
}

I have tried to identify each row by an edit button and then said that whenever that that row (the radio button) is clicked, get the information from that row like that:

 if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//something posted

for($i=0;$i<100;$i++)
if (isset($_POST[$i])) {        
    if (isset($_POST['more info'])) {       
    // do something.   

  }

Upvotes: 1

Views: 1669

Answers (2)

Nana Partykar
Nana Partykar

Reputation: 10548

First of all, A <form> is not allowed to be a child element of a <table>, <tbody> or <tr> or <td>. You can have an entire <table> inside a <form>. You can have a <form> inside a <table> cell. You cannot have part of a <table> inside a <form>. Check Form Inside Table

So, I will not suggest you to use <form> inside <table>.

Alternative / Suggestion

1) First Solution

<?php
for($i=0;$i<$myarray_lenght;$i++) 
{
  $row_id=$i;
  $remove = "Remove";
  $more_info = "more_info";
  echo "<tr>";
        echo "<td></td>";
        echo "<td>". $myarr[$i]['thesis_name'].  "</td>"."<td>" .$myarr[$i]['student_id']."</td>"
        echo "<td>".
                    "<a href='private_page.php?id=$passStudentId&action=$more_info'>".
                            "<input type='button' name='more_info' value='Get more info of the Student !'></input>".
                    "</a>".
             "</td>";
        echo "<td>".
                    "<a href='private_page.php?id=$passStudentId&action=$remove'>".
                        "<input type='button' name='remove' value='Remove the Student !'></input>".
                    "</a>".
              "<td>";                                   
    echo "</tr>";                
}
?>

private_page.php

<?php

$studentID = $_GET['id'];
$action = $_GET['action'];

if($action == "Remove") {
    // Do The Needfull action
}

if($action == "more_info") {
    // Do The Needfull action
}
?>

OR

2) Second Solution

<form method='POST' action='private_page.php'>
    <table>
        <?php
        for($i=0;$i<$myarray_lenght;$i++) 
        {
            $row_id=$i;
            echo "<tr>";
                echo "<td><input type='radio' name='student' value='."$studentID".'></td>";
                echo "<td>". $myarr[$i]['thesis_name'].  "</td>"."<td>" .$myarr[$i]['student_id']."</td>"                                   
            echo "</tr>";                
        }
        ?>
    </table>
    <input type='submit' name='submit' value='more_info'></input>
    <input type='submit' name='submit' value='Remove'></input>
</form>

private_page.php

<?php

$studentID = $_POST['student'];
$action = $_POST['submit'];

if($action == "Remove") {
    // Do The Needfull action
}

if($action == "more_info") {
    // Do The Needfull action
}
?>

Upvotes: 1

WillardSolutions
WillardSolutions

Reputation: 2314

For each of your forms, add a hidden input with the value set with your current id -

echo "<td><form action='private_page.php' method='POST'>";
echo "<input type='submit' name='more_info' value='Get more info of the Student !'>";
echo "<input type='hidden' name='thisID' value='". $myarr[$i]['student_id'] ."'/>";
echo "</form></input></td>";

Then in your private_page.php,

$id = $_POST['thisID'];
$action = (isset($_POST['remove'])) ? "remove" : "more info";

// do your $action on your $id....

Another option for you is to use Javascript to build your post request with the appropriate id and action.

Upvotes: 1

Related Questions