'How do I Post to .txt document before form submits

What I am trying to do is before I submit a form to Mailchimp with someones email I want to write that email to a .txt file. Mailchimp is using a "get" for the form and the "action" is run on mailchimp not the same page as form. Here is my code for the form.

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


Solution 1:[1]

You can try appending the values manually to the URL, then redirecting to it:

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

Notice the original "chimp" URL contains an ampersand $amp; as HTML symbol. I think we can get rid of it and use the "natural" ampersand &.

There is a checkbox in your form, we can add it too:

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; // ????

The variables $email and $chk are at the end of the URL. An example of the resulting URL would be:

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

Edit :

Added an if to the PHP code:

<?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;
}
?>

Edit #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;
}
?>

Edit #3

Redirect with a form and auto-submit it with 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;
}
?>

Edit #4 :

This is edit #2 but saving the URL in the textfile :

<?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;
}
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1