简体   繁体   中英

Using PHP to post HTML form data as a table

Hello all im trying to get four fields of an HTML form to post to an HTML table by using PHP. Here is what I have so far.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<head>
    <style>
    table, th, td {
        text-shadow: 0.2px 0.2px 0.2px black;
        border: 1px solid black;
        letter-spacing: 1px
    }
</style>
<body>
    <h1> User Info </h1>
    <table>
        <tr>
            <th style="background-color:#FFFF00">  First Name</th>
            <th style="background-color:#FFFF00"> Last Name</th>
            <th style="background-color:#FFFF00"> user id</th>
            <th style="background-color:#FFFF00"> vaules</th>

        </tr>
    </table>
<body>
<a href="userInfo.html"><-- Back</a>
<br>
<br>
<br>


<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
{

$first_name= $_POST['first_name'];
$last_name = $_POST['last_name'];
$user_id=$_POST['user_id'];
$values=$_POST['values'];

 "<th>";
 "<td>".$first_name."</td>";
 "<td>".$last_name."</td>";
 "<td>".$user_id."</td>";
 "<td>".$values."</td>";
 "</th>";

this code is only showing the table headers and not actually posting my data to each cell. I am not 100% sure what has gone wrong and I appreciate any help in advance!

Assuming you are actually sending the POST data via a script, try this:

<?php
if($_SERVER["REQUEST_METHOD"]=="POST") {
    $first_name= $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $user_id=$_POST['user_id'];
    $values=$_POST['values'];
}
?>
 <table>
       <tr>
          <th style="background-color:#FFFF00">  First Name</th>
          <th style="background-color:#FFFF00"> Last Name</th>
          <th style="background-color:#FFFF00"> user id</th>
          <th style="background-color:#FFFF00"> vaules</th>
       </tr>
        <tr>
            <td><?php if(isset($first_name)) echo $first_name; ?></td>
            <td><?php if(isset($last_name)) echo $last_name; ?></td>
            <td><?php if(isset($user_id)) echo $user_id; ?></td>
            <td><?php if(isset($values)) echo $values; ?></td>
        </tr>
    </table>

You need to echo the results. However, with this approach, you will only get 1 set of data. If you have more than one row, you will need to loop through them.

A couple of things, firstly you are already defining a <style> section in your html, so why not use it consistently and remove the inline styles from your headings:

<style>
    table, th, td {
        text-shadow: 0.2px 0.2px 0.2px black;
        border: 1px solid black;
        letter-spacing: 1px
    },
    th .table_header {
        background-color:#FFFF00;
    }
</style>

....

<th class="table_header"> First Name</th>

for the table values you can make use of the Null coalescing operator (which is ??). This operator checks if a value exists/is null/isset all in one so you avoid errors. Using this will allow you to set a default if the value does not exist - therefore you will always have some output and will be able to be sure if the table generated or not, eg:

<?php

    $first_name = $_POST['first_name'] ?? 'No name submitted';//or something more useful to you
    $last_name = $_POST['last_name'] ?? 'No surname submitted';
    $user_id=$_POST['user_id'] ?? 'No id found';
    $values=$_POST['values'] ?? 'No values found';

Make sure you do this above the area you want your table to be. Really, you would be best to do this at the top of the file in my opinion - by the time you are outputting HTML you should have already done as much of your PHP work as possible.

Then you can reduce the inline php in your html, which will make it more readable and easier to manage as your variables are always set:

....

<th style="background-color:#FFFF00"> vaules</th>

        </tr>
<?php    
$table = '<table>
           <tr>
              <th class="table_header">First Name</th>
              ...
           </tr>
            <tr>
                <td>' . $first_name . '</td>
                <td>' . $last_name . '</td>
               ...
            </tr>
        </table>';
    
    echo $table;
?>

</table>

I'm saving this in a $table variable here in case you want to use this idea in the future (eg in a loop for multiple rows) but you can just echo the whole string out if you don't want/need to save it in a variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM