php - PHP Ajax - 只发送数据库中的第一个输入值....为什么?
问题描述
$sql = mysql_query('SELECT * FROM article')or die(mysql_error());
while ($data = mysql_fetch_array($sql))
{
echo "<td>";
echo '<div><strong>'.$data['nom_article'].'</strong></div>';
echo $data['prix'].' FCFA';
echo "<input type='text' value='".$data['id']."' id='annonce_id'>";
?>
<button type="submit" id="submit" value="<?php echo $data['id']; ?>" title="<?php echo $data['id']; ?>" style="background: #fff; border: none; cursor: pointer; color: blue;">
<a href="">Save</a>
</button>
<?php
echo "</td>";
}
<script>
$(document).on('click', '#submit', function(){
var t = $('annonce_id').val();
$.ajax({
url: "saveform.php",
data:{
done: 1,
text: t
},
success: function(data)
{
alert(t);
}
});
});
</script>
这是 saveform.php
<?php
if(isset($_GET['done']))
{
$text = mysql_escape_string($_GET['text']);
mysql_query("INSERT INTO test VALUES('', '".$text."')")or die(mysql_error());
exit();
}
?>
解决方案
It is OK now. Here is the solution that I concocted...Thank you @hassanrrs
<button type="submit" id="submit" name="<?php echo $data['id']; ?>" style="background: #fff; border: none; cursor: pointer; color: blue;">
<a href="">Save</a>
</button>
<script>
$(document).on('click', '#submit', function(){
var t = $(this).attr('name');
$.ajax({
url: "saveform.php",
data:{
done: 1,
texte: t
},
success: function(data)
{
//alert(t);
}
});
});
</script>
保存表格.php
if(isset($_GET['done']))
{
$text = mysql_escape_string($_GET['texte']);
mysql_query("INSERT INTO test VALUES('', '".$text."')")or die(mysql_error());
exit();
}
推荐阅读
- json - Amazon Athena:更改表以忽略格式错误的 json 错误
- html - 为什么这个 HTML 没有结束
- 标签?
- javascript - '无法读取未定义的属性'DataTable''将 Google 图表实施到我的 SAPUI5 项目中
- jq - 访问具有给定值的数组的祖先
- php - groovy - 连接到soap php webservice
- android - 了解 GeoJsonLayer 是否包含给定点
- apache - 在 Apache 中设置 CA 时浏览器不要求客户端证书
- ios - 我可以仅在没有关联域的应用中使用 iOS 自动填充功能吗?
- python - 为什么这个程序在 Python 2.7.6 中可以运行,但在 Python 3 中却不行?
- javascript - 缺少时通过 vue-cli-service build 构建 vue 项目。在文件路径中