Non AJAX JQuery POST Request

I am trying to use the jQuery POST function, but it processes the request in AJAX style. I mean, this doesn’t actually go to the page, I say it to go.

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

This function should go to the comments.php page with the help value in hand. This sends well, but does not redirect to comments.php.




@ Doug Neyner Specification:

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

  2. Your method successfully sends the help value. But in comments.php, when I try to display this value, nothing is displayed.

  3. I am using Firebug. The console displays an echo request, which I successfully completed in step (2).

+74
javascript jquery
Jan 13 '10 at 5:45
source share
6 answers

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

First, if you do not modify the data on the server, do not use the POST request. Just #see_comments will be normal <a href='/comments.php?aid=1'>...

If you need to use POST , then do this so that the page matches your call:

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

How it really works.

The first $.post is just an AJAX method and cannot be used to execute the traditional form submit as you describe. Thus, in order to be able to publish the value and go to a new page, we need to simulate a form post.

So the stream is as follows:

  • You click on the image and your JS code gets imgnum
  • Then someone clicks #see_comments
  • We create a temporary form with an imgnum value in it as a hidden field
  • We submit this form, which submits the value and loads the comments.php page
  • Your comments.php page will have access to the published variable (i.e. in PHP it will be $_POST['aid'] )
+127
Jan 13 '10 at 5:54
source share
 $("#see_comments").click(function () { $('<form action="comments.php" method="POST"/>') .append($('<input type="hidden" name="aid" value="' + imgnum + '">')) .appendTo($(document.body)) //it has to be added somewhere into the <body> .submit(); }); 
+46
Sep 17 '12 at 17:27
source share

Although the Doug Neiner solution is not only correct, but also the most fully explained, it has one big problem: it only works in Chrome.

I fidgeted for a while, trying to figure out a workaround, and then stumbled upon nopnop77's second answer. The only difference is the appendTo($(document.body)) extra code appendTo($(document.body)) . Then I tested it in firefox and it worked like a charm. Apparently, Firefox and IE should have a temporary form attached somewhere in the body of the DOM.

I had to execute this implementation for the Symfony2 project, since the path generator inside the .twig templates worked only with GET parameters, and the mess with the query string distorted the chaos with application security. (By the way, if someone knows a way to get .twig templates for calling pages with POST parameters, please let me know in the comments).

+10
Oct 22 '13 at 16:44
source share

I think what you're asking is to go to 'comments.php' and post aid with the value imgnum . The only way to do this is to submit this value in the form.

However, you can make this form hidden and send it by arbitrary clicking somewhere with jquery.

html needed (put anywhere on the page):

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

js needed:

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

this will redirect to 'comments.php' and send the correct imgnum value (which I assume you are getting from somewhere else).

+3
Jan 13 '10 at 6:09
source share

In fact, $.post() sends some data to the server. This does not cause redirects unless you do this in your server-side code that processes the POST request. I can offer two solutions:

  • To go to the comment page, instead of using a jQuery message, you can simply use the anchor tag - <a href="comments.php?aid=1">Show Comments</a> .
  • Or, if you want to go through jQuery, you can use this piece of code: $(location).attr("href", "comments.php?aid=1");
0
Jan 13 '10 at 6:06
source share

didn't solve the problem. but managed to get around this. I had to make a lot of changes for JS to do this work, but the main problem of this issue was resolved by doing the following:

  $("#see_comments").attr({href: "comments.php?aid='"+imgnum+"'"}); 

this added the help value to the url, as @Doug Neiner originally offered me. Many thanks to Doug for all the efforts. I really appreciate it. +1 and accept your answer for your efforts.

-one
Jan 13 '10 at 19:30
source share



All Articles