在按钮单击上添加额外列到表单表

时间:2013-09-02 20:10:55

标签: php html mysql forms

这是相当长的啰嗦,我可能已经完全错了一切,所以随意对我大喊:P 我的PHP充其量是生锈的,所以请和我一起玩,如果我需要完全不同,那就说吧,否则我们就去了。

我目前有一个HTML表单,其中的一部分位于HTML表单中,该表单由以下PHP代码生成:

<?php $rows = array(
        array('weight' => 1000, 'cbm_min' => 0.1, 'cbm_max' => 2.3 ),
        array('weight' => 1500, 'cbm_min' => 2.31, 'cbm_max' => 3.5 ),
        array('weight' => 2000, 'cbm_min' => 3.51, 'cbm_max' => 4.6 ),
        array('weight' => 2500, 'cbm_min' => 4.61, 'cbm_max' => 5.75 ),
        array('weight' => 3000, 'cbm_min' => 5.75, 'cbm_max' => 6.9 )
); 

?>  
<form name="createtable" action="?page=createtable" method="POST" autocomplete="off">
    <label for=tablename>Name of Table</label>
    <input type=text name=tablename id=tablename>
      <table border='1'>
            <tr>
                <th> Weight </th>
                <th> CBM Min </th>
                <th> CBM Max </th>
                <th> Min </th>
            </tr>
<?php
$i = 1; // I'll use this to increment the input text name
foreach ($rows as $row) {
  // Everything happening inside this foreach will loop for as many records/rows that are in the $rows array.
 ?>
    <tr>
      <th> <?= (float) $row['weight'] ?> </th>
      <th> <?= (float) $row['cbm_min'] ?> </th>
      <th> <?= (float) $row['cbm_max'] ?> </th>
      <th> <input type=text name="min<?= (float) $i ?>"> </th>
    </tr>
  <?php
  $i++;
}
?>
  </table>
  <input type=submit value="Create Table">
</form>

当用户填写表单并提交表单时,内容随后用于创建mysql表。 这个想法是表单会复制创建的mysql表的样子。 这很好用,但是现在我需要能够在表格表中添加额外的“列”。

即当用户按下某个按钮时,另一列将出现在表格表中,这样用户就可以填写这些值,然后将该列添加到传递到创建mysql表的php函数的数据中。

处理提交数据的Php

<?php
if($page == "createtable"){
$tablename = $_POST['tablename'];
$mins = array($_POST['min1'],$_POST['min1'],$_POST['min2'],$_POST['min3'],$_POST['min4'],$_POST['min5'],$_POST['min6'],$_POST['min7'],$_POST['min8'],
        $_POST['min9'],$_POST['min10'],$_POST['min11'],$_POST['min12'],$_POST['min13'],$_POST['min14'],$_POST['min15'],$_POST['min16'],$_POST['min17'],$_POST['min18'],
        $_POST['min19'],$_POST['min20'],$_POST['min21'],$_POST['min22'],$_POST['min23'],$_POST['min24'],$_POST['min25'],$_POST['min26'],$_POST['min27'],$_POST['min28'],
        $_POST['min29'],$_POST['min30']);    
$log->createTable($tablename, $mins);
}
?> 

用于创建mysql的函数 我知道这不保护我的注入攻击的mysql等我为测试等而大量创建了它,并且无论如何都需要重做它以进行新的更改

function createTable($tablename, $mins){
        $con=mysqli_connect("localhost","****","****","****");

        // Check connection
        if (mysqli_connect_errno($con))
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        $tableCreate = "CREATE TABLE rates_{$tablename} (
                                              Weight int(11),
                                              CBMMin double,
                                              CBMMax double,
                                              Min double
                                              ) 
        ";
        $queryResult = mysqli_query($con, $tableCreate);
        if ($queryResult === TRUE) {
            print "<br /><br />Table Created";
            $queryResult = mysqli_query($con, "INSERT INTO Custom_Rates (TableName) VALUES ('rates_{$tablename}');");
            $rows = array(
                    array('weight' => 1000, 'cbm_min' => 0.1, 'cbm_max' => 2.3 ),
                    array('weight' => 1500, 'cbm_min' => 2.31, 'cbm_max' => 3.5 ),
                    array('weight' => 2000, 'cbm_min' => 3.51, 'cbm_max' => 4.6 ),
                    array('weight' => 2500, 'cbm_min' => 4.61, 'cbm_max' => 5.75 ),
                    array('weight' => 3000, 'cbm_min' => 5.75, 'cbm_max' => 6.9 ));
            $i = 0;
            foreach ($rows as $row) {
                $value = $mins[$i];
                if (empty($value)) {
                    $value = 0;
                }
                $queryResult = mysqli_query($con, "INSERT INTO rates_{$tablename} (Weight, CBMMin, CBMMax, Min) VALUES (".$row['weight'].",".$row['cbm_min'].",".$row['cbm_max'].",".$value.");");
                if ($queryResult === TRUE){

                }else{
                    print "<br /><br />No Row created. Check " . mysqli_error($con);
                }
                $i++;
            }
        } else {
            print "<br /><br />No TABLE created. Check " . mysqli_error($con);
        }

    }

如果这没有任何意义,请说,我会尝试绘制一些图像等。 我真的只是问我是否正确的方式,或者是否有可能/更好的方式来实现它

谢谢

1 个答案:

答案 0 :(得分:0)

你可以使用一些简单的JQuery

$(document).ready(function() {
   $('.button').click( 
     ('.table').html('echo the table HTML here');
       );
 });

在您的HTML中,添加一个空的div,并为其提供一个.table类,如上面的代码所示。

相关问题