views:

808

answers:

4

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.

My Javascript Function:

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

My HTML:

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

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

+3  A: 

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.

Diodeus
I understand, but I need to make it so that way when they click on the whole tr it submits the form. I also need to send the "mid" (id number) of the piece of mail they click on so i can load it in the next page.
Chris B.
@Chris: use form.submit() for that. (Note that's not a jQuery method)
JPot
+2  A: 

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();"
Josh Stodola
I have PHP post vars in the URL disabled for security purposes. So I need to send it as a form.
Chris B.
You are assuming he already has a Form element in his DOM. He doesn't suggest that he does. Just because you click on a table row doesn't mean it's in a form and that the values he needs are in there. He likely needs some value related to the row he clicked on.
KyleFarris
Listen. Don't critique my answer -- find something better to do. I'm not here to "steal your thunder". I tried to help him. That's all.
Josh Stodola
Since i have PHP post vars disabled via the url, i used GET instead. This works great. Thank you!
Chris B.
+4  A: 

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.

KyleFarris
I am not sure what you mean, but it seems like a good solution. Could you please explain it a bit more? Thanks!!
Chris B.
A: 

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.com/project/cookie

Joey Robert
Interesting, do you know how I might be able to pass over a variable from my PHP script to the jQuery so I can set it as a cookie?
Chris B.
I would suggest setting it directly from PHP using the setcookie() function, http://php.net/setcookie. You should be able to access it later through jQuery. If that's not an option, you might have to print out the variable and let jQuery handle it.
Joey Robert
The only problem here is that in certain PHP.ini setups, you may not be able to alter cookies accessible to PHP using javascript. This is for security purposes. Seeing as he's disabling GET, I would imagine he wouldn't want to post vars into cookies either. It's generally bad practice to do this unless you need the cookie to be accessible through multiple pages and specifically by javascript and not PHP.
KyleFarris