如何将表中的表单数据POST到PHP


How to POST form data from within a table to PHP

我目前有一个时间表表单,我正在为工作中的员工创建,我一直在努力让输入值正确发布。我已经建立了一个数据库,但现在,我甚至无法获得要在.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>

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

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

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

我在HTML中看到一些错误。

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

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

  • 您永远不会关闭表第一行中的<b>标记
  • 有一个表数据标记(<td>)在第2周小时后未关闭(而且太多)
  • 不关闭第一行的表行后台。(<tr>)
  • 您没有关闭表单标记
  • 打开表单标记中方法和操作之间的单词表单不正确。我想这个应该去掉

<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>

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

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

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

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

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

HTML

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

Insert.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: ...');

从数据库获取数据

<?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
   }
}
?>

希望这能有所帮助。如果这完全不是你想要的,对不起,我误解了你的问题。

您也可以这样做:

首先,形式

<form method="post" action="this.php">

第二,隐藏字段

<td><input type="hidden" name="<?php echo $variable ?>" value="<?php echo $variable ?>"><?php echo $variable ?></td>