Reputation:
I´m trying to pass value from php to ajax url
This is my code:
<?php
$id = $_GET['id'];
echo id;
?>
Code for javascript
$.ajax({
url:"http://localhost:3000/product/"+'<?$id; ?>',
type: "POST",
dataType: 'json',
crossDomain: "true",
success: function (result) {
if (result.type == false) {
alert("Error occured:" + result.data);
return false;
}
$.each(JSON.parse(result.data),function(index,obj){
console.log(obj.id);
I´m getting error in http://localhost:3000/product/%3C?=$id;%20?%
Upvotes: 0
Views: 2770
Reputation: 308
Try This :
<script>
var getid = '<?php echo $_GET['id'] ?>';
$.ajax({
url:"http://localhost:3000/product?id="+getid',
type: "POST",
dataType: 'json',
crossDomain: "true",
success: function (result) {
if (result.type == false) {
alert("Error occured:" + result.data);
return false;
}
$.each(JSON.parse(result.data),function(index,obj){
console.log(obj.id);
});
</script>
Upvotes: 0
Reputation: 2488
I think the problem is in the name of the parameter, try:
url:"http://localhost:3000/product?id=" + id',
If you want to get the id from php do:
id = <?php echo $id; ?>;
Upvotes: 1