我试图使用ajax从具有相同结构和名称的多个输入字段的表单填充我的mysqli数据库,但我非常困在这里。我已经尝试了几个解决方案,但都有相同的结果,只有我的最后两个输入字段被发送到我的数据库。
我的表单看起来像这样;
<form id="sendform" method="post" action="#">
<fieldset>
<input type="text" value="JD01" id="shortname" name="member[shortname]" readonly />
<input type="text" value="John Doe" id="fullname" name="member[fullname]" readonly />
</fieldset>
<fieldset>
<input type="text" value="JD02" id="shortname" name="member[shortname]" readonly />
<input type="text" value="Jane Doe" id="fullname" name="member[fullname]" readonly />
</fieldset>
<input type="submit" value="Send" />
</form>
ajax部分;
$('#sendform').submit(function(e){
$.ajax({
type: 'POST',
url: 'inc/post.php',
data: $(this).serialize(),
success: function(data){
alert("Data Save: " + data);
}
});
e.preventDefault();
});
我的php;
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', 'root');
define('DBNAME', 'test');
$db = new mysqli(HOST, USER, PASS, DBNAME);
$values = array();
foreach ($_POST['member'] as $member) {
$values[] = '(' . $member['shortname'] . ',' . $member['fullname'] . ')';
}
$sql = "INSERT INTO sendform (shortname, fullname) VALUES " . implode(',', $values);
$result = $db->query($sql);
if($result) {
echo "Yep";
}
$db->close();
我在这里做错了什么?日Thnx!
答案 0 :(得分:1)
试试这个:
<form id="sendform" method="post" action="#">
<fieldset>
<input type="text" value="JD01" id="shortname" name="member[shortname][]" readonly />
<input type="text" value="John Doe" id="fullname" name="member[fullname][]" readonly />
</fieldset>
<fieldset>
<input type="text" value="JD02" id="shortname" name="member[shortname][]" readonly />
<input type="text" value="Jane Doe" id="fullname" name="member[fullname][]" readonly />
</fieldset>
<input type="submit" value="Send" />
</form>
和PHP页面
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', 'root');
define('DBNAME', 'test');
$db = new mysqli(HOST, USER, PASS, DBNAME);
$values = array();
for($i=0 ;$i < count($_POST['member']); $i++) {
$values[] = '("' . $_POST['member']['shortname'][$i] . '","' . $_POST['member']['fullname'][$i] . '")';
}
$sql = "INSERT INTO sendform (shortname, fullname) VALUES " . implode(',', $values);
$result = $db->query($sql);
if($result) {
echo "Yep";
}
$db->close();
答案 1 :(得分:0)
您可以使表单看起来像这样(请注意,member []现在是一个数组本身)
<form id="sendform" method="post" action="">
<fieldset>
<input type="text" value="JD01" id="shortname" name="member[1][shortname]" readonly />
<input type="text" value="John Doe" id="fullname" name="member[1][fullname]" readonly />
</fieldset>
<fieldset>
<input type="text" value="JD02" id="shortname" name="member[2][shortname]" readonly />
<input type="text" value="Jane Doe" id="fullname" name="member[2][fullname]" readonly />
</fieldset>
<input type="submit" value="Send" />
</form>
在PHP使用中
foreach ($_POST['member'] as $member) {
$values[] = '(' . $member['shortname'] . ',' . $member['fullname'] . ')';
}
答案 2 :(得分:0)
您在html中使用了成员[shortname]和成员[fullname]两次。因此,第二个字段集中的值将覆盖第一个字段集。
因此,我们只得到第二个字段集中的值。
您可以尝试使用以下代码:
<form id="sendform" method="post" action="#">
<fieldset>
<input type="text" value="JD01" id="shortname" name="member['shortname'][0]" readonly />
<input type="text" value="John Doe" id="fullname" name="member['fullname'][0]" readonly />
</fieldset>
<fieldset>
<input type="text" value="JD02" id="shortname" name="member['shortname'][1]" readonly />
<input type="text" value="Jane Doe" id="fullname" name="member['fullname'][1]" readonly />
</fieldset>
<input type="submit" value="Send" />
</form>
我的php,进行以下更改:
$values = array();
$i = 0;
foreach ($_POST['member'] as $member) {
$values[] = '(' . $member['shortname'][$i] . ',' . $member['fullname'][$i] . ')';
$i++;
}
答案 3 :(得分:0)
<td class="text-left"><input type="text" class="form-control input-lg" name="expense[date][]" required placeholder="Enter Date (yy-mm-dd)"></td>
<td class="text-left"><input type="text" class="form-control input-lg" name="expense[msg][]" required placeholder="Enter Expenses Here"></td>
<td class="text-left"><input type="text" class="form-control input-lg" name="expense[vendor][]" required placeholder="Vendor Name Here"></td>
<td class="text-left"><input type="text" class="form-control input-lg" name="expense[amount][]" required placeholder="Enter Amount Here"></td>
<td class="text-left"><input type="text" class="form-control input-lg" name="expense[prove][]" required placeholder="Prove"></td>
PHP Code
<?php
if(isset($_POST['submit'])){
//$date = mysql_real_escape_string($_POST['date']);
//$date = strtotime($date);
// $date = date('Y-m-d', $date);
//$msg = mysql_real_escape_string($_POST['msg']);
//$vendor = mysql_real_escape_string($_POST['vendor']);
//$amount = mysql_real_escape_string($_POST['amount']);
//$prove = mysql_real_escape_string($_POST['prove']);
$values = array();
for($i=0 ;$i < count($_POST['expense']); $i++) {
$values[] = '("' . $_POST['expense']['date'][$i] . '","' . $_POST['expense']['msg'][$i] . '","' . $_POST['expense']['vendor'][$i] . '","' . $_POST['expense']['amount'][$i] . '","' . $_POST['expense']['prove'][$i] . '")';
}
$sql = ("INSERT INTO expenses (date, msg, vendor, amount, prove) VALUES " . implode(',', $values));
echo "<script>alert('Your expenses data inserted successfully!')</script>";
echo "<script>window.open('expenses.php','_self')</script>";
exit();
}
?>
show me the error Notice: Undefiened offset:2 in ..............