将自动生成的表单字段值插入数据库

时间:2016-11-09 10:26:01

标签: javascript php mysql

您好我想将生成的字段表单值插入到我的mysql数据库中。它将客户数量放入下拉框并根据下拉值生成一组字段。如果下拉值为2,则将重复表单字段,并且所有填充的数据应该转到数据库。任何人都可以帮助我将这些插入到数据库中。

建立数据库连接并正常工作。

main.php javascript部分

//when the webpage has loaded do this
$(document).ready(function() {
  //if the value within the dropdown box has changed then run this code            
  $('#num_cat').change(function() {
    //get the number of fields required from the dropdown box
    var num = $('#num_cat').val();

    var i = 0; //integer variable for 'for' loop
    var html = ''; //string variable for html code for fields 
    //loop through to add the number of fields specified
    for (i = 1; i <= num; i++) {
      //concatinate number of fields to a variable
      html += '<h3 style="weight:bold;color:#0080FF; text-align: center;">CUSTOMER NO ' + i + '</h3> <br/> \n';
      html += 'First Name ' + ': <input class="form-control"  type="text" id="first_name" name="AgclifName' + i + '" />';
      html += 'Last Name ' + ': <input class="form-control"  type="text" id="first_name" name="AgclilName' + i + '" /><br/> \n';
      html += 'NIC / Passport Number ' + ': <input class="form-control" type="text" name="Agclinicpassport' + i + '"/><br/>';
      html += 'Nationality ' + ': <input class="form-control" type="text" name="AgcliNationality' + i + '"/><br/>';
      html += 'Age ' + ': <input class="form-control" type="text" name="Agcliage' + i + '"/><br/>';
      html += 'Weight' + ': <input class="form-control" type="text" name="Agcliweight' + i + '"/><br><br/><hr class="style13">';

    }

    //insert this html code into the div with id catList
    $('#catList').html(html);
  });
});

html部分

<form method="post" action="action.php">
  <div class='form-group'>
    <label class='control-label col-md-2 col-md-offset-2' for='id_num_of_cus'>Number of Customers:</label>
    <div class='col-md-8'>
      <div class='col-md-3'>
        <div class='form-group internal'>
          <select class='form-control' id="num_cat" name="num_cat" style="width: 200%;" required>
            <option value="0">- SELECT -</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
            <option value="11">11</option>
            <option value="12">12</option>
            <option value="13">13</option>
          </select>
        </div>
      </div>
    </div>
  </div>
  <div id="catList"></div>
  <div class='form-group'>
    <div class='col-md-offset-4 col-md-3'>
      <button class='btn-lg btn-primary' type='submit' value="Submit">Submit</button>
    </div>
    <div class='col-md-3'>
      <button class='btn-lg btn-danger' style='float:right' type='reset'>Cancel</button>
    </div>
  </div>

Mysql部分

foreach($_POST['num_cat'])
{
    // your insert query
    $sql = "INSERT INTO tblcsdetails (fname, lname, nicpass,nationality,age,weight) VALUES (' $_POST[AgclifName]',' $_POST[AgclilName]',' $_POST[Agclinicpassport]',' $_POST[AgcliNationality]',' $_POST[Agcliage]',' $_POST[Agcliweight]')";
}

0 个答案:

没有答案