ajax send with jquery to php file doesn't work -


i want send information of contact form server , send contact info mail. normal contact form.

but error jquery.js on line 9631 part:

//do send request //this may raise exception //handled in jquery.ajax (so no try/catch here) xhr.send( ( options.hascontent && options.data ) || null ); 

can explain me error? don't find mistake , don't know how debug it.

    <fieldset id="contactform">     <p>wünschen sie eine abholung von schrott oder edelmetallen<br>     geben sie bitte <b>genaue angaben über die art und menge</b> um weitere rückfragen zu ersparen.     </p>     <hr>         <div class="contactfield">           <div id="result"></div>           <label for="name"><span>ihr name</span>             <input class="frm-name email required" type="text" name="name" id="name">           </label>            <label for="email"><span>ihre e-mail</span>             <input class="frm-mail email required" type="email" name="email" id="email">           </label>            <label for="subject"><span>betreff</span>             <input class="frm-mail subject required" name="subject" id="subject">           </label>            <label for="message"><span>nachricht</span>             <textarea name="message" rows="10" cols="30" class="no-value frm-message required" id="message"></textarea>           </label>            <label>             <button class="submit_btn" id="submit_btn">abschicken</button>           </label>         </div>     </fieldset> 

jquery

$("#submit_btn").click(function() {     //get input field values     var user_name       = $('input[name=name]').val();     var user_email      = $('input[name=email]').val();     var user_subject    = $('input[name=subject]').val();     var user_message    = $('textarea[name=message]').val();      //simple validation @ client's end     //we change border color red if empty field using .css()     var proceed = true;     if(user_name==""){         $('input[name=name]').css('border-color','red');         proceed = false;     }     if(user_email==""){         $('input[name=email]').css('border-color','red');         proceed = false;     }     if(user_subject==""){         $('input[name=subject]').css('border-color','red');         proceed = false;     }     if(user_message=="") {           $('textarea[name=message]').css('border-color','red');         proceed = false;     }      //everything looks good! proceed...     if(proceed)     {         //data sent server         post_data = {'username':user_name, 'useremail':user_email, 'usersubject':user_subject, 'usermessage':user_message};          //ajax post data server         $.post('mail.php', post_data, function(response){                //load json data server , output message                 if(response.type == 'error')             {                 output = '<div class="error">'+response.text+'</div>';             }else{                  output = '<div class="success">'+response.text+'</div>';                  //reset values in input fields                 $('#contact_form input').val('');                 $('#contact_form textarea').val('');             }              $("#result").hide().html(output).slidedown();         }, 'json');      } });  //reset set border colors , hide message on .keyup() $("#contact_form input, #contact_form textarea").keyup(function() {     $("#contact_form input, #contact_form textarea").css('border-color','');     $("#result").slideup(); }); 

php

<?php if($_post) {     $to_email       = "mymail.com"; //replace recipient email address       //check if ajax request, exit if not     if(!isset($_server['http_x_requested_with']) , strtolower($_server['http_x_requested_with']) != 'xmlhttprequest') {          //exit script outputting json data         $output = json_encode(         array(             'type'=>'error',             'text' => 'request must come ajax'         ));          die($output);     }      //check $_post vars set, exit if missing     if(!isset($_post["username"]) || !isset($_post["useremail"]) || !isset($_post["usermessage"])) || !isset($_post["usersubject"]))     {         $output = json_encode(array('type'=>'error', 'text' => 'input fields empty!'));         die($output);     }      //sanitize input data using php filter_var().     $user_name        = filter_var($_post["username"], filter_sanitize_string);     $user_subject   = filter_var($_post["usersubject"], filter_sanitize_string);     $user_email       = filter_var($_post["useremail"], filter_sanitize_email);     $user_message     = filter_var($_post["usermessage"], filter_sanitize_string);      //additional php validation     if(strlen($user_name)<4) // if length less 4 throw http error.     {         $output = json_encode(array('type'=>'error', 'text' => 'ihr name ist zu kurz.'));         die($output);     }     if(!filter_var($user_email, filter_validate_email)) //email validation     {         $output = json_encode(array('type'=>'error', 'text' => 'das ist keine gültige e-mail adresse'));         die($output);     }     if(strlen($user_subject)<2) //check emtpy message     {         $output = json_encode(array('type'=>'error', 'text' => 'sie haben einen betreff vergessen!'));         die($output);     }         if(strlen($user_message)<5) //check emtpy message     {         $output = json_encode(array('type'=>'error', 'text' => 'sie haben ihre nachricht vergessen!'));         die($output);     }      //proceed php email.     $headers = 'from: '.$user_email.'' . "\r\n" .     'reply-to: '.$user_email.'' . "\r\n" .     'x-mailer: php/' . phpversion();          // send mail     $sentmail = @mail($to_email, $user_subject, $user_message .'  -'.$user_name, $headers);      if(!$sentmail)     {         $output = json_encode(array('type'=>'error', 'text' => 'konnte mail nicht verschicken. wenden sie sich den administrator.'));         die($output);     }else{         $output = json_encode(array('type'=>'message', 'text' => .$user_name .' vielen dank für ihre nachricht.'));         die($output);     } } ?> 

since you're using ajax need prevent default action of clicking submit button -

$("#submit_btn").click(function(event) {     event.preventdefault();     //get input field values     .... 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -