最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How do I send values between pages using javascript? - Stack Overflow

programmeradmin8浏览0评论

I am currently using a javascript code to make an entire row in my table clickable. Once the row is clicked a function is ran, I am wondering what I can use to redirect to a PHP page and preferably send a post variable along with it. My guess is AJAX but I am not sure how or if it would work.

Javascript

function DoNav(theUrl) {
    document.location.href = theUrl;
};

HTML

<tr onclick="DoNav('myphpscript.php');">

I have access to jQuery so that is also an option. Any help is appreciated, Thanks!

I am currently using a javascript code to make an entire row in my table clickable. Once the row is clicked a function is ran, I am wondering what I can use to redirect to a PHP page and preferably send a post variable along with it. My guess is AJAX but I am not sure how or if it would work.

Javascript

function DoNav(theUrl) {
    document.location.href = theUrl;
};

HTML

<tr onclick="DoNav('myphpscript.php');">

I have access to jQuery so that is also an option. Any help is appreciated, Thanks!

Share Improve this question edited Jan 24, 2020 at 6:08 ankitkanojia 3,1224 gold badges24 silver badges37 bronze badges asked Apr 20, 2009 at 14:33 Chris BierChris Bier 14.5k19 gold badges69 silver badges106 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 4

If you need to POST the data (not use GET), One easy option is to create a form element on the fly, attach input elements with the values you need and submit it. You can do that like so if you use jQuery:

$(function() { 
    $('tr').click(function() {
        var mail_id = /* get the mail id of this row... not sure where since I dont' have the HTML */
        $('body').append('<form method="post" action="myphpscript.php" id="donavform" style="display:none;"></form>');
        $('#donavform').append('<input type="hidden" name="mid" value="'+mail_id+'" />');
        $('#donavform').submit();
    });
});

Hope that makes sense. If not, let me know! It's, okay...

Explanation: The very first line is a jQuery shortcut way of saying "when the document is done loading..." So, when the page is done loading, I'm going to attach an event listener to all elements in the document. When one of those elements is clicked, we can then extract the mail id (and whatever else you need) that is in relation to that particular table row. So, if you had HTML like this:

<!-- 8435 is the mail ID in this example. -->
<tr id="row3">8435</tr>

Then we could extract the mail_id variable like so:

var mail_id = $(this).html();

Now, we are going to attach a hidden form element to the end of the body of the HTML (it doesn't really matter where we put it since it is hidden... so, the end is fine). In this form element, we set the method to POST and the action to whatever php file you need to POST to. I also set an ID so it's easily referred to in the next step.

I'm now going to select the newly-created form element using its ID and I'm going to append a new hidden input element to it with the appropriate name value pair.

$('#donavform').append('<input type="hidden" name="mid" value="'+mail_id+'" />');

Finally, I'm going to use the jQuery JavaScript submit method to trigger the submit event on the form. This is basically equivalent to pressing the 'submit' button on a normal form.

Try it out, it should work flawlessly.

If you're going to a new page, just submit the form as usual. Put the data in form fields (hidden if required). No need to Ajax, jQuery or any other magic unless you want to stay on the same page and post in the background.

If the amount of data is not ridiculously large, use a query string...

<tr onclick="DoNav('myphpscript.php?key=value');">

Or if you need a natural HTTP post, you can programmatically submit the form with Javascript...

onclick="document.forms[0].submit();"

You could send the data along in a cookie. There's a nice jQuery plugin that helps with setting cookies in the jQuery namespace.

http://plugins.jquery./project/cookie

发布评论

评论列表(0)

  1. 暂无评论