Hi all...

I'm trying to create a simple HTML table generated by php from mySQL table. This table simply displays the First Name and Last Name of all Employees in the database table.

The trick is, I would also like to generate an HTML FORM, so beside each first and last name, there is a button that submits the first and last name to another php processing page.

Can this be done?

<?php

//Establish Connection String
$con = mysql_connect("DOMAIN","USERNAME","PASSWORD");

//Connect to database
mysql_select_db("DATABASE", $con);

//Query database and set result to variable
$result = mysql_query("SELECT * FROM cls_app_form");

//Close Connection
mysql_close($con);

//Establish Table Header
echo "<thead><tr> <th>First Name</th> <th>Last Name</th> <th>Go to Employee Application Form</th></tr></thead>";


//Set Form Name
$FormName = 0;
//Loop through table data
while($row = mysql_fetch_array($result))
{

//Set variables based on row
$FirstName = $row ['FirstName'];
$LastName = $row ['LastName'];
$FormName = $FormName++;


//Create Table
echo TABLE HEADER STUFF;
echo "<tr>";
echo "<td>"$row['FirstName'] . "</td><br />";
echo "<td>"$row['LastName'] . "</td><br />";
echo "<td>"SUBMIT BUTTON with $FirstName and $LastName submitted to another process.php page"</td>";
echo "</tr>;
}
?>

The Logic:

echo TABLE HEADER STUFF;
echo " <form id="$FormName" method="post" action="fetch.php"><tr>";
echo "<td>"$row . "</td><br />";
echo "<td>"$row . "</td><br />";
echo "<td>"<input type=submit value='Fetch Employee Application'>"</td>";
echo "</tr></form>;

Recommended Answers

All 3 Replies

Well, first, close your connect to the database at the end of the script

mysql_close($con);

Second, use \ when echoing html and you have additional double quotes.
example:

echo " <form id=\"form1\" method=\"post\" action=\"fetch.php\"><tr>";

Otherwise it's not gonna work.
Finally, i think you got this one, when you are clicking submit, just declare your variables at the beginning of your php script so u can use POST to get them.

Thank you very much. You pointed me in the right direction. Here's the final code if anyone is interested:

From an HTML page, you click a button called Fetch Employees. This directs you to a php page that generates a table with the first and last name of all employees in the sql database table. It also incorporates a button (html form field) to send that first and last name to another php processing page which populates an application form with the data from the database (Select * From DB WHERE First Name = and Last Name =).

<?php

//Establish Server Connection String
$link = mysql_connect("Server","username","password");


//Connect to database
mysql_select_db("cls_1", $link);

//Query database and set result to variable
$result = mysql_query("SELECT * FROM cls_app_form");


//Generate Table
echo"<table><tr><th>First Name</th><th>Last Name</th><th>Get Application</th></tr>";
while($row = mysql_fetch_array($result))
{

$FirstName = $row ['FirstName'];
$LastName = $row ['LastName'];


echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td><br />";
echo "<td>" . $row['LastName'] . "</td><br />";
echo "<td><form id= \"$FormName\" method=\"post\" action=\"process3.php\">
<input name=\"FirstName\" type=\"hidden\" value=\"$FirstName\">
<input name=\"LastName\" type=\"hidden\" value=\"$LastName\">
<input name=\"submit\" type=\"submit\" value=\"Fetch Employee Application\">
</form></td></tr>";
}
echo "</table>";


?>

Now for some header and footer info and some css to make it look purdy.
Thanks alot

No problem, I am glad I helped. :)

P.S. Don't forget to mark it as "solved" if you don't have any more questions ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.