如何将表中的表单数据发布到 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29364285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:22:32  来源:igfitidea点击:

How to POST form data from within a table to PHP

phphtmlformshtml-tablehttp-post

提问by LuckoftheLefty

I currently have a Timesheet form I am creating for the employees at my work and I am stuck at getting the input values to post correctly. I have a database set up but right now, I can't even get the values to show in a .php page. Here is a sample from my current table:

我目前有一个我正在为我工​​作中的员工创建的时间表表单,并且我坚持正确发布输入值。我设置了一个数据库,但现在,我什至无法在 .php 页面中显示值。这是我当前表中的一个示例:

<html>    
    <form method="POST" form action="result.php">
    <table>
        <tr><td><b>Day of Week</td><td><b>Week 1 Hours</td><td><b>Week 2 Hours</td>  <td>
        <tr><td>Monday</td><td>
        <input type="text" name="Monday" size="3" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')">
        <input type="checkbox" tabindex="-1" name="Stime1">Sick?<input type="checkbox" tabindex="-1" name="Vac1">Vacation?</td>
        <td><input type="text" name="Monday2" size="3" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')">
        <input type="checkbox" tabindex="-1" name="Stime2">Sick?<input type="checkbox" tabindex="-1" name="Vac2">Vacation?</td></tr>
    </table>
    <input type="submit" value="submit">
</html>

This pattern continues for each day of the week. And then when I try to view the results of the post I couldn't get anything to work. I resorted to trying:

这种模式在一周中的每一天都持续存在。然后当我尝试查看帖子的结果时,我什么也做不了。我求助于尝试:

<html>
<?php var_dump ($_POST);
?>
</html>

All I get is a blank page, and if I view source it just shows the php code used. It's late here so I must be too tired and missing something but I just can't figure it out.

我得到的只是一个空白页面,如果我查看源代码,它只会显示使用的 php 代码。来晚了,所以我一定是太累了,错过了什么,但我就是想不通。

采纳答案by Fjarlaegur

I see some errors in your HTML.

我在您的 HTML 中看到了一些错误。

I formatted your source locally. I dont know if you did this on purpose (just copy a small part of the table) but there are some tags missing.

我在本地格式化了您的源。我不知道您是否故意这样做(只需复制表格的一小部分),但缺少一些标签。

I corrected your HTML, maybe you should try that. I dont know if it solves the problem. But here are some errors:

我更正了您的 HTML,也许您应该尝试一下。不知道能不能解决问题。但这里有一些错误:

  • You never close the <b>tags in the first table row.
  • There is a table data tag (<td>)you do not close (And which is one too many) after Week 2 hours.
  • You do not close the table row tage for the first row. (<tr>)
  • You do not close the form tag.
  • The word form between the method and action in your opening form tag is incorrect. This should be removed i guess.
  • 您永远不会关闭<b>第一个表格行中的标签。
  • 有一个表数据标签(<td>)在第 2 周后您没有关闭(而且太多了)。
  • 您没有关闭第一行的表格行标签。 (<tr>)
  • 您没有关闭表单标签。
  • 开始表单标签中方法和操作之间的词形式不正确。我想这应该被删除。

<html>

<html>

<form method="POST" action="result.php">
<table>
    <tr>
        <td><b>Day of Week</b></td>
        <td><b>Week 1 Hours</b></td>
        <td><b>Week 2 Hours</b></td>
    </tr>
    <tr>
        <td>Monday</td>
        <td><input type="text" name="Monday" size="3" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"> <input type="checkbox" tabindex="-1" name="Stime1">Sick?<input type="checkbox" tabindex="-1" name="Vac1">Vacation?</td>
        <td><input type="text" name="Monday2" size="3" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"> <input type="checkbox" tabindex="-1" name="Stime2">Sick?<input type="checkbox" tabindex="-1" name="Vac2">Vacation?</td>
    </tr>
</table>
<input type="submit" value="submit">
</form>
</html>

Let me know if this helps. Cheers.

如果这有帮助,请告诉我。干杯。

回答by Patrick R

I wanna start to apologize if i have not understood your trouble correctly.

如果我没有正确理解你的麻烦,我想开始道歉。

First, you are missing some closing-tags.

首先,您缺少一些结束标签。

Second, You can take a look at how i insert into my DB, and when gets the data afterwards. Hope this will help you a bit. I have it as a template for my self, so i don't need to create the files and the connection all over again when i make a new project, but just change the DB information.

其次,您可以看看我如何插入到我的数据库中,以及之后何时获取数据。希望这会对你有所帮助。我把它作为我自己的模板,所以当我创建一个新项目时,我不需要重新创建文件和连接,而只需更改数据库信息。

I'm not saying this is THE right way to do it, theres a lot of other ways, but this is how i do it.

我并不是说这是正确的方法,还有很多其他方法,但这就是我的方法。

HTML

HTML

<form action="insert.php" method="post">
   //Insert your table here
   <input name="Monday" value="" type="text" placeholder="Wauw Talk" />
</form>

Insert.php

插入.php

$conn = mysql_connect('HOST', 'USER', 'PASS');
mysql_select_db('DATABASE', $conn);
if(!$conn){
     die("Connection Error: " . mysql_error());
}

$Monday = $_POST['Monday']; //Var containing your input data

$sql = mysql_query('INSERT INTO table_in_db (Day) VALUES ("'. $Monday .'")')or die(mysql_error());

if (!$sql) {
    echo "Something went wrong...";
}

Header('Location: ...');

Get the data from DB

从数据库中获取数据

<?php
    $conn = mysql_connect('HOST', 'USER', 'PASS');
    mysql_select_db('DATABASE', $conn);
    if(!$conn){
         die("Connection Error: " . mysql_error());
    }

    $sql_select = mysql_query('SELECT * FROM table_in_db');

    if(mysql_num_rows($sql_select) > 0){
     while($row = mysql_fetch_assoc($sql_select)){
      ?><div class="Days">
         <label class="Mondayclass" ><? echo $row["Day"]; ?></label>
        </div>
<?php
   }
}
?>

Hope this helps. If this is totally not what you ware looking for, I'm sorry, i have misunderstood the problem you ware having.

希望这可以帮助。如果这完全不是您想要的,对不起,我误解了您所遇到的问题。