我有一个函数,可以将数据发送到php文件。尽管我得到一个显示正确ID和成功消息的警报,但当我尝试在我的process.php中回显它时,没有显示任何内容。 。函数在adopt.php中,是指process.php:
的文件 <script type="text/javascript">
$(function(){
$('#loginform').submit(function(e){
return false;
});
<?php
$q = pg_query($conn, "SELECT * FROM caini_donati");
while($res = pg_fetch_assoc($q)){ ?>
$('#<?php echo $res['id_caine']; ?>').leanModal({ top: 110, overlay: 0.45, closeButton: ".hidemodal" });
$("#<?php echo $res['id_caine']?>").click(function(event) {
var id_caine = event.currentTarget.id;
alert(id_caine);
$.ajax({
type: 'POST',
url: 'process.php',
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
data: { id_caine : id_caine},
success: function(data){
alert("succes"),
console.log(data);
}, error: function(xhr, ajaxOptions, thrownError) {alert ("Error:" + xhr.responseText + " - " + thrownError );}
});
});
<?php } ?>
});
</script>
in process.php i have:
<?php
$var = $_POST['id_caine'];
echo "tr1: ".$_POST['id_caine'];
echo "try: ".$var;
?>
有谁知道我做错了什么?在process.php中,我想基于'id_caine'变量运行更新查询。任何ideea都是apreciated ..不重要,但我对ajax一无所知。
答案 0 :(得分:1)
RiggsFolly是正确的,你错过了javascript html标签,我已经调整了你的代码,告诉你应该在哪里。我也纠正了成功方法,以便你实际上有一个变量可以使用。您将rs定义为参数,但后来尝试使用“数据”。
<?php
$q = pg_query($conn, "SELECT * FROM caini_donati");
while($res = pg_fetch_assoc($q)){ ?>
<script type="text/javascript">
$('#<?php echo $res['id_caine']; ?>').leanModal({ top: 110, overlay: 0.45, closeButton: ".hidemodal" });
$("#<?php echo $res['id_caine']?>").click(function(event) {
var id_caine = event.currentTarget.id;
alert(id_caine);
$.ajax({
url: 'process.php',
type: 'POST',
data: { id_caine : id_caine},
dataType: 'html',
success: function(data){
alert("succes"),
console.log(data);
}, error: function(xhr, ajaxOptions, thrownError) {alert ("Error:" + xhr.responseText + " - " + thrownError );}
});
});
</script>
<?php } ?>