简体   繁体   English

使用 PHP 将 HTML 表单数据作为表格发布

[英]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.大家好,我正在尝试使用 PHP 获取 HTML 表单的四个字段以发布到 HTML 表。 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!我不是 100% 确定出了什么问题,我提前感谢任何帮助!

Assuming you are actually sending the POST data via a script, try this:假设您实际上是通过脚本发送 POST 数据,请尝试以下操作:

<?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.但是,使用这种方法,您将只能获得 1 组数据。 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:有几件事,首先你已经在你的 html 中定义了一个<style>部分,那么为什么不一致地使用它并从你的标题中删除内联样式:

<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 ??).对于表值,您可以使用Null coalescing operator (即 ??)。 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.确实,我认为最好在文件顶部执行此操作 - 在输出 HTML 时,您应该已经完成​​了尽可能多的 PHP 工作。

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:然后,您可以减少 html 中的内联 php,这将使其更具可读性且更易于管理,因为您的变量总是被设置:

.... ....

<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我将它保存在$table变量中,以防将来您想使用这个想法(例如在多行循环中),但如果您不想/不需要保存,您可以只回显整个字符串它在一个变量中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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