Post不能正确使用JavaScript动态创建的文本框

Post not working correctly with JavaScript dynamically created textboxes

本文关键字:创建 动态 文本 JavaScript 不能 Post      更新时间:2023-09-26

我从来没有尝试过发布用javascript创建的动态文本框来处理数据。发送到处理页面的唯一表单数据是提交按钮值。关于如何做到这一点,使所有的文本框数据通过邮政正确发送的想法?

页面代码:

<?php
if(!isset($_GET['saleid'])) {
    header('location: dashboard.php');
    die();
}else{
    include('db_connect.php');
    $saleid = mysqli_real_escape_string($mysqli, $_GET['saleid']);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script>
$(function() {
    $("#date").datepicker({ dateFormat: 'yy-mm-dd' })
    $(document).ready(function(){
        var counter = 2;
    $("#addButton").click(function () {
    if(counter>5){
            alert("Only 10 textboxes allow");
            return false;
    }   
    $("#sale > tbody").append("<tr><td> <label>Date:</label><br /><input type='textbox' id='date" + counter + "' ></td><td> <label>Type:</label><br /><input type='textbox' id='type" + counter + "' ></td><td> <label>Amount:</label><br /><input type='textbox' id='amount" + counter + "' ></td><td><label>Notes:</label><br /><input type='textbox' id='notes" + counter + "' ></td></tr>");

    counter++;
     });
     $("#removeButton").click(function () {
    if(counter<3){
          alert("No more textbox to remove");
          return false;
       }   
    counter--;
        $('#sale tr:last').remove();
   })
     });
});
</script>
</head>
<body>
<?php echo "Sale ID: ". $saleid; ?>
<form id="form1" name="form1" method="post" action="<?php echo "process_saletrans.php?saleid=" . $saleid; ?>">
<h2>Sale Transaction</h2>
          <table id="sale" width="300" border="0" cellpadding="2">
            <tr>
              <td> <label>Date:</label><br />
            <input type='textbox' id='date1' ></td>
              <td> <label>Type:</label><br />
            <input type='textbox' id='type1' ></td>
            <td> <label>Amount:</label><br />
            <input type='textbox' id='amount1' ></td>
            <td> <label>Notes:</label><br />
            <input type='textbox' id='notes1' ></td>
      </table>
    <input type='button' value='Add' id='addButton'>
<input type='button' value='Remove' id='removeButton'>
<p>
  <input type="submit" name="submit" id="submit" value="Add Transaction" />
</p>
</form>
</body>
</html>

我认为您遇到的问题是您正在使用id而不是name。您可以同时使用id,但我很确定它不会设置表单字段的名称以提交表单。