user8179727
user8179727

Reputation:

How to pass value from php get in url ajax?

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

Answers (3)

Harsh Panchal
Harsh Panchal

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

Sooraj J
Sooraj J

Reputation: 9

url: urlCall + '?' + $.param({"id": id}),

Upvotes: 0

Javier Arias
Javier Arias

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

Related Questions