gpt4 book ai didi

php - 单击按钮时添加额外的列以形成表格

转载 作者:行者123 更新时间:2023-11-30 01:20:41 24 4
gpt4 key购买 nike

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

<?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 我的注入(inject)攻击等,我只是为了测试等而创建它,无论如何都需要重做它以进行新的更改

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);
}

}

如果这没有任何意义,请说出来,我会尝试绘制一些图像等。我真的只是想问我是否正在采取正确的方法,或者是否有可能/更好的方法来解决这个问题。

最佳答案

您可以使用一些简单的 JQuery

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

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

关于php - 单击按钮时添加额外的列以形成表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18580127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com