tags:

views:

60

answers:

2

Hi, this is my first time to post here. I have a problem on my jquery script. My goal is, I need to send values of a certain link to another page, so that the other page can use the value. I think using ajax post can do the job but I cant get any result. Here is my code:

$(function() {
    var myLink = $('#link');

    $('.class').click(function() {
        var cell1 = $(this).closest('tr').find('td:eq(4)').text();
        var cell2 = $(this).closest('tr').find('td:eq(3)').text();
        var cell3 = $(this).closest('tr').find('td:eq(2)').text();

        var data = 'cell1=' + cell1 + '&cell2=' + cell2 + 'cell3=' + cell3;

    });
});

So I want to know how can I send those value to another page just by clicking the table cell using ajax post. I want to display open it to a new window.

I hope you can help me with this, thank you very much in advance. ;)

A: 

If you are posting the values you will have to store them serverside till you reach the other page, another way of doing it would be like this (assuming #link is the link)

$(function() {
    var myLink = $('#link');

    $('.class').click(function() {
        var cell1 = $(this).closest('tr').find('td:eq(4)').text();
        var cell2 = $(this).closest('tr').find('td:eq(3)').text();
        var cell3 = $(this).closest('tr').find('td:eq(2)').text();

        var data = 'cell1=' + cell1 + '&cell2=' + cell2 + 'cell3=' + cell3;
        var baseUrl = "some.page.php" //could be anything
        myLink.attr('href',baseUrl+"?"+data).click();
    });
});

Now, in order to open it in a new window, you could use the old target="_blank" (on #link solution or look up one of the other more proper javascript ways

Kristoffer S Hansen
What do you mean by "store them serverside"? Do I need to have another serverside page to store the value then I will go to my target or the "baseUrl" is the target page? Im sorry I got a little bit confused.
madzman23
baseURL will be the target page here, this way you wont have to store the information server side till your user reaches the final page, the information will instead be in the URL
Kristoffer S Hansen
why am I getting "url/object%20object?
madzman23
it shouldnt be with the code I supplied, can you update your question with your new code?
Kristoffer S Hansen
well i used your code but it didnt work.. it didn't go to the baseUrl? what do you think of the problem?
madzman23
I think this was the right answer for my question. I just changed the selector and remove the ".click()" function after the attr statement.
madzman23
A: 

Data in URLs must be properly escaped. I suggest you store it in an object and let jQuery take care of the dirty details:

// Untested
var tr = $(this).closest('tr');
var data = {
    cell1: tr.find('td:eq(4)').text(),
    cell2: tr.find('td:eq(3)').text(),
    cell3: tr.find('td:eq(2)').text()
};

Then, pick your favourite AJAX function and feed it with your object.

Update

$('.class').click(function() {
    var tr = $(this).closest('tr');
    var url = 'other-page.html';
    var data = {
        cell1: tr.find('td:eq(4)').text(),
        cell2: tr.find('td:eq(3)').text(),
        cell3: tr.find('td:eq(2)').text()
    };

    $("#load-stuff-here").load(url, data);
});
Álvaro G. Vicario
Actually I really dont have a problem on the calling of data, I do try it and I got the value I needed. My only problem is on how will I pass the value to another page. I just need to pass it using ajax post, but I am not that familiar with ajax function. Can you please give me a working example on how to send values using ajax post. Thank you very much.
madzman23
@madzman23, see the update
Álvaro G. Vicario
BTW, lack of proper escaping is never an issue when test data does not need to be escaped :)
Álvaro G. Vicario
hi, alvaro.. thank you for the response and it works but it displays on the same page. what I want to do is to pass it to another page not on a div on my current page. I am also thinking to display it using thickbox. So I need to pass the value to another page then the thickbox will show that page. Do you think we can do that? Thank you again. ;)
madzman23
@madzman23 Then, why use AJAX at all? I don't get it.
Álvaro G. Vicario
@alvaro hi, yes i thought I can use the .post function to send the values to another page then display the page on the thickbox(modal)..
madzman23