0

I am creating multiple forms using a foreach statement.
I want to retrieve the $_POST value that my paragraph id is set to when the user click the button.

I don't know how to use $_POST in my displayTeacherProfile function in this case since I don't know what the exact value of my paragraph id is set to.

<?php foreach ($data['teachers'] as $teachData): ?>
<form class="form-horizontal" id="form" action="/MVC/teacher/displayTeacherProfile" method="post">
    <tr>
        <td><?php echo $teachData['username_email'] ?></td>
        <td><p class="form-control-static" name="<?php $teachData['person_id'] ?>"
               id="<?php $teachData['person_id'] ?>"><?php echo $teachData['language_name'] ?></p>
        </td>
        <td>
            <button id="continueButton" name="action" type="submit" class="btn btn-default"
                    value="Search">Visit Profile
            </button>
        </td>
    </tr>
</form>
Viteazul
  • 181
  • 4
  • 12

1 Answers1

0

You are in a very wrong direction my friend.

First of all You can't put a form in between table and tr

the structure of table is

table < tr < td You cant put it like table < form < tr < td

If you inspect your code then you can see what i was saying.

To maket it happen put the form in td or outside table as per requirement

In your case use it inside td.

after that you can't get the value of paragraph id in post data. You have to create a form field to get that data.

In your case that would be a hidden field.

So the code will be somthing like this

<?php foreach ($data['teachers'] as $teachData): ?>
<tr>
    <td><?php echo $teachData['username_email'] ?></td>
        <td><p class="form-control-static" name="<?php $teachData['person_id'] ?>"
               id="<?php $teachData['person_id'] ?>"><?php echo $teachData['language_name'] ?></p>
        </td>
    <td>
        <form class="form-horizontal" action="/MVC/teacher/displayTeacherProfile" method="post">
            <input type="hidden" name="teacher_id" value="<?php echo $teachData['person_id'] ?>">
             <button name="action" type="submit" class="btn btn-default"
                    value="Search">Visit Profile
            </button>
        </form>
    </td>
</tr>

It will give you data in $_POST['teacher_id']

This will work.

I will suggest you to use GET instead of POST. Check this link

When do you use POST and when do you use GET?

Check this code for $_GET

<?php foreach ($data['teachers'] as $teachData): ?>
<tr>
    <td><?php echo $teachData['username_email'] ?></td>
        <td><p class="form-control-static" name="<?php $teachData['person_id'] ?>"
               id="<?php $teachData['person_id'] ?>"><?php echo $teachData['language_name'] ?></p>
        </td>
    <td>
         <a href="/MVC/teacher/displayTeacherProfile?person_id=<?php echo $teachData['person_id'] ?>" class="btn btn-default" 
                >Visit Profile
        </a>
    </td>
</tr>
Community
  • 1
  • 1
Aman Rawat
  • 2,625
  • 1
  • 25
  • 40