1

我做了一个简单的 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'];

谢谢!

4

2 回答 2

5

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

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

当您执行提交并将值发布到服务器时,您将能够使用 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.
}

这应该允许你清理你的代码,你可以对你的权重做同样的事情。

于 2013-01-16T01:06:49.533 回答
-1

::::::::查看代码后编辑:::::::::

你用你的代码来解决这一切都是错的。但是,如果您想使用现有代码使其正常工作,请尝试以下操作:

改变

<tr> <td><input type = "text" name = "grade12" /> </td> <td> <input type = "text" name = "weight12"  /> </td> </tr>

<tr id="last"> <td><input type = "text" name = "grade12" /> </td> <td> <input type = "text" name = "weight12"  /> </td> </tr>

那么jQuery看起来像这样:

$("#add-button").click(function() {
      $('#last').append("<tr><td><input type = \"text\" name = \"customgrades[\]\" /> </td> <td> <input type = \"text\" name = \"customweight[\]\"  /> </td> </tr>");
    });

然后做一个按钮或链接添加成绩

提交表单后,在 php 方面

$_REQUEST[customgrades];
$_REQUEST[customweight];

或者,如果您在互联网上的精英朋友想要责备您使用 $_REQUEST,即使在此应用程序中您没有理由不能使用它,您也可以使用:

$_POST[customgrades];
$_POST[customweight];

这两个变量将保存具有相关数据的数组。

于 2013-01-16T01:04:55.123 回答