gpt4 book ai didi

php - 将 foreach 值添加到 Ajax

转载 作者:行者123 更新时间:2023-11-29 03:22:36 25 4
gpt4 key购买 nike

我有以下两个问题;1)我如何将一个值从 foreach 传递给 ajax。这是我的代码和我到目前为止所拥有的,尝试将 ajax 放在 foreach 中,但它一直在为我提供 for each 中姓氏的 id。

2) 有没有一种方法可以让我点击添加,数据将保存并放入此 html 表格下方的另一个 html 表格中,但在我点击第二个表格中的插入之前不会插入到数据库中。

 <table class="table table-bordered datatable" id="table_export">
<thead>
<tr>
<th>#</th>
<th><div>Item</div></th>
<th><div>Quantity Left</div></th>
<th><div>Price</div></th>
<th><div>Quantity</div></th>
<th><div>Sell</div></th>

</tr>
</thead>
<tbody>
<?php
$count = 1;
$notarray = DataDB::getInstance()->get_rows_from_field('name');
foreach($notarray as $row):?>

<tr>

<td><?php echo $count++;?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['nickname'];?></td>
<td><?php echo $row['surname']; ?></td>

<form method="post" action="" role="form">
<td>

<div class="form-group">

<div class="">
<input type="number" class="form-control" name="kin" data-validate="required" data-message-required="Kin required" autofocus>
</div>
</div>

</td>

<td>

<div class="btn-group">
<button type="submit" class="btn btn-primary" name = "check">Add</button>
</div>
</td>
</form>

</tr>

<?php endforeach;?>
</tbody>
</table>

<br><br>
<div id="get_result">
</div>


<script type="text/javascript">


$(function () {

$('form').on('submit', function (e) {

e.preventDefault();

$.ajax({
type: 'post',
url: 'verify.php',
data: $('form').serialize() + '&ins=sellin' + '&id=<?php echo $row['name_id'];?>',
success: function(data)
{
jQuery('#get_result').html(data);
}
});

});

});

最佳答案

如果我没看错,你的意思是你想从表 A 的项目列表中选择一个项目,它应该显示在表 B 中。之后,你想单击表 B 中的提交按钮以保存从表中选择的项目一个?

解决方法如下:

请将此文件命名为 test.php

<?php
/**
* Created by PhpStorm.
* User: SQ05
* Date: 06/01/2017
* Time: 06:31 PM
*/
//Note that data is a typical example of data u gotten from DB. so make sure u push all $row into an array
//such that u create something of this nature eg
// $data=array();
// $i=0;
//while($row=mysql_fetch_assoc($query)){
//$data[$i]=$row;
//$i++;
//}
// this will then create data of this nature below,
$data=array(
array('id'=>1,'name'=>'Loveth','age'=>23,'phone'=>'9889087455'),
array('id'=>2,'name'=>'Susan','age'=>51,'phone'=>'787987455'),
array('id'=>3,'name'=>'Precious','age'=>13,'phone'=>'98989087455'),
array('id'=>4,'name'=>'Hannah','age'=>21,'phone'=>'987087455'),
array('id'=>5,'name'=>'Kenneth','age'=>43,'phone'=>'569087455'),
);

?>
<div style="float:left;width:50%">
THE LIST OF ALL ITEMS
<table>
<tr>
<td>Name</td>
<td>Age</td>
<td>Phone</td>
<td>Action</td>
</tr>
<?php
$i=0;
for ($i=0;$i<count($data);$i++) {
$item=$data[$i];
?>
<tr>
<td><?php echo $item['name'];?></td>
<td><?php echo $item['age'];?></td>
<td><?php echo $item['phone'];?></td>
<td><button onclick='addToList(<?php echo json_encode($item);?>);'>Add</button></td>
</tr>
<?php
}
?>
</table>
</div>

<div style="float:left; width:50%">
THE ITEM TO BE SAVED
<div id="showToSave"></div>
</div>

<script src="bower_components/jquery/dist/jquery.js"></script> <!--Please install jquery base on ur own directory path-->
<script>
var listToSave=[]; // must be global

/**
* The add to list function that process data and add to list
* @param data
*/
var addToList= function(data){
var lenData=listToSave.length;
if(lenData>0){
//this is used to avoid duplicate
for(var j=0;j<lenData;j++){
if(data.id==listToSave[j].id) return;
}
}
listToSave.push(data);
console.log(listToSave);
document.getElementById('showToSave').innerHTML=createData(listToSave);

};

var createData= function (data) {
var len=data.length;
var tableToSave="<table><tr><td>Name</td> <td>Age</td> <td>Phone</td> <td>Action</td></tr>";
var i;
for(i=0;i<len;i++){
content=data[i];
tableToSave+="<tr><td>"+content.name+"</td><td>"+content.age+"</td><td>"+content.phone+"</td><td>" +
"<button onclick='deleteFromSave("+i+")'>Delete</button></td></tr>";
}
tableToSave+="</table><div><button onclick='saveData()' type='button'>Save</button></div>";
return tableToSave;
};


/**
* This is use to delete data added locally
*/
var deleteFromSave=function (index) {
listToSave.splice(index,1); //this is use to delete from list to save
document.getElementById('showToSave').innerHTML=createData(listToSave); //to rerender after delete
};

/**
* This is use to submit data
*/
var saveData=function () {
console.log('thjis=',listToSave);
$.ajax({
type: "POST",
url: "getData.php",
data: {list : JSON.stringify(listToSave)},
success: function(resp){
console.log('response=',resp);
}
});
};
</script>

请将其命名为getData.php,以使用多值插入方法处理添加数据列表的提交。

    <?php
/**
* Created by PhpStorm.
* User: SQ05
* Date: 06/01/2017
* Time: 07:28 PM
*/
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$data = json_decode(stripslashes($_POST['list'])); //the data here can now be used to create a multiple value insert to ur mysql db.
print_r(json_encode($data)); // this is used to send response back to ur page if using array buh echo if string
}

关于php - 将 foreach 值添加到 Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41494493/

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