1

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.   

  }
dinotom
  • 4,990
  • 16
  • 71
  • 139
Cem Aytekin
  • 109
  • 1
  • 1
  • 11
  • Since you're building the table dynamically, you can add a hidden field inside each `
    ` and set the value of that field to the id of your current record. Then that will be passed to your private_page.php
    – WillardSolutions May 10 '16 at 16:32
  • Incidentally, you should get rid of those `` tags. – Don't Panic May 10 '16 at 16:36
  • In `private_page.php`, do you need to work on multiple selected records at once, or just one? – Don't Panic May 10 '16 at 16:44
  • I need to work one record.For example teacher logs in,sees his courses and the students associated with them.At each row there is a button to delete that student or that course.So in order to make an edit for the teacher, he first has to select the row and then hit the apropriate button in that row. – Cem Aytekin May 10 '16 at 17:04

2 Answers2

1

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.

WillardSolutions
  • 2,316
  • 4
  • 28
  • 38
1

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
}
?>
Community
  • 1
  • 1
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
  • Thanks, everything seems to be great but I get an " Undefined index: passStudentId " error. I created the variable $passStudentId=$myarr[$i]['student_id']; in for loop because it hadnt been declared but still same error. – Cem Aytekin May 10 '16 at 18:44
  • In which page error comes ? Means in **private_page.php** ? @CemAytekin – Nana Partykar May 10 '16 at 18:50
  • Yes in fact they are both in private_page.php maybe that creates a confusion ? – Cem Aytekin May 10 '16 at 18:52
  • No. No. @CemAytekin. Some other problem. **One question :** Is `$passStudentId=$myarr[$i]['student_id'];` value coming correctly in First Page. – Nana Partykar May 10 '16 at 18:58
  • If you pass `student id` correctly. Then, all will be well. Just there problem is. Which I can't help due to lack of understanding the logic behind this. @CemAytekin. Going Home. That's It For Today. *Cheers From India.* – Nana Partykar May 10 '16 at 19:12
  • When i click on the link that i created on the table, the url changes and shows the id of the student correctly but somehow the $passStudentId variable obtained by method GET seems to fail. – Cem Aytekin May 10 '16 at 19:13
  • My Bad. Please replace `$studentID = $_GET['passStudentId'];` to `$studentID = $_GET['id'];` @CemAytekin – Nana Partykar May 10 '16 at 19:15
  • Please have a look on my updated answer @CemAytekin and the 2nd last comment. *Sorry.* – Nana Partykar May 10 '16 at 19:17
  • 1
    yes problem solved ! thank you very much for your interest. – Cem Aytekin May 10 '16 at 20:07
  • @CemAytekin: Then, Please Mark This answer as correct answer. As, it will help other user to find this answer easily. And, if you don't know how to mark correct answer. Please click http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work. *Glad To Help*. -- *Keep Coding* – Nana Partykar May 10 '16 at 20:40