开发者

passing parameter from javascript to servlet

I am retr开发者_StackOverflow社区ieving all the contacts of a user from gmail and yahoo, I have added the checkbox, the user needs to select the desired email id to which he need to send email. I need to collect all user selected email id's and save them in a string send that string to another servlet where I am sending emails.

I was able to add the check box dynamically but I am not able to collect the emails and save them in a string. This is the code I have written to add check box before all the emails, kindly help me to put those selected email id's in a string

I used the following code, but still I am not able to do it.You can have a look at the demo of this app http://ec2-50-16-183-101.compute-1.amazonaws.com/SocialAuthNew/ To get the contacts from Gmail type google in the text box and for yahoo type yahoo and click on submit button

 List<Contact> contactsList = provider.getContactList();
                    PrintWriter out = response.getWriter();  
                    out.println("<html>");
                    out.println("<head><script type='text/javascript'>");
                    out.println("function getAllContacts(size){ var selected_list='';");
                    out.println("for(var c=0;c<size;c++){if(document.getElementById('mailCheckbox'+c).checked==true){selected_list=selected_list+document.getElementById('lblmail'+c).innerHTML+':';}}");
                    out.println("document.getElementById('final_mailing_list').innerHTML=selected_list;}</script>");
                    out.println("<title></title>");
                    out.println("</head>");
                    out.println("<body>");

                    for(int i=0;i<contactsList.size();i++){
                        System.out.println(contactsList.get(i).getFirstName()+" : "+contactsList.get(i).getLastName()+":"+contactsList.get(i).getEmail());

                        out.println("<h1> Imported conatcts from your mail are:-</h1>");
                        out.println("<input type='checkbox' id='mailCheckBox"+i+"' name='mailCheckbox'></input>");
                      /*  out.println(contactsList.get(i).getFirstName());
                        out.println(contactsList.get(i).getLastName());*/
                        out.println("<label id='lblmail"+i+"'>"+contactsList.get(i).getEmail()+"</label>");
                    }
                    int size=contactsList.size();
                    out.println("<input type='button' value='GetContact' onclick='getAllContacts("+size+");'/> ");
                    out.println("<div id='final_mailing_list'></div></body>");
                    out.println("</html>");
                }


Try this:

1) Wrap your email in a DOM element to make it easier to access

out.println("<span>" + contactsList.get(i).getEmail() + "</span>");

2) Using something like e.g. JQuery for normalizing access to the DOM on the client, do

function getSelectedEmails() {
  var emails = [];
  $('body').find('input[name="mailCheckbox"]:checked').each(function() {
    emails.push($(this).closest('span').text());
  });
  return emails;
}

This returns the emails in an array - which you can easily concatenate into a string if you want with e.g.

var emailString = emails.join(", ");

...although I think using an array is usually better (perhaps JSON encoded if you need to serialize it).


s using array is much easier. i have used array and gave the check box name as check1 and on the click of submit button i have called the following function. this function alerts the value of the selected check boxes and passes the action to servlet

<script>
function onCallForInterview()
{
var selectedIds;
var count=0;
for (i=0; i<document.frm.check1.length; i++)
{
if (document.frm.check1[i].checked==true)
{
if(count==0){
selectedIds=document.frm.check1[i].value; 
count=count+1; 
}
else
selectedIds=selectedIds+","+document.frm.check1[i].value;
}
}
alert(selectedIds);  
document.frm.action="<%=contextPath%>/SearchCandInfo?    action=selectcanforinterview&ids="+selectedIds;
document.frm.submit();
}
</script>


A slightly more primitive way to achieve this,

Create a bunch of Check-Box as you are doing now but with the difference that all of htem should have the same name i.e. do the following correction in your code

out.println("<input type='checkbox' id='mailCheckBox' name='mailCheckbox'></input>");

Now retrieve all the values of such text boxes on your server side using following call on request object,

            String[] emailIds = request.getParameterValues("mailCheckBox");

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜