-2

At first here is my code:

HTML with Javascript:

function getValues(){
    var filter;
    $.ajax({
            type: "POST",
            url: "myphpfile.PHP?action=sek",
            data: "id=1",
            async: false,
            success: function(ret){
                    filter = ret;
                }
        });
    console.log(filter);

PHP myphpfile.php:

if(isset($_GET['action'])){
    print myfunction($_GET['action'],$_GET['id']);
}

My problem: When I try to print the result out with console.log, the whole myphpfile.php is printed. As I know, that means that no values reached the HTML file. But Why?

The function "myfunction" have results, i have tried it first. This function returns an array. ( I can't show this function here)

I don't get it what i'm doing wrong, Please have a look, It drive me crazy.

Thanks in advance!

TarangP
  • 2,711
  • 5
  • 20
  • 41
Melody
  • 182
  • 2
  • 12

3 Answers3

0

Change PHP File Request

if(isset($_GET['action'])){
    print myfunction($_GET['action'],$_GET['id']);
}

Also You can use $_REQUEST.

TarangP
  • 2,711
  • 5
  • 20
  • 41
0

First in Javascript:

function getValues(){
    var filter;
    $.ajax({
            type: "POST",
            url: "myphpfile.PHP?action=sek",
            data: { id: 1 },
            async: false,
            success: function(ret){
                    filter = ret;
                }
        });
    console.log(filter);

Afterthat you have to obtain id by $_POST['id']

Kancho Iliev
  • 701
  • 5
  • 13
0

action is in the $_GET super global and id is in the $_POST super global.

PHP:

if(isset($_GET['action'])){
    print myfunction($_GET['action'],$_POST['id']);
}

Also, you need to pass an object to your data parameter, not a string:

JS:

$.ajax({
    type: "POST",
    url: "myphpfile.PHP?action=sek",
    data: {id: "1"},
    async: false,
    success: function(ret){
            filter = ret;
        }
});
Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77