POST form through while loop not working

f I am calling an undistinguishable amount of records from a database through a while loop, and with each record called a submit button is accompanied with it, how can I get each submit button to respond exclusively to its respective iterated record?

Imagine there is a list displaying X amount of user profiles with each profile having a ‘Contact’ button at the bottom of the frame of each profile. If I click the ‘Contact’ button for a given profile displayed on the list, the name associated with profile with which the button was pressed is given a session variable to be used in the next page, being the contact message.

The issue I am struggling to troubleshoot is: when the ‘Contact’ button is pressed for any given profile, the name of the last profile is stored into the session variable. What I need instead,is that when the ‘Contact’ button of a given profile is pressed, I would like the name corresponding with the profile in question to be stored into the session variable.

Here is the code I am stuck with:

list.php:
[php]

<?php session_start(); require_once( "./inc/connect.inc.php"); if(!isset($_SESSION["email_login"])) { header("location: index.php"); } else { } ?> <?php if ($searchST) { while ($row = $searchST->fetch_assoc()) { echo ''; echo '
'; echo '
'; echo '
'; echo '
'; echo "
C1
".$row["c1"]."
"; echo "
C2
".$row["c2"]."
"; echo "
C3
".$row["c3"]."
"; echo "
C4
".$row["c4"].""; echo '
'; echo '
'; echo '
'; echo " "; echo '
'; echo '
'; echo "".$row["country"].""; echo "&nbsp".$row["first_name"]; echo '
'; echo '
'; echo ''; echo ''; echo ''; echo '
'; echo '
'; echo '
'; if (isset($_POST['message'])) { $carry = "SELECT `first_name` FROM `users`"; $carryST = $con->query($carry); if($carryST) { while ($row = $carryST->fetch_assoc()) { $_SESSION['to_name'] = $row["first_name"]; $_SESSION['to_id'] = $row["id"]; } } $carryST->close(); header("location: compose.php"); exit(); } echo ''; } } $searchST->close(); } else { } [/php] Like I mentioned, when I execute this the last record is passed, not the record corresponding with its iterated submit button (so if I click on submit button #4 I want profile #4 to pass over).

The issue isn’t your loop, it is the session variable:

[php] $_SESSION[‘to_name’] = $row[“first_name”];
$_SESSION[‘to_id’] = $row[“id”][/php]
“to_name” and “to_id” can only hold a single value with this usage. Since it looks like the form/s process on the same page that the form is created, you want to attach the id to the url string or in the post value.

Don’t nest your forms. You can use a form for each person, but it can get messy. If you do something like the following you can retrieve distinct values:

form page:
[php]

<?php if (isset($_POST['form'])) { $val = $_POST['person']; echo '

Person Value

'; echo $val; } else { for ($i = 0; $i < 10; $i ++) { echo <<<HERE Person $i
HERE; } } [/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service