将变量从ajax传递到php

时间:2022-05-03 12:17:22

i have 1 php file name index.php. in that file i want to pass one variable from ajax to php.

我有一个php文件名index。php。在该文件中,我希望将一个变量从ajax传递到php。

var elem = document.getElementById("mydiv").value;
(function($)
{
    $(document).ready(function()
    {   
        $.ajax(
        {   
            type:"POST",
            url: window.location.href,
            data: 'action='+elem,
            beforeSend: function() {
                $('#cctv').hide();
            },
            complete: function() {
                $('#cctv').show();
            },
            success: function() {
                $('#cctv').show();
            }
        });
        var $container = $("body");
        $container.load('findAllPath.php',{'function': 'findAllPath'});
        var refreshId = setInterval(function()
        {
            $container.load('findAllPath.php',{'function': 'findAllPath'});
        }, 10000);
    });
})(jQuery); 

and my php

和我的php

if (isset ($_POST['action']))
{   
    echo $_POST['action'];
}

in firebug, i can see that ajax already post 'action=value' but in php, $_POST['action'] is empty. anyone can help me, what's wrong with my code? thank you

在firebug中,我可以看到ajax已经发布了'action=value',但是在php中,$_POST['action']是空的。任何人都可以帮助我,我的代码有什么问题?谢谢你!

4 个解决方案

#1


4  

Try using data like this

尝试使用这样的数据。

data: {action: elem},

For Example

例如

$.ajax({
   type: "POST",
   url: "some.php",
   data: { name: "John", action: "save" }
   }).done(function( msg ) {
  alert( "Data Saved: " + msg );
});

printr whole $_REQUEST and check if your are getting action

打印整个$_REQUEST并检查是否得到了操作

#2


1  

set this as json object:

将其设置为json对象:

data: {action: elem}

and don't mix jquery with pure js.

不要把jquery和纯js混在一起。

var elem = $('#myDiv').val();

and as friend above say. There can be situation where js is trying to get value property before whole page is loaded. You should do everything in document ready block.

正如上面的朋友所说。在加载整个页面之前,可能会出现js试图获取值属性的情况。您应该在文档就绪块中执行所有操作。

You can also do:

你也可以做的事:

data: {
action: $('#myDiv').val()
}

You will be sure then that you have the newest data on the form!

您将确定您已经有了表单上的最新数据!

#3


1  

Look in to this and find what you have missed

看看这个,看看你错过了什么。

    $.ajax({
        type: "POST",
        url: "some.php",
        data: { action: elem}
        beforeSend: function() {
            $('#cctv').hide();
        },
        complete: function() {
            $('#cctv').show();
        },
        success: function() {
            $('#cctv').show();
        }
    });
    var $container = $("body");
    $container.load('findAllPath.php',{'function': 'findAllPath'});
    var refreshId = setInterval(function()
    {
        $container.load('findAllPath.php',{'function': 'findAllPath'});
    }, 10000);
});

#4


0  

if (!empty($_POST['action']))
{   
echo $_POST['action'];
}

Try this code...

试试这个代码…

#1


4  

Try using data like this

尝试使用这样的数据。

data: {action: elem},

For Example

例如

$.ajax({
   type: "POST",
   url: "some.php",
   data: { name: "John", action: "save" }
   }).done(function( msg ) {
  alert( "Data Saved: " + msg );
});

printr whole $_REQUEST and check if your are getting action

打印整个$_REQUEST并检查是否得到了操作

#2


1  

set this as json object:

将其设置为json对象:

data: {action: elem}

and don't mix jquery with pure js.

不要把jquery和纯js混在一起。

var elem = $('#myDiv').val();

and as friend above say. There can be situation where js is trying to get value property before whole page is loaded. You should do everything in document ready block.

正如上面的朋友所说。在加载整个页面之前,可能会出现js试图获取值属性的情况。您应该在文档就绪块中执行所有操作。

You can also do:

你也可以做的事:

data: {
action: $('#myDiv').val()
}

You will be sure then that you have the newest data on the form!

您将确定您已经有了表单上的最新数据!

#3


1  

Look in to this and find what you have missed

看看这个,看看你错过了什么。

    $.ajax({
        type: "POST",
        url: "some.php",
        data: { action: elem}
        beforeSend: function() {
            $('#cctv').hide();
        },
        complete: function() {
            $('#cctv').show();
        },
        success: function() {
            $('#cctv').show();
        }
    });
    var $container = $("body");
    $container.load('findAllPath.php',{'function': 'findAllPath'});
    var refreshId = setInterval(function()
    {
        $container.load('findAllPath.php',{'function': 'findAllPath'});
    }, 10000);
});

#4


0  

if (!empty($_POST['action']))
{   
echo $_POST['action'];
}

Try this code...

试试这个代码…