如何在表单提交之前发布到.txt文档

时间:2016-09-20 15:23:16

标签: php ajax mailchimp

好的,所以我要做的是在我向Mailchimp提交表单之前,我想将该电子邮件发送到.txt文件。 Mailchimp正在使用" get"表格和"行动"在mailchimp上运行并不是与表单相同的页面。这是我的表格代码。



   <form id="subscribe-form1" action="https://personaltrainer.us6.list-manage.com/subscribe/post-json?u=4aeb5b710adef51ab754ll02f&amp;id=76420114ff"
    method="get" class="form-inline">
      <div class="input-group">
        <input type="email" class="form-control" placeholder="Email address" name="EMAIL">

        <div class="input-group-btn">
          <button class="btn btn-grn" type="submit button" data-toggle="modal" data-target="#myModal">Sign Up</button>
        </div>
      </div>
      <div style="visibility:collapse;" id="subscribe-result1"> </div>
        <div class="checkbox">
          <label>
            <input type="checkbox" id="mce-group[6917]-6917-0" name="group[6917][1024]" value="1024" checked="checked" style="">
            I agree to recieve FREE newsletter from Personal Trainer Food </label>
        </div>  
     
 <?php //this only works if I change get->post and action ="" but then it does not submit to mail chimp.
//Get the email from POST
$email = $_REQUEST['EMAIL'];
$file = fopen("document.txt","a+");
fwrite($file,$email . "\n");


//redirect
?>    
      

    </form>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您可以尝试手动将值附加到网址,然后重定向到该网址:

                                    ▼
  <form id="subscribe-form1" action="" method="get" class="form-inline">
      <div class="input-group">
        <input type="email" class="form-control" placeholder="Email address" name="EMAIL">

        <div class="input-group-btn">
          <button class="btn btn-grn" type="submit button" data-toggle="modal" data-target="#myModal">Sign Up</button>
        </div>
      </div>
      <div style="visibility:collapse;" id="subscribe-result1"> </div>
        <div class="checkbox">
          <label>
            <input type="checkbox" id="mce-group[6917]-6917-0" name="group[6917][1024]" value="1024" checked="checked" style="">
            I agree to recieve FREE newsletter from Personal Trainer Food </label>
        </div>  

 <?php //this only works if I change get->post and action ="" but then it does not submit to mail chimp.
//Get the email from GET ◄■■■
$email = $_REQUEST['EMAIL'];
$file = fopen("document.txt","a+");
fwrite($file,$email . "\n");                                          URL PARAMETERS START
fclose($file); // ◄■■■                                                           ▼
header("Location: https://personaltrainer.us6.list-manage.com/subscribe/post-json?u=4aeb5b710adef51ab754ll02f&id=76420114ff&EMAIL=$email"); // ◄■■■
exit; // ◄■■■
?>    

    </form>

注意原作&#34;黑猩猩&#34; URL包含&符号$amp;作为HTML符号。我想我们可以摆脱它并使用&#34; natural&#34; &符号&

表单中有一个复选框,我们也可以添加它:

fclose($file); // ◄■■■
if ( isset( $_GET["group[6917][1024]"] ) ) // IF CHECKBOX IS CHECKED...
     $chk = "&group[6917][1024]=1024";                                URL PARAMETERS START
else $chk = "";                                                                  ▼
header("Location: https://personaltrainer.us6.list-manage.com/subscribe/post-json?u=4aeb5b710adef51ab754ll02f&id=76420114ff&EMAIL=$email$chk"); // ◄■■■
exit; // ◄■■■

变量$email$chk位于网址的末尾。结果URL的一个示例是:

  

https://personaltrainer.us6.list-manage.com/subscribe/post-json?u=4aeb5b710adef51ab754ll02f&id=76420114ff&EMAIL=josmanaba@yahoo.com&group[6917][1024]=1024

修改:

在PHP代码中添加了if

<?php
if ( isset( $_GET["EMAIL"] ) ) {
  $email = $_REQUEST['EMAIL'];
  if ( isset( $_GET["group"] ) )
       $chk = "&group[6917][1024]=1024";
  else $chk = "";
  header("Location: https://personaltrainer.us6.list-manage.com/subscribe/post-json?u=4aeb5b710adef51ab754ll02f&id=76420114ff&EMAIL=$email$chk");
  exit;
}
?>

编辑#2:

<?php
if ( isset( $_GET["EMAIL"] ) ) {
  $email = $_REQUEST['EMAIL'];
  // SAVE EMAIL.
    $file = fopen("document.txt","a");
    fwrite($file,$email . "\n");
    fclose($file);
  if ( isset( $_GET["group"] ) )
       $chk = "&group[6917][1024]=1024";
  else $chk = "";
  header("Location: https://personaltrainer.us6.list-manage.com/subscribe/post-json?u=4aeb5b710adef51ab754ll02f&id=76420114ff&EMAIL=$email$chk");
  exit;
}
?>

编辑#3

使用表单重定向并使用javascript自动提交:

<?php
if ( isset( $_GET["EMAIL"] ) ) {
  $email = $_REQUEST['EMAIL'];
  // SAVE EMAIL.
    $file = fopen("document.txt","a");
    fwrite($file,$email . "\n");
    fclose($file);
  if ( isset( $_GET["group"] ) )
       $chk = "&group[6917][1024]=1024";
  else $chk = "";
  echo "<form method='get'" .
       "      id='frm'" .
       "      target='_blank'" .
       "      action='https://personaltrainer.us6.list-manage.com/subscribe/post-json?u=4aeb5b710adef51ab754ll02f&id=76420114ff&EMAIL=$email$chk'>" .
       "</form>" .
       "<script type='text/javascript'>" .
       "document.getElementById('frm').submit();" .
       "</script>";
  exit;
}
?>

编辑#4:

这是编辑#2,但将URL保存在文本文件中:

<?php
if ( isset( $_GET["EMAIL"] ) ) {
  $email = $_REQUEST['EMAIL'];
  if ( isset( $_GET["group"] ) )
       $chk = "&group[6917][1024]=1024";
  else $chk = "";
  $url = "https://personaltrainer.us6.list-manage.com/subscribe/post-json?u=4aeb5b710adef51ab754ll02f&id=76420114ff&EMAIL=$email$chk"
  // SAVE EMAIL.
    $file = fopen("document.txt","a");
    fwrite($file,$email . "\n");
    fwrite($file,$url . "\n");
    fclose($file);
  header("Location: $url");
  exit;
}
?>