【问题标题】:How do I Post to .txt document before form submits如何在表单提交之前发布到 .txt 文档
【发布时间】:2017-01-28 14:36:07
【问题描述】:

好的,所以我想做的是在我向 Mailchimp 提交带有某人电子邮件的表单之前,我想将该电子邮件写入 .txt 文件。 Mailchimp 对表单使用“获取”,并且“操作”在 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>

【问题讨论】:

    标签: php ajax mailchimp


    【解决方案1】:

    您可以尝试手动将值附加到 URL,然后重定向到它:

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

    请注意,原始“黑猩猩”URL 包含一个 & 符号 $amp; 作为 HTML 符号。我认为我们可以摆脱它并使用“自然”与符号&amp;

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

    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 的末尾。生成的 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;
    }
    ?>
    

    【讨论】:

    • 我投了赞成票,只是因为你的答案中有说教字符
    • 当我使用您的代码运行表单时,它会停留在同一页面上。它看起来像是提交了,但顶部的 URL 没有改变..
    • 它不会那样重定向。它保持在同一页面上。它只是提交电子邮件。停留在同一页面上。不管它是否有效。每次工作时我都会收到一封电子邮件。每次它不起作用时,我都没有收到电子邮件。
    • 注意:未定义变量:第 16 行 /Applications/MAMP/htdocs/PTF_LOCAL/risk-free/includes/header.php 中的 _SESSION 注意:未定义索引:/Applications/MAMP/htdocs/ 中的 cid PTF_LOCAL/risk-free/includes/header.php 在第 16 行
    • 不显示任何内容
    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多