如何使用action-xhr和admin-post发送AMP表单电子邮件

时间:2017-04-25 13:35:13

标签: php wordpress amp-html

我在wordpress中有一个网站,我在AMP中做了一些页面,但我需要与AMP建立一个兼容的表单,here我有文档,我有一些困难,主要是为了连接“ action-xhr“与wordpress。我试图制作一个永恒的文件,但我遇到了错误。 enter image description here

<form target="_top" action-xhr="https://mywebsite.com/wp-admin/admin-post.php" method="post" name="test">
        <input type="text" name="name" value="Name">
        <input type="submit"/>
    </form>

1 个答案:

答案 0 :(得分:2)

使用此代码,如果您在整合此问题时遇到任何问题,请告知我们。

   <?php
    if(!empty($_POST))
    {
        $name = $_POST['name'];

        /*/ this is the email we get from visitors*/
        $domain_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";

        /*//-->MUST BE 'https://';*/
        header("Content-type: application/json");
        header("Access-Control-Allow-Credentials: true");
        header("Access-Control-Allow-Origin: *.ampproject.org");
        header("AMP-Access-Control-Allow-Source-Origin: ".$domain_url);


        /*/ For Sending Error Use this code /*/
        if(!mail("email@example.com" , "Test submission" , "email: $name <br/> name: $name" , "From: $name\n ")){
            header("HTTP/1.0 412 Precondition Failed", true, 412);

            echo json_encode(array('errmsg'=>'There is some error while sending email!'));
            die();
        }
        else
        {
            /*/--Assuming all validations are good here--*/
            header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");   

                echo json_encode(array('successmsg'=>$_POST['name'].'My success message. [It will be displayed shortly(!) if with redirect]'));
            die();
        }
    }?>