开发者

Jquery Ajax Data Post Problem in 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?


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


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;
    }


Two things, output the error and check to make sure instance is not undefined.

Output the error like this:

$('.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);
    },
    error: function(response, status, error)
    { 
        alert(response.responseText);
        alert(status);
        alert(error);
    }
    });
});


I think you have formed this POST like a get, not sure why. Try doing this in JS:

var dataString = 
    {
        "process" : "userReceipeEdit",
        "instance" : instance
    };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜