Reputation: 21
so I am trying to make a ajax post so I made a simple post:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(function(){
$.ajax({
type: "POST",
url: 'example1.php',
data: ({Imgname:"13"}),
success: function(data) {
//alert(data);
}
});
});
</script>
<?php
$temp = $_GET['Imgname'];
echo $temp;
?>
</body>
</html>
But it I don't get anything, what am I doing wrong? please help.
Upvotes: 0
Views: 1098
Reputation: 24001
data: ({Imgname:"13"}),
should be data: {Imgname:"13"},
The default type
of ajax
is get
..But while you use type: "POST",
use $_POST['Imgname'];
instead of $_GET['Imgname'];
If console.log(data);
after changes still returns an error so check the path of url: 'example1.php',
If the ajax
and the php
code in the same file so use url: '/',
and put the php code on top of the html code and in php after echo return false;
Upvotes: 1
Reputation: 939
Try this, it should must work for your case: Here is your HTML Code:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(function () {
$.ajax({
type: "POST",
url: 'example1.php',
data: {Imgname: "13"},
success: function (result) {
alert(result);
}
});
});
</script>
</body>
</html>
INSIDE your example1.php File you have to check with:
<?php
$temp = $_POST['Imgname'];
echo $temp; exit;
?>
Your mistake was, you was sending the POST request and you was trying to get data in $_GET variable. Another mistake with your data posted as well. Try my code now.
Upvotes: 0