Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-AJAX jQuery POST request

I am trying to use the jQuery POST function but it is handling the request in AJAX style. I mean it's not actually going to the page I am telling it to go.

$("#see_comments").click(function() {
    $.post(
        "comments.php", 
        {aid: imgnum}, 
        function (data) {

        }
    );
});

This function should go to comments.php page with the aid value in hand. It's posting fine but not redirecting to comments.php.


@Doug Neiner Clarification:

  1. I have 15 links (images). I click on a link and it loads my JavaScript. The script knows what imgnum I opened. This imgnum I want in the comments.php. I have to use this JavaScript and no other means can do the trick. The JavaScript is mandatory

  2. Your method successfully POSTs the aid value. But in the comments.php when I try to echo that value, it displays nothing.

  3. I am using Firebug. In the Console, it shows the echo REQUEST I made in Step (2) successfully.

like image 965
amit Avatar asked Jan 13 '10 05:01

amit


4 Answers

I know what you are trying to do, but its not what you want.

First, unless you are changing data on the server, don't use a POST request. Just have #see_comments be a normal <a href='/comments.php?aid=1'>...

If you have to use POST, then do this to get the page to follow your call:

$("#see_comments").click(function() {
  $('<form action="comments.php" method="POST">' + 
    '<input type="hidden" name="aid" value="' + imgnum + '">' +
    '</form>').submit();
});

How this would actually work.

First $.post is only an AJAX method and cannot be used to do a traditional form submit like you are describing. So, to be able to post a value and navigate to the new page, we need to simulate a form post.

So the flow is as follows:

  1. You click on the image, and your JS code gets the imgnum
  2. Next, someone clicks on #see_comments
  3. We create a temporary form with the imgnum value in it as a hidden field
  4. We submit that form, which posts the value and loads the comments.php page
  5. Your comments.php page will have access to the posted variable (i.e. in PHP it would be $_POST['aid'])
like image 188
Doug Neiner Avatar answered Nov 03 '22 00:11

Doug Neiner


$("#see_comments").click(function () {
    $('<form action="comments.php" method="POST"/>')
        .append($('<input type="hidden" name="aid">').val(imgnum))
        .appendTo($(document.body)) //it has to be added somewhere into the <body>
        .submit();
});
like image 22
devside Avatar answered Nov 03 '22 00:11

devside


While the solution by Doug Neiner is not only correct but also the most comprehensively explained one, it has one big problem: it seems to only work at Chrome.

I fidgeted around for a while trying to determine a workaround, and then stumbled upon the second answer by devside. The only difference is the extra code appendTo($(document.body)). Then I tested it in firefox and it worked like a charm. Apparently, Firefox and IE need to have the temporary form attached somewhere in the DOM Body.

I had to do this implementation for a Symfony2 project, since the path generator inside the .twig templates would only work with GET parameters and messing with the query string was breaking havoc with the security of the app. (BTW, if anyone knows a way to get .twig templates to call pages with POST parameters, please let me know in the comments).

like image 10
Mario Eduardo Castillo Avatar answered Nov 02 '22 23:11

Mario Eduardo Castillo


i think what you're asking is to get to 'comments.php' and posting aid with value imgnum. The only way to do this is to submit this value with a form.

However, you can make this form hidden, and submit it on an arbitrary click somewhere with jquery.

html necessary (put anywhere on page):

<form id='see_comments_form' action='comments.php' action='POST'>
    <input id='see_comments_aid' type='hidden' name='aid' value=''>
</form>

js necessary:

$("#see_comments").click(function(){
    $('#see_comments_aid').val(imgnum);
    $('#see_comments_form').submit();
);

this will redirect to 'comments.php' and send the proper value imgnum (that i assume you are getting from somewhere else).

like image 4
helloandre Avatar answered Nov 03 '22 01:11

helloandre