从Jquery创建的动态表单中获取值到php

时间:2015-09-27 12:55:54

标签: javascript php jquery

我想尝试从我在页面上动态创建的表单中获取值; 这是一些代码:

$('#InsertTicket').click(function() {
  $('#FistticketSub').after("<tr id='row" + NumTicket + "'> <th class='col-xs-3' ><input type='text' class='form-control' name='Typeticket[]' id='Typeticket " + NumTicket + "' value='" + $("#Typeticket").val() + "' required='required'></th>" +
    "<th class='col-xs-3'><input type='number' value='" + $("#Jumlah").val() + "' class='form-control' name='Jumlah[]' id='Jumlah" + NumTicket + "' required='required'></th>" +
    "<th class='col-xs-6'>" +
    "<div class='form-group'>" +
    "<label class='sr-only' for='exampleInputAmount'>Amount</label>" +
    "<div class='input-group'>" +
    "<div class='input-group-addon'>Rp.</div>" +
    "<input type='number' value='" + $("#Harga").val() + "'class='form-control' name='Harga[]' id='Harga" + NumTicket + "' placeholder='Amount'required='required'>" +
    "<div class='input-group-addon'>.000</div>" +
    "</div>" +
    "</div></th>" +
    "<th><a class='btn btn-success' onclick='delet(" + NumTicket + ")' >Delete</a></th></tr>");
  NumTicket++;
});
$i = 0;

 while (isset($_POST["Typeticket"][$i])) {

   $dataTicket[$i][0] = $_POST["Typeticket"][$i];
   $dataTicket[$i][1] = $_POST["Jumlah"][$i];
   $dataTicket[$i][2] = $_POST["Harga"][$i];
   $dataTicket[$i][3] = $i + 1;
   $i++;
   insert("Ticket",$dataTicket[$i]);//isnert data to data base
 }

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

假设您使用PHP作为后端,为什么不使用:

<form action="processform.php" method="POST">
    <input type="number" name="harga" value=""/>
    ...
    <input type="submit" name="sbmt" value="Submit"/>
</form>

只需将其转换为javascript即动态表单。

processform.php就像:

<?php

    if(isset($_POST["sbmt"]))
    {
        $i = 0;

        while (isset($_POST["Typeticket"][$i])) 
        {

           $dataTicket[$i][0] = $_POST["Typeticket"][$i];
           $dataTicket[$i][1] = $_POST["Jumlah"][$i];
           $dataTicket[$i][2] = $_POST["Harga"][$i];
           $dataTicket[$i][3] = $i + 1;
           $i++;
           insert("Ticket",$dataTicket[$i]);//isnert data to data base
        }
    }

?>