views:

31

answers:

2

I am trying to send parameters that I get from a table in my jsp to other JSP using ajax. I am using the followinf function to send all values to JSP: ajaxForm but I don't know why the send failed every time I run it:

Here is the javascript function:

function editarow() {
    var xhr = getXhr();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            selects = xhr.responseText;
            // On se sert de innerHTML pour rajouter les options a la liste
            document.getElementById('prjsel').innerHTML = selects;
        }
    };

    var row, firstNameCell, lastNameCell;
    var table = document.getElementById("sheet");
    var buttons = table.getElementsByTagName("button");
    for (var i = 0; i < buttons.length; i++) {
        if (buttons[i].name == "edit") {
            buttons[i].onclick = function() {
                row = this.parentNode.parentNode;

                // The first name cell is the first child
                NameCell1 = findElement(row.firstChild);
                NameCell2 = findElement(NameCell1.nextSibling);
                NameCell3 = findElement(NameCell2.nextSibling);
                NameCell4 = findElement(NameCell3.nextSibling);
                NameCell5 = findElement(NameCell4.nextSibling);
                NameCell6 = findElement(NameCell5.nextSibling);
                NameCell7 = findElement(NameCell6.nextSibling);

                // `innerHTML` pour obtenir la valeur
                /*alert("name 1  is " + NameCell1.innerHTML);
                alert("name 2  is " + NameCell2.innerHTML);
                alert("name 3  is " + NameCell3.innerHTML);
                alert("name 4  is " + NameCell4.innerHTML);
                alert("name 5  is " + NameCell5.innerHTML);
                alert("name 6  is " + NameCell6.innerHTML);
                alert("name 7 is " + NameCell7.innerHTML);*/

            }
        }
    }

    xhr.open("POST", "ajaxForm.jsp", true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.send("NameCell1="+NameCell1,"NameCell2="+NameCell2,"NameCell3="+NameCell3,"NameCell4="+NameCell4,"NameCell5="+NameCell5,"NameCell6="+NameCell6,"NameCell7="+NameCell7 );
}

After I get the value from the table I want to send all of them to the ajaxForm.jsp.

+2  A: 

From the last line:

xhr.send("NameCell1="+NameCell1,"NameCell2="+NameCell2,"NameCell3="+NameCell3,"NameCell4="+NameCell4,"NameCell5="+NameCell5,"NameCell6="+NameCell6,"NameCell7="+NameCell7 );

This isn't the way to concatenate a String in JavaScript.

Since you're using JSP, you should know Java as well. You should concatenate the String in JavaScript the same way as you would do in Java:

xhr.send("NameCell1=" + NameCell1 + ",NameCell2=" + NameCell2 + "etc...");

That said, this should however have errored in the JavaScript console. Did you pay attention to this? Anyway, for better JavaScript debugging I suggest you to grab Firebug and for less verbose/opaque and more crossbrowser compatible Ajax handling and HTML DOM traversal, I strongly recommend you to have a look at jQuery. With jQuery and the Ajax Form Plugin you would have been ready with only the following lines:

$(document).ready(function() {
    $('#formId').ajaxForm(function(response) {
        $('#prjsel').html(response);
    });
});

This way you don't need to worry about browser specific details and how to send the request properly.

BalusC
A: 

The send method only takes one argument. You are failing to convert your data so it is a application/x-www-form-urlencoded string.

i.e. a set of keys and values (where the keys and values are all processed with encodeURIComponent separated by ampersands.

David Dorward