2017-04-25 13 views
1

我在wordpress中有一個網站,我在AMP中做了一些頁面,但我需要使用AMP製作兼容表單,here我有文檔,我主要將「action-xhr」與wordpress連接起來有些困難。我試圖建立一個永恆的文件,但我有錯誤。 enter image description here如何使用action-xhr和admin-post發送AMP表單電子郵件

<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> 

回答

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 protected]" , "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(); 
     } 
    }?> 
+0

感謝回答,代碼顯然是可行的,因爲當我點擊發送它出現類「用戶有效的amp-form-submit-success」,但沒有任何東西到達我的電子郵件。可能會丟失什麼? 謝謝 – ctovelox

+1

沒有看到你的代碼不能說什麼...... – BlueSuiter

+0

我使用的是wordpress AMP插件,一切看起來都很好我用你的PHP代碼,我改變了把電子郵件的一部分,我把它加載到主我的網站文件夾wp。 我改變了這部分我的電子郵件:'!mail(「[email protected]」' 我的表單是 '

' 不攜帶 – ctovelox

相關問題