0

Hi, my problem is as follows.

My aim is when a user clicks 'comment' for a post, a <div> appears with the form, which I can get working just fine.

However as moving on with my development, now when a user clicks 'comment', parameters are passed to change the url to add on the id of the selected post so I can work with each post as a separate element. E.g.

?do=comment&id=2.

Now, because the page changes when 'comment' is clicked to to add the id accordingly, the hidden div never gets shown!

I can see it slightly appear but as soon as the website applies the parameter, it disappears again.

What I need help with is making this div show regardless, but I still need the paramters to be passed to the URL!

Using event.preventDefault(); shows the div, but does not pass the parameters.

Any help would be appriciated!

2
Contributors
8
Replies
43
Views
4 Years
Discussion Span
Last Post by pixelsoul
0

Well, the only way to alter the query string (unless you are applying an anchor) is to reload the page. If you are trying to show the form before the page is reloaded, then you are going to need to change around the order of things here. Or, you could have the form shown based on a paramter that you set in the query string after the page has reloaded maybe?

Edited by pixelsoul

0

Yeah that sounds right. So after I click comment for a post with a post_id of 3 for example, the url changes to www.website.co.uk/?do=comment&id=3 (which is currently not a problem). And then I would like the hidden form to be shown? Is this possible?

0

Yeah, check number 6 on this list of js/jQuery snippets. He has one there that grabs the query parameters in a query string. Then all you need to do is check if param=value exists, and then call your function.

http://www.designchemical.com/blog/index.php/jquery/8-useful-jquery-snippets-for-urls-querystrings/

var vars = [], hash;
    var q = document.URL.split('?')[1];
    if(q != undefined){
        q = q.split('&');
        for(var i = 0; i < q.length; i++){
            hash = q[i].split('=');
            vars.push(hash[1]);
            vars[hash[0]] = hash[1];
        }
}

You could create a function out of that and just call the function.

0

Thanks, I got that example working to alert out parameters, however I am not sure how to implement this to work for my problem? As to show the hidden div, I would not be able to have any paramters as the div will only show on the same page?

Edit: how about if I change the ?do=comment&id=2 to #do=comment&id=2 as this with the hash, it still shows the hidden div, and then use example no.3 from the website you provided to get the #paramters?

Edited by rp91

0

Well, the whole point of all of this was because you needed to modify the query string for some reason. If you are able to use a hash instead for your operation, then you don't need to reload the page at all.

0

I think the problem is confused, I do not want to modify the query string, I just cannot use my jquery script to show a hidden div when comment is clicked because clicking comment leads to a reload in page.

So basically, when I click comment this happens: /newsfeed.php changes to /newsfeed.php?do=comment&id=1.

And I would like my jquery script to show the hidden div after the reload as at the moment it literally flashes and disappears due to the reload

0

The page must be reloading when someone submits a comment? Okay, then use the first script to grab the parameters in the query string.

You should have a function to show the div that has your comment form. You would check the query string first onload to see if it has your paramter=value, and if true call your function to show the div. You would also call the same function if someone was to click on your comment button.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.