Thursday, October 13, 2011

Ajax code to call another application page - Mail sending from Javascript to ASPX


function frm_sendmail() {
        if (document.getElementById("first_name").value == "") {
            alert("Please provide your first name");
            document.getElementById("first_name").focus();
            return false;
        }
        if (document.getElementById("last_name").value == "") {
            alert("Please provide your last name");
            document.getElementById("last_name").focus();
            return false;
        }
        if (document.getElementById("email").value == "") {
            alert("Please provide your email address");
            document.getElementById("email").focus();
            return false;
        }
        else {
            if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById("email").value)) {
                document.getElementById("txtHint").innerHTML = '<span align="center"><img src="links/images/img16.gif"border="0" /></span>';
                var xmlhttp;

                if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                }
                else {// code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }

                xmlhttp.onreadystatechange = function () {
                    // alert('hello'+' '+xmlhttp.readyState+' '+xmlhttp.status);
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        // alert(xmlhttp.responseText);
                        // document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                    }
                }

                first_name = document.getElementById("first_name").value;
                last_name = document.getElementById("last_name").value;
                email = document.getElementById("email").value;
                to_email = document.getElementById("to_email").value;
                subject = document.getElementById("subject").value;

                xmlhttp.open("POST", "smtpmail/Default.aspx?first_name=" + first_name + "&last_name=" + last_name + "&email=" + email + "&to_email=" + to_email + "&subject=" + subject, true);
                xmlhttp.send();
                close_popup('close_seat');
                alert('Thank you for your sign up request. We will contact you shortly.');
            }
            else {
                alert("Incorrect email address format");
                document.getElementById("email").focus();
                return false;
            }
        }
    }

No comments:

Post a Comment