[英]ajax with multiple records to insert into database
I have a block of code which is a dynamically generated div with a form (based on array loop) that has dynamic inputs which are added by a button: 我有一个代码块,它是一个动态生成的div,其形式(基于数组循环)具有通过按钮添加的动态输入:
<?php foreach($tickerDisplays as $key => $ticker):?>
<form id="Items" method="post">
<label id="ItemLabel">Item 1: </label>
<input type="text" name="Items[]"><br/>
<button type="button" class="moreItems_add">+</button>
<input type="hidden" name="tickerID" id="tickerID" value="<?php echo $ticker['ticker'] ?>">
<input type="submit" name="saveTickerItems" value="Save Ticker Items">
</form>
<?php endforeach;?>
<script type="text/javascript">
$("button.moreItems_add").on("click", function(e) {
var tickerID = $('#tickerID').val();
var numItems = $("input[type='text']", $(this).closest("form")).length;
if (numItems < 10) {
var html = '<label class="ItemLabel">Item ' + (numItems + 1) + ': </label>';
html += '<input type="text" name="Items[]"/><br/>';
$(this).before(html);
console.log(tickerID);
}
});
</script>
That code above is working and simply allows the '+' button to add a new input. 上面的代码可以正常工作,并且只需允许“ +”按钮添加新的输入即可。 I'm getting the input values as well as the tickerID from my hidden input in preparation for ajax submission.
我从隐藏的输入中获取输入值以及tickerID,以准备提交Ajax。 I'm getting what I expect from the serialized form but I have an issue.
我从序列化表格中得到了期望,但是有一个问题。
The following code: 如下代码:
<script type="text/javascript">
$("#Items").submit(function(e) {
e.preventDefault();
var data = $("#Items").serialize();
console.log(data);
});
</script>
Prints this: 打印此:
Items%5B%5D=this&Items%5B%5D=is&Items%5B%5D=test&tickerID=1
Which I expect. 我期望的。 The problem is that with my ajax call to my mysql insert function, I need to insert one record for each value plus the tickerID.
问题是,通过对我的mysql插入函数进行ajax调用,我需要为每个值和tickerID插入一条记录。 My sql insert is inserting into columns tickerID and content.
我的SQL插入正在插入tickerID和content列。 So for the above console.log, I would need the following insert:
因此,对于上面的console.log,我需要以下插入:
tickerID | content
----------------------
1 this
1 is
1 test
How can I properly pass my form data to the ajax and then do something like a foreach in order to insert multiple records? 如何正确地将表单数据传递给Ajax,然后执行诸如foreach之类的操作以插入多个记录?
ajax call 阿贾克斯电话
<script type="text/javascript">
$("#Items").submit(function(e) {
$.ajax({
type: "POST",
url: addticker.php,
data: form.serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
</script>
addticker.php addticker.php
$tickerID = $_POST[''];
$content = $_POST[''];
$addTicker = "
INSERT INTO tickerTable (tickerID, content)
values ('$tickerID', '$content');
"
$mysqlConn->query($addTicker)
Hope this works. 希望这行得通。
$items = $_POST['Items'];
$tickerID = $_POST['tickerID'];
foreach ($items as $item){
$addTicker = "
INSERT INTO tickerTable (tickerID, content)
values ('$tickerID', '$item');
"
$mysqlConn->query($addTicker);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.