我有一些运行良好的代码,但仅从数据库发送了第一条记录。问题是如何按照select语句指示一次发送所有记录。 我已经尝试遵循Sending multiple objects with node.js, mysql and socket.io和send data from phone to server,但是它们的实现并不能真正解决我的问题,因为我希望发布使用该帖子定义的记录以进行序列化和跟踪。
Index.html
<script type="text/javascript">
function sendAll() {
db.transaction(function(tx) {
Squery = 'SELECT * FROM details';
tx.executeSql(Squery,
null,
function(tx, results)
{
for(i=0; i<results.rows. length; i++){
row = results.rows.item(0);
var name = row['name'];
var age = row['age'];
var dataString = "name=" + name + "&age=" + age +"&sendAll=";
if ($.trim(name).length > 0 & $.trim(age).length) {
$.ajax({ type: "POST",
url: "http://my_location/send.php",
data: dataString,
crossDomain: true,
cache: false,
success: function(data) {
if (data == "success") {
alert("Record sent");
$("#sendAll").val('submit');
} else if (data == "error") {
alert("error");
}
}
});
}
return false;
}});
});
}
</script>
<form id="sendAll" method="POST" data-ajax="false" onsubmit="return false;" class="formular">
<button class="button button-block" onClick="sendAll()" data-inline="true">Send</button>
</form>
send.php
include "db.php";
if(isset($_POST['sendAll']))
{
$name=$_POST['name'];
$age=$_POST['age'];
$price=$_POST['price'];
$q=mysqli_query($con,"INSERT INTO `details` (`name`,`age`) VALUES ('$name','$age')");
if($q)
echo "success";
else
echo "error";
}