我需要在以下代码中提供一些建议。
var count = 1;
$(function(){
$('#addmore').click(function(){
count += 1;
$('ul').append("<li><input type='text' id='hd"+count+"' name='hd"+count+"' value='heading' onClick='this.select();'> <input type='text' id='amount"+count+"' name='amount"+count+"' value='amount' onClick='this.select();'></li>");
// return false;
});
});
我应该使用什么确切的 php 代码来将所有输入提交到我的 mysql 测试表中。
<button id="addmore">+</button>
<form id="myForm" name="myForm" action="" method="post" onSubmit="return false;">
<ul>
<li><input type="text" id="hd1" name="hd1" value="heading" onClick="this.select();">
<input type="text" id="amount1" name="amount1" value="amount" onClick="this.select();"></li>
</ul>
<input type="submit" value="Submit" id="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
echo "h";
};
?>
请记住,我已经测试了包含两个字段“标题”和“金额”的表。
<小时/>if(isset($_POST['submit'])){
foreach($_POST['hd'] as $hds){
echo $hds;
mysqli_query($con,"INSERT INTO test (hdg) VALUES ($hds)");
};
};
并且它没有将数据添加到我的表中
最佳答案
使用[]
作为name
属性而不是数字:
<li><input type="text" id="hd1" name="hd[]" value="heading" onClick="this.select();">
在服务器端,您将有一个值数组。
$hd = $_POST['hd']; // Returns an array
关于php - 通过jquery动态输入并提交到mysql表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16783297/