0

How to pass arrays and values into javascript function. fund and agent pass as an array. fromDate and toDate are pass as a single variable.

function fetch_javaScript(fund,agent,fromDate,toDate)
         {
           -------
           -------

           var agent = document.getElementById('agent').value;
           var fromDate = document.getElementById('fromDate').value;
           var toDate = document.getElementById('toDate').value;
           var queryString = "?fund=" + fund_name ;
           queryString +=  "&agent=" + agent ;
           queryString += "&fromDate=" + fromDate;
           queryString += "&toDate=" + toDate;
           ajaxRequest.open("GET", "final_ajax.pl" +
                              queryString, true);       
         }


        <tr><td>
           <h2>Funds</h2>
           </td>
           <td><select name="Funds" id="fund" multiple >
                  <option value="Select">Select</option>
                  <option value="PMF">Principal</option>
                  <option value="PRAMERICA">Pramerica</option>
                  <option value="JM">JM</option>
                  <option value="DHL">DHL</option>
                  <option value="EMF">Edelweiss MF</option>
                  ----
                  ---- 
          </select> <br></td>
        </tr>       


         <tr><td>
             <h2>Agents</h2>
             </td>
         <td><select name="Agents" id="agent" multiple>
                <option value="Select">Select</option>
                <option value="Gyanesh">Gyanesh</option>
                <option value="Satish">Satish</option>
                <option value="Sailesh">Sailesh</option>
                <option value="ArchanaMuluguru">Archana</option>
                ----
                ----    
         </select></td>
         </tr>     

I'm able to pass single variables into query string like that

?fund=PMF&agent=Balaji&fromDate=5-8-2013%209:33:6&toDate=10-8-2013%209:33:14

problem with array.

 How can I build a querystring looks like that?

?fund[]=["PMF","JM"]&agent[]=["Balaji"]&fromDate=5-8-2013%209:33:6&toDate=10-8-2013%209:33:14

fund and agent are array types it is depend upon user it select single value or multiple value. fromDate and toDate are single values

2
  • fund and agent are passed to javascript as array, or to your ajax request or both? Commented Aug 15, 2013 at 5:16
  • ajax request is also there Commented Aug 15, 2013 at 5:18

3 Answers 3

1

You can use this function to get multiple selected options from select:

function get_selected_data(o){
    var selectedOptions = [];
    for (var i = 0; i < o.options.length; i++){
        if(o.options[i].selected){ 
            selectedOptions.push('"' + o.options[i].value + '"');
        } 
    }
    if(selectedOptions.length){
        return '[]=[' + selectedOptions.join(',') + ']';
    } else {
        return '';
    }
}

For example:

var queryString = "?fund" + get_selected_data(document.getElementById('fund'));
queryString +=  "&agent" + get_selected_data(document.getElementById('agent'));
queryString += "&fromDate=" + fromDate;
queryString += "&toDate=" + toDate;
Sign up to request clarification or add additional context in comments.

Comments

0

Well, you could use the toString Javascript function

http://www.w3schools.com/jsref/jsref_tostring_array.asp

1 Comment

w3schools is a terrible resource. Here's a better one: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. Anyways, this is a link-only answer, and we don't want link-only answers here. Can you write a code sample on how that function should be used?
0

Use serialized json array like this and send data via post

{

"fund":[
    "value1","value2"
    ],
    "agent":
    ["value1","value2"],
    "fromdate":"value",
    "todate":"value"
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.