如何使一个实时PHP文件将数据发送回本地机器?

时间:2017-05-27 17:49:59

标签: php html

我正在构建一个带有集成联系表单(使用PHP)的phonegap应用程序,我必须添加一个实时的.php文件才能使其正常工作。在"success"我的在线文件未将数据发送回本地计算机后,我的联系表单不会重置,也没有成功消息。有什么建议吗?



$("#contactForm").validator().on("submit", function (event) {
    if (event.isDefaultPrevented()) {
        // handle the invalid form...
        formError();
        submitMSG(false, "Did you fill in the form properly?");
    } else {
        // everything looks good!
        event.preventDefault();
        submitForm();
    }
});


function submitForm(){
    // Initiate Variables With Form Content
    var name = $("#name").val();
    var email = $("#email").val();
    var message = $("#message").val();

    $.ajax({
        type: "POST",
        url: "https://example.com/contact.php",
        data: "name=" + name + "&email=" + email + "&message=" + message,
        success: function(text){
            if (text == "success"){
                formSuccess();
            } else {
                formError();
                submitMSG(false,text);
            }
        }
    });
}

function formSuccess(){
    $("#contactForm")[0].reset();
    submitMSG(true, "Message Submitted!")
}

function submitMSG(valid, msg){
    if(valid){
        var msgClasses = "h3 text-center tada animated text-success";
    } else {
        var msgClasses = "h3 text-center text-danger";
    }
    $("#msgSubmit").removeClass("hidden").addClass(msgClasses).text(msg);
}

    <form role="form" id="contactForm" data-toggle="validator">
        <div class="row">
            <div class="form-group col-sm-6">
                <!--<label for="name" class="h4">Name</label>-->
                <input type="text" class="form-control" id="name" placeholder="Enter name" required>
                <div class="help-block with-errors"></div>
            </div>
            <div class="form-group col-sm-6">
                <!--<label for="email" class="h4">Email</label>-->
                <input type="email" class="form-control" id="email" placeholder="Enter email" required>
                <div class="help-block with-errors"></div>
            </div>
        </div>
        <div class="form-group">
            <!--<label for="message" class="h4 ">Message</label>-->
            <textarea id="message" class="form-control" rows="5" placeholder="Enter your message" required></textarea>
            <div class="help-block with-errors"></div>
        </div>
        <button type="submit" id="form-submit" class="btn btn-success btn-lg pull-right ">Submit</button>
        <div id="msgSubmit" class="h3 text-center"></div>
        <div class="clearfix"></div>
    </form>
&#13;
&#13;
&#13;

<?php

$errorMSG = "";

// NAME
if (empty($_POST["name"])) {
$errorMSG = "Name is required ";
} else {
$name = $_POST["name"];
}

// EMAIL
if (empty($_POST["email"])) {
$errorMSG .= "Email is required ";
} else {
$email = $_POST["email"];
}

// MESSAGE
if (empty($_POST["message"])) {
$errorMSG .= "Message is required ";
} else {
$message = $_POST["message"];
}


$EmailTo = "example@domain.com";
$Subject = "New Message Received";

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);

// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
    echo "Something went wrong :(";
} else {
    echo $errorMSG;
}
}

?>

1 个答案:

答案 0 :(得分:1)

  

XMLHttpRequest无法加载https://mywebsite.com/contact.php。没有   &#39; Access-Control-Allow-Origin&#39;标题出现在请求的上   资源。起源&#39; null&#39;因此不允许访问。

您需要在服务器上启用CORS,请参阅

https://enable-cors.org/

这可以像将Access-Control-Allow-Origin: *添加到https://mywebsite.com/contact.php一样简单,即:

<?php
 header("Access-Control-Allow-Origin: *");
 # rest of the code...