我通过javascript动态地在表单上生成附加字段,并且每一行包含三个字段,因此我希望能够使用ajax将显示在php中的所有字段中的所有值发布到
am能够动态生成其他字段并发布到php,但只发送了一个列。
+这里是表格
<table id="nameBoxWrap">
<tr id="nameBox">
<input type="text" id="phonenumber" name="phonenumber[]" placeholder="phone number">
<input type='text' id='firstname' name='firstname[]' placeholder='firstname'>
<input type='text' id='lastname' name='lastname[]' placeholder='lastname'>
<input type="button" value="Load More" onclick="addNameSection()" >
<input type="button" value="Submit" id="submitdata1" style="margin-left:50px"><br>
<input type="hidden" id="addSectionCount" value="1" name="addSectionCount">
</tr>
</table>
+此处代码会动态生成其他字段
<script type="text/javascript">
function addNameSection(){
var addSectionCount=$("#addSectionCount").val();
addSectionCount++;
$("#addSectionCount").val(addSectionCount);
$("#nameBoxWrap").append('<tr id="nameBox'+addSectionCount+'"><td></td>'
+'<td><input type="text" placeholder="phone number" id="phonenumber'+addSectionCount+'" name="phonenumber[]"></td>'
+'<td><input type="text" id="firstname'+addSectionCount+'" name="firstname[]" placeholder="firstname"></td>'
+'<td><input type="text" id="lastname'+addSectionCount+'" name="lastname[]" placeholder="lastname"><td>'
+'<td><input type="button" value="REMOVE" onclick=removeNameSection("'+addSectionCount+'")></td></tr>');
}
</script>
+脚本,用于将表单数据提交到数据库
<script>
$(function(){
$('#submitdata1').on('click',function(e){
e.preventDefault(); // do not allow the default form action
var phoneS = $('input[name="phonenumber[]"]').serializeArray();
var firstn = $('input[name="firstname[]"]').serializeArray();
var lastname = $('input[name="lastname[]"]').serializeArray();
$.ajax({
method: "POST",
url: "senddata.php",
data:lastname
});
});
});
</script>
+这是我的PHP脚本
foreach($_POST['lastname'] as $key=>$value){
$phone = $_POST['phonenumber'][$key];
$lastname = $value;
$firstname = $_POST['firstname'][$key];
$query =mysqli_query($db,"insert into itequipment(phone_number,firstname,lastname) values ('$phone','$firstname','$lastname')");
}
+我想在数据库表的一行中存储名字,姓氏,电话号码,但只能发送和存储姓氏。
答案 0 :(得分:0)
尝试一下:
$('#submitdata1').on('click',function(e){
e.preventDefault(); // do not allow the default form action
var list = {};
var i = 0;
$( "tr" ).each(function() {
var inputs = $(this).find('input');
list[i] = {};
inputs.each(function() {
$input = $(this);
list[i][$input.attr('name')] = $input.val();
});
i++;
});
var json_data = JSON.stringify(list);
var json = JSON.parse(json_data);
$.ajax({
url : 'senddata.php.php',
type : 'POST',
dataType : 'json',
data : json
});
});
<table>
<tr class="line"> <!-- Line 1 -->
<td>
<input type="text" name='phonenumber' value="phone_1">
</td>
<td>
<input type="text" name='firstname' value="firstname_1">
</td>
<td>
<input type="text" name='lastname' value="lastname_1">
</td>
</tr>
<tr class="line"> <!-- Line 2 -->
<td>
<input type="text" name='phonenumber' value="phone_2">
</td>
<td>
<input type="text" name='firstname' value="firstname_2">
</td>
<td>
<input type="text" name='lastname' value="lastname_2">
</td>
</tr>
</table>
console.log(json)
的结果:
{0: {…}, 1: {…}}
0:
firstname: "firstname_1"
lastname: "lastname_1"
phonenumber: "phonenumber_1"
1:
firstname: "firstname_2"
lastname: "lastname_2"
phonenumber: "phonenumber_2"