0

I am having some issues with the jQuery Ajax function and PHP.

I am checking the existance of the nav key in the $_REQUEST variable in PHP with code such as this:

if ($_REQUEST['nav']) {
    // do something
} else {
    echo 'Please specify NAV.';
}

However the above expression never evaluates as nav is never passed to it and always outputs 'Please specify NAV.'

console.log('paramList: ' + paramList);

$.ajax({
    type: 'POST',
    url: '/admin/nav_builder/edit.php?act=save&nav_id=<?php echo $nav_id; ?>',
    data: {'nav':paramList},
    dataType: 'json',
    error: function(xhr, err) {                             
        loadLayout();
        hideLoader();
        hideLoaderPalette();
    },
    success: function(data){
        $('.errorMsg').html(data.html);
        hideLoader();
        hideLoaderPalette();
    }
});

Using the Firefox Firebug plugin I can see that paramList does indeed hold a value, this is:

paramList: {"section0":{"elem0":{"nav_palette":"text","nav_name":"fdgfdgdfg","nav_url":""},"elem1":{"nav_palette":"category","c_id":"226"}}}

I can't see for the life of my why nav is not being passed to the URL provided to the ajax function.

5
  • 2
    Have you tried dumping the $_REQUEST on the PHP side to see what's actually being populated? Commented May 29, 2012 at 14:49
  • Is there request in Firebug->Net->XHR? Can you dump it? Commented May 29, 2012 at 14:50
  • Does it work with $_POST['nav'] instead of $_REQUEST['nav']? Commented May 29, 2012 at 14:51
  • @AlessandroPezzato No, it doesn't work with $_POST, $_GET or $_REQUEST Commented May 29, 2012 at 15:05
  • Why are you specifying a POST? Does the server require a POST instead of a GET? Commented May 29, 2012 at 15:23

2 Answers 2

1

Try to print first if the "nav" parameter if it has a value.

echo $_REQUEST['nav'];

The other one is that you have used two method in one request.

Just try the following.

$.ajax({
    type: 'POST',
    url: '/admin/nav_builder/edit.php',
    data: {act:'save', nav_id:'<?php echo $nav_id; ?>', nav:paramList},
    dataType: 'json',
    error: function(xhr, err) {                             
        loadLayout();
        hideLoader();
        hideLoaderPalette();
    },
    success: function(data){
        $('.errorMsg').html(data.html);
        hideLoader();
        hideLoaderPalette();
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try $_POST instead. As you're posting the nav contents.

3 Comments

What is the difference? $_REQUEST - An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.
$_REQUEST is the combination of $_POST and $_GET and $_COOKIE.
I'd worry that the querystring built up above could contain something spurious which could poison the well (e.g. I'm not sure what'd happen if in the nav_id there was an ampersand then nav). If you're using $_POST, you KNOW it's a post value you're getting and not anything from a different/subverted source.

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.