简体   繁体   中英

Issue with submitting form as email

I'm trying to submit a form as an email through PHP but I'm sending the data through an AJAX function call in jQuery so the page itself doesn't refresh (in short, the user fills out the form and then when they hit submit, it sets the data to a variable and sends that to the PHP page via AJAX call, all without the page reloading) but for some reason, the emails aren't sending, but the code itself seems to function?

Here's the code that I have (just the AJAX call and the php code).

        var dataString = 'name=' + name + '&email=' + email + "&subject=" + subject +  '&msg=' + msg;
$.ajax ({  
            type: "POST",  
            url: "process.php",  
            data: dataString,  
            success: function() {  
                $("#success").show();
                $( '#contactUs' ).each(function(){
                    this.reset();
                });
            }
});

PHP

<?php
if(isset($_POST['email'])) {

    $email_to = "xxx@someplace.net";
    $email_subject = "subject";

    $name = $_POST['name']; // required
    $email_from = $_POST['email']; // required
    $message = $_POST['msg']; // required

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Message: ".clean_string($message)."\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, $message, $headers);  

?>

try to use this syntax for posting your variables as an object

$.ajax ({  
        type: "POST",  
        url: "process.php",
        dataType: 'json'  
        data: {
            dataString : name,
            email : email,
            subject :subject,
            msg : msg
        },  
        success: function() {  
            $("#success").show();
            $( '#contactUs' ).each(function(){
                this.reset();
            });
        }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM