I have a function that is supossed to send data to a php file..although i get an alert that shows the correct ID and a success message, when trying to echo it out in my process.php, nothing is showed..the function is in adopt.php, the file that refers to 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;
?>
Does anyone know what I'm doing wrong? In process.php i want to run an update query based on the 'id_caine' variable..Any ideea is apreciated..Not important but I have altmost no knowledge about ajax.
RiggsFolly is correct, you are missing the javascript html tags, i have tweaked your code to show you where they should be. Also i corrected the success method so that you actually had a variable to work with. You defined rs as the parameter but then tried to use "data" instead.
<?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 } ?>