单击按钮时,我已经在 Ajax 的帮助下获取(列出)数据,现在我想将此数据插入(提交)到另一个表中



提交表格后,我没有在页面上获得帖子数据。 这是索引.php

<!DOCTYPE html>
<html>
<head>
<title>employee list</title>
</head>
<body>
<select name="fetch" >
<option value='emp_id'>multiple names of employee </option>
</select>
<form method="post" action="addempaonthertbl.php">
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Batch no</th>
<th>Address</th>
</tr>
</thead>
<tbody id="listing">
</tbody>
</table>
<input type="submit" name="submit" value="add">
</form>
</body>
</html>

更改下拉列表后运行脚本

<script>
$(document).ready(function(){
$("fetch").change(function()
{
$.ajax({
type: 'POST', 
data: 'emp_id='+emp_id,     
url: 'fetch.php' ,
success: function(response)
{
jQuery('#listing').html(response);
}
});
});
});
</script>        

从获取中获取员工的所有详细信息.php

$result = $this->db->query(" select * from tbl_emp where emp_id ='$emp_id'")->result_array();
$x = 1;
foreach($result as $row)
{
$pid = $row['prodid'];
echo "<tr>";
echo "<td>". $x++. "</td>";
echo "<td>".$row['name']. "</td>";
echo "<td>".$row['batchno']. "</td>";
echo "<td>".$row['address']. "</td>";
echo "</tr>";
}

我已经添加了一些代码并修改了一些部分,但帖子仍然无法提交。 我该如何解决这个问题?

你不能像你使用的那样在 JavaScript 中使用 Select name。 您可以在选择选项中使用 id,而不是在 JavaScript 中使用该 ID。

试试这个代码

<!DOCTYPE html>
<html>
<head>
<title>employee list</title>
</head>
<body>
<select name="fetch" id="fetch">
<option value='emp_id'>multiple names of employee </option>
</select>
<form method="post" action="addempaonthertbl.php">
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Batch no</th>
<th>Address</th>
</tr>
</thead>
<tbody id="listing"></tbody>
</table>
<input type="submit" name="submit" value="add">
</form>
</body>
</html>
<script>
$(document).ready(function() {
$("#fetch").change(function() {
$.ajax({
type: 'POST',
data: 'emp_id=' + emp_id,
url: 'fetch.php',
success: function(response) {
jQuery('#listing').html(response);
}
});
});
});
</script>

您需要确保您与fetch.php位于同一文件夹中,否则它会将POST发送到无效位置。您的选择器也有问题。改变

$("fetch").change(function()

$("select[name=fetch]").change(function()

我认为您可以将每个foreach循环直接插入表中。例如,如果您使用的是pdo

$db = new PDO('...');
foreach($result as $row){ 
$stmt = $db->prepare('INSERT INTO tbl SET name=?,batchno=?,address=?'); 
$stmt->execute([$row['name'], $row['batchno'], $row['address']]);
//...
}

最新更新