Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Ajax Data Post Problem in PHP

Tags:

jquery

ajax

php

I am using jquery's AJAX in my project. Today, I used it somewhere else with all same themethods but it doesn't work.

Is there something wrong with my script?

HTML:

<a class='btn edit_receipe_btn' id='myreceipe-52'>Edit</a>

JQuery:

(Click function works. When I put alert(instance) after var instance line, it works)

$(document).ready(function(){
$('.edit_receipe_btn').click(function(){
   var instance = $(this).attr('id');
   var dataString = 'process=userReceipeEdit&instance='+instance;
   $.ajax({
    type: 'POST',
    url: 'ajax/ajaxs.php',
    data: dataString,
    cache: false,
    success: function(msg) {
        alert(msg);
    }
    });
});
});

PHP:

$prcs = $_POST['process'];
if($prcs=='userReceipeEdit'){
        $instance = $_POST['instance'];
        return $instance;
    }

It appears the problem is in the PHP. What am I doing wrong?

like image 741
aiternal Avatar asked Nov 20 '25 20:11

aiternal


2 Answers

Is that the entire PHP page? if so, you should echo instead of return

like image 92
Jasper De Bruijn Avatar answered Nov 23 '25 10:11

Jasper De Bruijn


As Jasper De Bruijn notified me, the problem was in my php script. I should use echo instead of return: Wrong usage:

$prcs = $_POST['process'];
if($prcs=='userReceipeEdit'){
        $instance = $_POST['instance'];
        return $instance;
    }

Correct usage:

$prcs = $_POST['process'];
if($prcs=='userReceipeEdit'){
        $instance = $_POST['instance'];
        echo $instance;
    }
like image 20
aiternal Avatar answered Nov 23 '25 09:11

aiternal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!