gpt4 book ai didi

php - 如何允许用户添加另一个提交到 php 文件的输入行

转载 作者:行者123 更新时间:2023-12-01 07:20:28 25 4
gpt4 key购买 nike

我制作了一个简单的 GPA(平均绩点)计算器,我只是在玩一些东西,并且真的很想知道如何允许用户随意添加行。我能找到的最好的例子是在这个网站上:http://gpacalculator.net/high-school-gpa-calculator/

我想我可能会理解,当用户单击按钮时,jquery 函数可以添加一行,但是我如何获取它,以便每次有人添加行时输入元素的名称都不同。 php 文件如何知道 $_POST['someName'].

简而言之,问题是:如何使用 jquery 添加一行,然后将该表单提交到 php 文件,以便它与预制的行不同。

以防万一您需要知道,我将发送到 php 文件的值存储在变量中。然后将它们输入到计算 GPA 的公式中。

编辑这里是表的代码:

<table>
<form id = "myform" name = "myform" method = "POST" action = "">
<tr>
<th> Grade in test </th>
<th> Weight of grade </th> <br />
</tr>
<tr> <td><input type = "text" name = "grade1" /> </td> <td> <input type = "text" name = "weight1" /> </td> </tr>
<tr> <td><input type = "text" name = "grade2" /> </td> <td> <input type = "text" name = "weight2" /> </td> </tr>
<tr> <td><input type = "text" name = "grade3" /> </td> <td> <input type = "text" name = "weight3" /> </td> </tr>
<tr> <td><input type = "text" name = "grade4" /> </td> <td> <input type = "text" name = "weight4" /> </td> </tr>
<tr> <td><input type = "text" name = "grade5" /> </td> <td> <input type = "text" name = "weight5" /> </td> </tr>
<tr> <td><input type = "text" name = "grade6" /> </td> <td> <input type = "text" name = "weight6" /> </td> </tr>
<tr> <td><input type = "text" name = "grade7" /> </td> <td> <input type = "text" name = "weight7" /> </td> </tr>
<tr> <td><input type = "text" name = "grade8" /> </td> <td> <input type = "text" name = "weight8" /> </td> </tr>
<tr> <td><input type = "text" name = "grade9" /> </td> <td> <input type = "text" name = "weight9" /> </td> </tr>
<tr> <td><input type = "text" name = "grade10" /> </td> <td> <input type = "text" name = "weight10" /> </td> </tr>
<tr> <td><input type = "text" name = "grade11" /> </td> <td> <input type = "text" name = "weight11" /> </td> </tr>
<tr> <td><input type = "text" name = "grade12" /> </td> <td> <input type = "text" name = "weight12" /> </td> </tr>

<tr>
<td> <div id = "submitButton">
<input type = "submit" value = "SUBMIT" style = " font-family:'HelveticaNeue-Light', 'Helvetica Neue Light', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; padding: 10px;border: none; color: white; background-color: #0499ff; width: 100%; font-size: 13px; border-radius: 5px;""/></div></td> <td> <input type = "reset" /> </td></tr>

</form>
</table>
<div id = "results2"></div>
</div>

然后 PHP 文件中的代码缩减为初始变量:

/* GRADES VARS */
$grade1 = $_POST['grade1'];
$grade2 = $_POST['grade2'];
$grade3 = $_POST['grade3'];
$grade4 = $_POST['grade4'];
$grade5 = $_POST['grade5'];
$grade6 = $_POST['grade6'];
$grade7 = $_POST['grade7'];
$grade8 = $_POST['grade8'];
$grade9 = $_POST['grade9'];
$grade10 = $_POST['grade10'];
$grade11 = $_POST['grade11'];
$grade12 = $_POST['grade12'];


/* GRADE WEIGHT VARS */
$weight1 = $_POST['weight1'];
$weight2 = $_POST['weight2'];
$weight3 = $_POST['weight3'];
$weight4 = $_POST['weight4'];
$weight5 = $_POST['weight5'];
$weight6 = $_POST['weight6'];
$weight7 = $_POST['weight7'];
$weight8 = $_POST['weight8'];
$weight9 = $_POST['weight9'];
$weight10 = $_POST['weight10'];
$weight11 = $_POST['weight11'];
$weight12 = $_POST['weight12'];

谢谢!

最佳答案

所以基本上你需要使用字段名称作为数组,所以举个例子:

<input type="text" name="someName[]" value="0" />
<input type="text" name="someName[]" value="2" />
<input type="text" name="someName[]" value="4" />
<input type="text" name="someName[]" value="6" />

当您执行 for 提交并将值发布到服务器时,您将能够使用 PHP 以数组形式访问它们,如下所示:

echo $_POST['someName'][0]; // outputs 0
echo $_POST['someName'][1]; // outputs 2
echo $_POST['someName'][2]; // outputs 4
echo $_POST['someName'][3]; // outputs 6

现在,由于您不知道有多少行,因此可以对其进行循环:

foreach($_POST['someName'] as $someName){
echo $someName; //will output the value 0/2/4/6 depending on the iteration
}

使用 JavaScript 添加按钮时,您只需确保名称相同并带有括号:

$('#addButton').click(function(){
$('.container').append('<input type="text" name="someName[]" value="someNumber" />');
});

只要你有这个,并且输入位于可以提交的表单标记内,这就可以工作。

编辑,因此根据您添加到初始答案中的代码,我建议利用 PHP 中的数组。

假设您使用了我上面解释的输入(顺便说一下,这可以用于任何可以发布到后端的 HTML 元素 [复选框、输入、文本区域、选择等...])。你可以这样做:

$grades = array();
foreach($_POST['grade'] as $g){
$grades[] = $g; // add grade from each input to the array
}

// then you can manipulate the data through looping through it.
foreach($grades as $grade){
// do something here with each grade...whether it be manipulating it
// or adding it another array with some changes whatever.
}

这应该允许您清理代码,并且您可以对权重执行相同的操作。

关于php - 如何允许用户添加另一个提交到 php 文件的输入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14349775/

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