0

For example in form, I've 2 or more element that have the same name like in the below code:

<form name="form1" method="post" action="saveToDb.jsp">
    <span id="feedBackList">
    <table>
        <thead>
            <tr>
                <td>column1</td>
                <td>column2</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type=text id=field1 name=field value="firstvalueTosavetoDb"></td>
                <td><input type text id="field2 name=field value="secondvalueTosavetoDb"></td>
            </tr>
        </tbody>
    </table>
    </span>
</form>

Value for this two field I want to capture and save it to database. This can be done by submit the form, and in my saveToDb.jsp file I just need to get the value by using

String[] value = request.getParameterValues("field"); 

loop through the array and save it to database. the problem is I don't want to submit the form because the page will be refresh. what I'm trying to achieved is, I want to use jQuery.get method and pass the value and view back the value without refresh the page and put it in my <span> tag like below code

    var $p = jQuery.noConflict(){
    function submitAndView(){
        //here is where i should get the value
        var span = document.getElementById("feedBackList");
        $p.get("saveToDb",{pass the value here}, function(data){
            span.innerHTML = data
    });

}

the question is what is the equivalent to request.getParameterValues in JavaScipt or jQuery. p/s: the number of <input> elements is not fixed.

pb2q
  • 58,613
  • 19
  • 146
  • 147
Zahary
  • 329
  • 8
  • 23
  • 1
    possible duplicate of [Get URL parameter with jQuery](http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquery) – Raphaël Althaus Oct 12 '12 at 15:41
  • 1
    all you need is `$(form).serializeArray()` in place of `{pass the value here}`, along with fixing the many syntax errors throughout the code. – Kevin B Oct 12 '12 at 15:46

1 Answers1

0

Could you add a name and onsubmit attribute to your form, calling a validation JS function which loops through all of the fields with the same name and constructs some type of string or array for you to easily add info to your db? e.g.:

<form name=form1 action='#' method=post onsubmit='return validateForm()'>
<fieldset>
<input type=text name='field[]' value='firstValue'>
</fieldset>
</form>

function validateForm(){
count = 0;
str = '';
for(x=0; x<document.form1.elements["field[]"].length; x++){
str += document.form1.elements["field[]"][x].value + ','
count++;
}
//submit form data
SirTophamHatt
  • 1,581
  • 17
  • 23