5

i have 1 php file name index.php. in that file i want to pass one variable from ajax to 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

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

6
  • Does the isset test pass? Is it set but just to an empty string? Commented Mar 19, 2013 at 9:41
  • From where on the page is this javascript executed? For example, is it inside the <head> tag, or at the bottom, near </body>? Commented Mar 19, 2013 at 9:47
  • Try making data a hash rather than a query string. Commented Mar 19, 2013 at 10:03
  • @richard: isset test is not pass Commented Mar 20, 2013 at 0:55
  • @sverri: is in tag body Commented Mar 20, 2013 at 0:56

4 Answers 4

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

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

Comments

1

set this as json object:

data: {action: elem}

and don't mix jquery with pure 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.

You can also do:

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

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

Comments

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

2 Comments

hmm?i don't get it,, if you mean url: "some.php" and not window.location.href, is not make any different
data: 'action='+elem, this is the wrong one, are you not getting answer or unable to understand?
0
if (!empty($_POST['action']))
{   
echo $_POST['action'];
}

Try this code...

1 Comment

but $_POST['action'] is undefined, so empty test will not pass

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.