gpt4 book ai didi

javascript - 使用 Jquery Ajax 在 PHP 中动态添加/删除多个输入字段

转载 作者:行者123 更新时间:2023-11-30 09:45:28 25 4
gpt4 key购买 nike

<分区>

这是我表单的 HTML

<div class="crcform">

<h1>Internship Details</h1>

<form name="internship_details" id="intership_details">
<table class="table table-bordered" id="dynamic_field">
<tr>
<td>
<!--div class="top-row"-->
<div class="field-wrap">
<label>
Company<span class="req">*</span>
</label>
<input type="text" required autocomplete="off" name="company[]"/>
</div>

<div class="field-wrap">
<label>
Project<span class="req">*</span>
</label>
<input type="text"required autocomplete="off" name="project[]"/>
</div>

<div class="field-wrap">
<label>
Duration<span class="req">*</span>
</label>
<input type="text"required autocomplete="off" name="duration[]"/>
</div>

<div class="field-wrap">
<label>
Key Learning<span class="req">*</span>
</label>
<input type="text"required autocomplete="off" name="key_learning[]"/>
</div></td>
<td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
</tr>
</table>
<input type="button" name="submit" id="submit" class="btn btn-info" value="Submit" />
<!--div class="top-row">
<div class="field-wrap">
<button class="button button-block" name="submit" id="submit"/>NEXT</button>
</div-->

</form>
</div>

这是动态添加/删除表单的javascript

<script>
$(document).ready(function(){
var i = 1;
$('#add').click(function(){
i++;
$('#dynamic_field').append('<tr id="row'+i+'"><td><div class="field-wrap"><label>Company<span class="req">*</span></label><input type="text" required autocomplete="off" name="company[]"/></div><div class="field-wrap"><label>Project<span class="req">*</span></label><input type="text"required autocomplete="off" name="project[]"/></div><div class="field-wrap"><label>Duration<span class="req">*</span></label><input type="text"required autocomplete="off" name="duration[]"/></div><div class="field-wrap"><label>Key Learning<span class="req">*</span></label><input type="text"required autocomplete="off" name="key_learning[]"/></div></td></td><td><button name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
});
$(document).on('click','.btn_remove', function(){
var button_id = $(this).attr("id");
$("#row"+button_id+"").remove();
});

$('#sumbit').clic(function(){
$.ajax({
url:"internship_details.php",
method:"POST",
data:$('#internship_details').serialize(),
success:function(data)
{
alert(data);
$('#internship_details')[0].reset();
}
});
});
});
</script>

这是在数据库中存储数据的php代码

<?php

include 'connection.php';

$number = count($_POST["company"]);
if ($number > 0){
for($i = 0; $i < $number; $i++){
if(trim($_POST["company"][$i]) != ''){
$sql = "INSERT INTO internship VALEUS('".mysqli_real_escape_string($connect, $_POST["company"][$i]."')";
mysqli_query($connect, $sql);
}
}
echo 'Data Inserted';
}
else{
echo "Enter Name";
}
?>

我无法将值存储在数据库中。谁能告诉我哪里出错了?你能告诉我它的插入查询是什么吗,因为我在一行中添加了多个列。所以帮我解决这个问题

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com