HELP php email with ajax?

hello i cant figure out how 2 get my online email form working

got these codes

html

<form role="form" id="contact-form" name="contact-form" method="post" action="send_form_email.php" class="contact-form">
            <div class="form-group col-md-6">
              <label class="sr-only" for="exampleInputEmail1">Your Name: *</label>
              <input required type="text" class="form-control" id="name" name="name" placeholder="Your Name: *">
            </div>
            <div class="form-group col-md-6">
              <label class="sr-only" for="exampleInputEmail1">Email: *</label>
              <input required type="email" class="form-control" id="email" name="email" placeholder="Email: *">
            </div>
            <div class="clearfix"></div>
            <div class="form-group">
              <label class="sr-only" for="exampleInputEmail1">Subject:</label>
              <input type="text" class="form-control" id="subject" name="subject" placeholder="Subject:">
            </div>
            <div class="form-group">
              <textarea required class="form-control" id="message" name="message" rows="5" placeholder="Message: *"></textarea>
            </div>
            <input id="submit-button" type="submit" class="btn btn-lg btn-info" value="Submit">
          </form>

also on the same html page :

<script type="text/javascript">
            function send_special_order() {
                var data1 = $('#name').val();
                var data2 = $('#subject').val();
                var data3 = $('#email').val();
                var data4 = $('#message').val();
        $.ajax({
			type: "POST",
			url: "send.php",
			dataType : "json",
			data: ( {"name" : data1, "subject" : data2, "email" : data3,"message" : data4} ) ,
			   success: function() {
                               
			   },
                           complete: function(){
                                $('#feedback').append('<p>Thank you for sending us a message. Our specialists will contact you as soon as possible.</p><p>Your message details are:</p>\n\
                                <ul>\n\
                                    <li>Name: <b>' + data1 + '</b></li>\n\
                                    <li>Email: <b>' + data3 + '</b></li>\n\
                                    <li>Subject: <b>' + data2 + '</b></li>\n\
                                    <li>Message: <b>' + data4 + '</b></li>\n\
                                </ul>\n\
                                <p>If you want to change your information press the button:\n\
                                <p><a href="#" id="send-more" class="btn btn-lg btn-info">edit info</a></p>');
                                $('#contact-form').slideUp();
                                $('#send-more').click(function(e){
                                    e.preventDefault()
                                    $('#contact-form').slideDown();
                                    $('#feedback').html('');
                                });
                           }
        });
}
            $('#contact-form').submit(function() {
                send_special_order();
                return false;
            });
        </script> 
<script type="text/javascript">

and the php file :

<?php
if(isset($_POST['email'])) {
 
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "info@solstice-oldeberkoop.nl";
    $email_subject = "Your email subject line";
 
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
 
 
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
 
     
 
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
 
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
 
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
 
    $string_exp = "/^[A-Za-z .'-]+$/";
 
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
 
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
 
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
 
  if(strlen($error_message) > 0) {
    died($error_message);
  }
 
    $email_message = "Form details below.\n\n";
 
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
 
     
 
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
 
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>
 
<!-- include your own success html here -->
 
Thank you for contacting us. We will be in touch with you very soon.
 
<?php
 
}
?>

oke i will admit i don’t really understant this all and its a bunch of google searching

who can help me out with perhaps fix the PHP to get the rest working ?

The first problem is that the fields in your form are called name, email, subject and message, but your PHP code is looking for form fields called first_name, last_name, email, telephone and comments. One of the first things your PHP code does is check that all those fields are filled in, and throw an error if they are not.

So, you can either change the form (and the JS) so that the field names match the ones that the PHP code is expecting, or (probably easier), change the PHP code to accept and deal with the fields you have in the form.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.