javascript - 如何在 Ajax 中传递动态 html 行的数据?

标签 javascript php jquery ajax

我有这样的表单,用户可以使用 JQuery 添加行:

<form id="frm_salary"  role="form" action="" method="post">
       <!--start of frm_salary--> 
       <div class="form-group">
          <label for="ca_salary_date1">Date:</label>
          <input type="date" class="form-control" name="ca_salary_date_filed" placeholder="Date" required/>
       </div>
       <div class="form-group">
          <label for="ca_purpose1">Purpose:</label>
          <input type="text" class="form-control" name="ca_salary_purpose" placeholder="Purpose" required/>
       </div>
       <table class="table table-bordered table-hover" id="tab_logic">
          <thead>
             <tr >
                <th class="text-center">
                   Area
                </th>
                <th class="text-center">
                   Date/s
                </th>
                <th class="text-center">
                   Number of Local Hires
                </th>
                <th class="text-center">
                   Salary Per Local Hire Per Day
                </th>
                <th class="text-center">
                   Amount
                </th>
             </tr>
          </thead>
          <tbody>
             <tr id='addr0'>
                <td>
                   <input type="text" name='ca_salary_area[]'  placeholder='Area' class="form-control" required/>
                </td>
                <td>
                   <input type="text" name='ca_salary_date[]' placeholder='Date/s' class="form-control" required/>
                </td>
                <td>
                   <input type="text" name='ca_salary_localhires[]' placeholder='Number of Local Hires' class="form-control" required/>
                </td>
                <td>
                   <input type="text" name='ca_salary_perday[]' placeholder='Salary Per Local Hire Per Day' class="form-control" required/>
                </td>
                <td>
                   <input type="text" name='ca_salary_amount[]' placeholder='Amount' class="form-control" />
                </td>
             </tr>
             <tr id='addr1'></tr>
          </tbody>
       </table>
       <div class="form-group">
          <input type="hidden" class="form-control" name="ca_total_salary" >
       </div>
       <div class="text-center"> 
          <input type="submit" name="submit_salary" class="btn btn-success" value="Submit" /> 
       </div>
    </form>
    <!--end of frm_salary-->

我还有这个 PHP 代码,用于将其值插入数据库。它正在工作。

<?php
$total_amount = 0;
if (isset($_POST['submit_salary'])) {
    //calculations
    for ($j = 0; $j < count($_POST['ca_salary_area']); $j++) {
        $nos_hire1               = $_POST['ca_salary_localhires'][$j];
        $nos_salaray1            = $_POST['ca_salary_perday'][$j];
        $subtotal_salary_amount1 = $nos_hire1 * $nos_salaray1;
        $total_amount += $subtotal_salary_amount1;
    }

    $form_data               = array();
    $form_data['date_filed'] = mysql_real_escape_string($_POST['ca_salary_date_filed']);
    $form_data['purpose']    = mysql_real_escape_string($_POST['ca_salary_purpose']);
    $form_data['total']      = $total_amount;

    $tbl    = "tblcasalaryform";
    // retrieve the keys of the array (column titles)
    $fields = array_keys($form_data);

    // build the query
    $sql_salary = "INSERT INTO " . $tbl . "
                                (`" . implode('`,`', $fields) . "`)
                                VALUES('" . implode("','", $form_data) . "')";

    // run the query result resource
    mysql_query($sql_salary);
    $last_id_inserted = mysql_insert_id($bd);

    echo $last_id_inserted;


    for ($i = 0; $i < count($_POST['ca_salary_area']); $i++) {
        $nos_hire               = $_POST['ca_salary_localhires'][$i];
        $nos_salaray            = $_POST['ca_salary_perday'][$i];
        $subtotal_salary_amount = $nos_hire * $nos_salaray;
        $sql                    = "INSERT INTO `tblcasalaryformdetails` SET
                                                `area` = '" . $_POST['ca_salary_area'][$i] . "', 
                                                `dates` = " . $_POST['ca_salary_date'][$i] . ",
                                                `number_of_local_hires` = " . $_POST['ca_salary_localhires'][$i] . ",
                                                `salary_per_local_hire_per_day` = " . $_POST['ca_salary_perday'][$i] . ",
                                                `amount` = " . $subtotal_salary_amount . ",
                                                `casalaryform_id` = " . $last_id_inserted . "";
        mysql_query($sql);
    }
    echo '<p class="bg-success" style="padding:5px;border-radius:5px;text-align:center">Successfully Inserted</p>';
}

?>

我想为此使用 Ajax,我的问题是如何从动态创建输入名称的表单表中传递/获取值?

最佳答案

是 jQuery 吗?
然后是这样的:

var data = $( "#frm_salary" ).serialize();
$.post( "test.php", data )

但是因为 jQuery.serialize() 在方括号方面存在一些问题,要将它们放入 $_POST 中,您所要做的就是添加一点魔法:

var data = $( "#frm_salary" ).serialize().replace(/%5B%5D/g, '[]');
$.post( "test.php", data )

就这么简单!

关于javascript - 如何在 Ajax 中传递动态 html 行的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28007890/

相关文章:

javascript - 在 PHP 页面上用 Javascript 创建 XML 会导致 '<?' 被解释为 PHP

php - 实现python切片表示法

javascript - JQuery 更改标签中的文本

javascript - 如何渲染JS为cookie生成指纹?

javascript - jQuery ajax 调用 mvc Action 不会触发成功/错误

javascript - 如何使用 Javascript/jQuery 将用户带到新页面以及一些变量,而不会导致 URI Too Large 错误?

javascript - 如何使用 HTML 从树结构中显示选定的文本

javascript - 在ajax成功时重新绘制Jqplot

php - JavaScript QR 码阅读器 - 可以吗?或者,远程服务?

javascript - 在滚动上缩放 div - Javascript