T.Pilvelis T.Pilvelis - 2 years ago 51
HTML Question

Passing variables via HTML into a PHP function

How would one pass a parameter from a PHP result into a HTML button then into another PHP function.

What I personally have is a table of records and next to the record is a link to another HTML page. The page will contain more detailed information of the record selected at first. E.g. Client then once clicked, that clients information.

This Function gets all the companies in a row along with a button (to beside it in another column

function display_companies(){
$conn = connect();
$query = "SELECT * FROM company";
$results = mysqli_query($conn, $query);
echo "<table >
while ($row = mysqli_fetch_array($results)) {
echo "<tr>
<td><a href='../Views/displayconnection.php'>Display Connection</a></td>

Then I want the parament of the name to be passed down to this block of code

function display_company_connection($name){

$conn = connect();
$query = "SELECT * FROM companyconnection WHERE CompanyName='$name'";
$results = mysqli_query($conn, $query);
echo "<table >
while ($row = mysqli_fetch_array($results)) {
echo "<tr>

both functions are in 1 file. the
is what contains the company name and buttons and once a button is pressed it goes to
which will
include display_company_connection($name)
and display the table of information

Answer Source

First of all, change the file extension from .html to .php i.e change company.html to company.php.

Second, on company.php page, you can append $row['Name'] value to the URL like this:

echo "<tr>
    <td>" . $row['Name'] . "</td>
    <td><a href='../Views/displayconnection.php?name=" . urlencode($row['Name']) . "'>Display Connection</a></td>

And then on displayconnection.php page, get the URL parameter value using $_GET superglobal and simply pass this value to display_company_connection() function, like this:

// Get the URL parameter value
$name = $_GET['name'];

// Pass $name to display_company_connection() function
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download