ajax form submission with php

ぃ、小莉子 提交于 2019-12-02 08:49:49

can you try $.post instead of $.ajax

$.post(url, {argument_name: value, ...} , function(data){

// callback function..

}, 'json'}

do this with the php page...

  sleep(2);
  //Sanitize incoming data and store in variable
  $name = trim(stripslashes(htmlspecialchars($_POST['name'])));           
  $email = trim(stripslashes(htmlspecialchars($_POST['email'])));
  $message = trim(stripslashes(htmlspecialchars($_POST['message']))); 
  $recipient = "info@internetmarketingtrio.com";



//Validate data and return success or error message
$errors = array();   
$reg_exp = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,4}$/";

if (!preg_match($reg_exp, $email)) {

            $errors[] = "<p>A valid email address is required.</p>";             
}
if (empty($name) || $name == '') {

            $errors[] = "<p>Please provide your name.</p>";              
}           
if (empty($message) || $message == '') {

            $errors[] = "<p>A message is required.</p>";
}
if(empty($errors)) {
    $return['success'] = true;
    $return['message'] = "<p>Thanks for your feedback " .$name. ".</p>";
} else {
    $return['success'] = false;
    $return['message'] = "<h3>Oops! The request was successful but your form is not filled out correctly.</h3>";
    foreach($errors as $error) {
        $return['message'] .= $error ."<br />";
    }
}

And then in your call to get this page... the ajax call...

$.ajax({    
type: 'POST',
url: 'feedback.php',        
data: formData,
dataType: 'json',
cache: false,
success: function(data) { 
    if(data.success) {
        $("form#response").removeClass().addClass('success').html(data.message).fadeIn('fast');
        removeResponse(5000);
    } else {
        $("form#response").removeClass().addClass('error').html(data.message).fadeIn('fast'); 
    }
}        
}); 

function removeResponse(time) {
    setTimeout(function() {
        $("form#response").fadeOut('fast');
    }, time);
}

And that should do ya

adding this to the bottom of my php ended up fixing my issue if anyone reads this

        $emailSubject = 'Contact Form';
        $webMaster = 'blake.harrison1@cox.net';

$body="
<br><hr><br>
<strong>Name:</stong> $name <br>
<br>
 <strong>Email:</stong> $email <br>
 <br>
 <strong>Message:</stong> $message 
";      

        $headers = "From: $email\r\n";
        $headers .= "Content-type: text/html\r\n";


        //send email and return to user
        if(mail($webMaster, $emailSubject, $body, $headers)) {

            $return['error'] = false;
            $return['msg'] = "<p>Message sent successfully. Thank you for your intrest " .$name .".</p>"; 
            echo json_encode($return);
        }
    }   
} else {

$return['error'] = true;
$return['msg'] = "<h3>Oops! There was a problem with your submission. Please try again.</h3>";  
echo json_encode($return);
 }

 ?> 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!