views:

410

answers:

5

i am trying to use the jquery POST function but it is handling the request in AJAX style. i mean its 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. its 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 JS knows wat 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 JS 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.

+4  A: 

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'])
Doug Neiner
the aid value is coming from the DB. so i would really like the jquery thing to work. as i need my Javascript too.
amit
@amit, in the code you post, the `aid` value is not coming from the database it is being *sent* to the AJAX request. Have you not posted your whole function?
Doug Neiner
the imgnum value is from db.
amit
@amit, then your question is not clear. My code POST's the imgnum to the `comments.php` file AND redirects at the same time. What else are you wanting it to do?
Doug Neiner
@amit, I am not disagreeing with you where `imgnum` comes from, its just that my answer parallels your code exactly except it with the redirect. Just try swapping out the `$('form... .submit();` call with your `$.post` and see if it doesn't do exactly what you want.
Doug Neiner
it redirects to comments.php. but it does not echo the POSTed value. but it shows the echo response in firebug console.
amit
@amit. I really think I can help you with this, but you need to provide more info in your question (please edit it to add). We need to know what steps you want, and the expected result. i.e.: "1. The id is submitted to `comments.php`, 2. I expect the returned value to display like this..."
Doug Neiner
@amit, thanks for the clarification. Based on what you provided, my updated answer would not have worked for you. I am going to provide explanation around how my original answer works so maybe it makes more sense.
Doug Neiner
@amit Ok, I have posted an explanation with a walk through. Am I missing anything you need to have happen?
Doug Neiner
sorry but does nt work.
amit
this function makes the link unclickable. i mean when i click on the link nothing happens.
amit
OK, I'll take a look at it put together a test. Maybe I left something out.
Doug Neiner
A: 

Actually, $.post() sends some data to the server. It does not cause any redirection unless you do it in your server side code which handles the POST request. I can suggest two solutions:

  1. To go to comment page, instead of using JQuery post, you can simply use a 'anchor' tag - <a href="comments.php?aid=1">Show Comments</a>.
  2. Or if you are want to go through JQuery, you can use this code snippet: $(location).attr("href", "comments.php?aid=1");
Veera
A: 

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).

contagious
A: 

didnt exactly solve the problem. but did manage to work around it. i had to do a lot modification to the JS to make this work, but the core problem of this question was solved by doing this:

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

this appended the aid value to the URL as @Doug Neiner initially suggested me to do. Thanks a lot Doug for all the effort. I really appreciate. +1 and accept to your answer for the effort.

amit