0

I have built a table in PHP to display a list of people and data relating to them. I want the user to be able to select one of those users to send an e-mail request to. So I have put a button at the end of each row for every user.

Im not sure how to write the PHP script to identify which button has been pressed. I have tried assigning the name of the button to be the e-mail address of the user for the row but I dont know how to read that value in the corresponding script...

Code for initial page:

if ($query_run = mysql_query($query))
{

echo "<fieldset>";

echo "<table>";
while ($row = mysql_fetch_assoc($sqldata))
{

    echo "<tr><th>Name</th>";
    echo "<td>";
    echo $row['FirstName']." ".$row['LastName'];
    echo "<td>";
    echo $row['Country'];
    echo "<td>";
    echo $row['Profile'];
    echo "<td>";
    echo $row['Email'];
    $Email = $row['Email'];
    echo "<td>";
    echo '<input type = "submit" name="$Email" value="connect">';

}
echo "</table>";
echo "</fieldset>";

Thanks!

Alex Szabo
  • 3,274
  • 2
  • 18
  • 30
Nickitfc
  • 13
  • 1

3 Answers3

0

If you are planning to do this in HTML5, you could alter the submit button, so that it would redirect to a page, where you can get the e-mail by $_GET[];

echo "<input type=\"submit\" formaction=\"request.php?email=".$Email."\" value="Submit">\";

Then in your request.php you could use:

$email = $_GET['email'];
Alex Szabo
  • 3,274
  • 2
  • 18
  • 30
0

I think, you have to change name attribute from "$Email" to, I don't know, maybe "user_email" and assign $email variable to value attribute. So in script which must handle this form you will just need to retrieve a value of $_POST['user_email'].

Also I think it's better to use $Email in double quotes:

echo "<input name='$Email'" ...>;

because you will get string '$Email' instead of expected email address.

Or you can create a form for each table row.

Alex
  • 1,073
  • 9
  • 20
0

There are any number of ways you could approach this, but I would personally choose to use an HTML form for this purpose. Consider the following HTML:

<form name="myForm" method="GET" action="">
    <button type="submit" name="email" value="test1@email.web">test 1</input>
    <button type="submit" name="email" value="test2@email.web">test 2</input>
    <button type="submit" name="email" value="test3@email.web">test 3</input>
</form>

If you were to try to click on one of these buttons, you'd notice your query string will now show ?email=test2%40email.web (assuming you clicked the second button).

The gist of this approach is to use the same name attribute for your submit buttons, but assign a different value to each. I chose button over input due to the ability to present button text different from the value attribute (as described in this question).

Applying this principle in your PHP, you'd get something like:

while ($row = mysql_fetch_assoc($sqldata))
{

    echo "<tr><th>Name</th>";
    echo "<td>";
    echo $row['FirstName']." ".$row['LastName'];
    echo "<td>";
    echo $row['Country'];
    echo "<td>";
    echo $row['Profile'];
    echo "<td>";
    echo $row['Email'];
    $Email = $row['Email'];
    echo "<td>";
    echo '<button type="submit" name="email_address" value="' . $Email . '">Send email</button>';

}

Then, you just check your email_address parameter for the email address.

Community
  • 1
  • 1
Justin Bell
  • 396
  • 1
  • 10
  • Wouldnt the value print on the button by doing that? – Nickitfc Oct 04 '14 at 16:02
  • No, that's the beautiful part -- the `button` allows you to treat it as though it were a `input` of type `submit`, while still allowing you to have a `value` attribute that differs from the actual button of the text. – Justin Bell Oct 05 '14 at 01:57