3

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?

13
  • You do have $prcs = $_POST['process']; in your PHP as well, yes? Commented Dec 13, 2010 at 15:25
  • Yeah, I just added it to my post. It was there from the beginning in my code. Commented Dec 13, 2010 at 15:26
  • if you take out that if $prcs statement can you hit the page directly in a browser? Commented Dec 13, 2010 at 15:28
  • print_r($_POST); and show us what PHP is receiving. Commented Dec 13, 2010 at 15:28
  • 1
    Please don't put 'solved' in the title. If something someone else wrote solved your problem, please upvote and accept their answer. Commented Dec 13, 2010 at 16:04

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.