Unable to send email through PHP with alert message as notification linked with jQuery

854 views Asked by At

I am trying to set an alert message if the message has been sent successfully or if an error occurs.

This is the following error when I run and click the "Send Mail" button in my html page.

Error: Microsoft JScript runtime error: Unable to get value of the property 'addClass': object is null or undefined

This is my PHP code snippet:

if(@mail($to,$subject,$message,$header)) $send = true; else $send = false;

if(isset($_POST['email'])){

            if ($send)
            {
                echo '<script language="javascript">';
                echo 'alert("Mail has been Sent Successfully")';    
                echo '</script>';     
                exit;                    
            }
            else
            {
                echo '<script language="javascript">';                   
                echo 'alert("ERROR")';  
                echo '</script>';
                exit;
            }

This is my jQuery snippet:

function tfuse_custom_form(){ 
var my_error;
var url = jQuery("input[name=temp_url]").attr('value');


jQuery("#send").bind("click", function(){

my_error = false;
jQuery(".ajax_form input, .ajax_form textarea, .ajax_form radio, .ajax_form select").each(function(i)
{
       var surrounding_element  = jQuery(this);
       var value                = jQuery(this).attr("value"); 
       var check_for            = jQuery(this).attr("id");
       var required             = jQuery(this).hasClass("required"); 

    if(check_for == "email")
    {
        surrounding_element.removeClass("error valid");
        baseclases = surrounding_element.attr("class");

        if(!value.match(/^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$/))
        {
            surrounding_element.attr("class",baseclases).addClass("error");
            my_error = true;
        }
        else
        {
            surrounding_element.attr("class",baseclases).addClass("valid"); 
        }
    }
    if(check_for == "message")
     {
        surrounding_element.removeClass("error valid");
        baseclases = surrounding_element.attr("class");

        if(value == "" || value == "Write your message...")
        {                   
            surrounding_element.attr("class",baseclases).addClass("error");
            my_error = true;
        }
        else
        {
            surrounding_element.attr("class",baseclases).addClass("valid"); 
        }
    }

    if(required && check_for != "email" && check_for != "message")
    {
        surrounding_element.removeClass("error valid");
        baseclases = surrounding_element.attr("class");
        if(value == "")
        {           
            surrounding_element.attr("class",baseclases).addClass("error");
            my_error = true;
        }
        else
        {
            surrounding_element.attr("class",baseclases).addClass("valid"); 
        }
     }
}
1

There are 1 answers

0
Hari Om Srivastava On

First compare this code and then replace it simply if you want to jquery in php add this code in php file.

1).

         <?php  echo '<script language="javascript">
                   alert("Mail has been Sent Successfully");   
               </script>';
          ?>

2).

        if ($send)
        {
            echo '<script language="javascript">
            alert("Mail has been Sent Successfully");   
            </script>';     
            exit;                    
        }
        else
        {
            echo '<script language="javascript">
            alert("ERROR")'
            </script>';

            exit;
        }