Problem sending jquery ajax data to PHP

I am using jQuery AJAX in my project. Today I used it somewhere else with all the same methods, but it does not work.

Is there something wrong with my script?

HTML:

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

JQuery

(The Click function works. When I put alert(instance)after the line var instance, 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;
    }

Looks like the problem is in PHP. What am I doing wrong?

+3
source share
4 answers

Is this the whole PHP page? if so, you should echo instead of returning

+3
source

As Jasper De Bruijn told me, the problem was in my PHP script. I should use echo instead of returning: Misuse:

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

Proper use:

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

, , , undefined.

:

$('.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);
    }
    });
});
0

, POST, , . JS:

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

Source: https://habr.com/ru/post/1780029/


All Articles