我正在尝试动态地在我的示例表中添加值。这是index.php代码
<body>
<div class="container">
<br />
<br />
<h2 align="center"></h2>
<div class="form-group">
<form name="add_name" id="add_name">
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<td><input type="text" name="medname[]" placeholder="Medicine Name" class="form-control name_list" /></td>
<td><select name="frequency[]" class="form-control name_list"><option>24Hrs</option><option>8Hrs</option><option>12Hrs</option></select></td>
<td><input name = "morningmed[]" type ="checkbox" class = "form-control name_list"/></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>
</form>
</div>
</div>
</body>
<script>
$(document).ready(function(){
var i=1;
$('#add').click(function(){
i++;
$('#dynamic_field').append('<tr id="row'+i+'"><td><input type="text" name="medname[]" placeholder="Mediince Name" class="form-control name_list" /></td><td><select name="frequency[]" class="form-control name_list"><option>24Hrs</option><option>8Hrs</option><option>12Hrs</option></select></td><td><input name = "morningmed[]" type ="checkbox" class = "form-control name_list"/></td><td><button type="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();
});
$('#submit').click(function(){
$.ajax({
url:"name.php",
method:"POST",
data:$('#add_name').serialize(),
success:function(data)
{
alert(data);
$('#add_name')[0].reset();
}
});
});
});
</script>
以下是在table,name.php中插入数据的代码。目前我只是插入一列,medname。
<?php
error_reporting(E_ALL);
$connect = mysqli_connect("localhost", "root", " ","androidtest");
$medicineName = $_POST["medname"];
var_dump($medicineName);
$i=0;
$countPatientMedName = count($medicineName);
echo $countPatientMedName;
while ($i < $countPatientMedName) {
$medicineName = $medicineName[$i];
$result=mysqli_query($connect,"insert into tbl_name (medname) Values ('$medicineName')");
$i++;
}
?>
我插入了#hello&#39;和&#39; World&#39;,插入了2行,&#39; Hello&#39;并且&#39; e。不明白为什么会这样。无论我在第二行输入什么,它的第二个字母都插入数据库中。
帮助将不胜感激。