'If action is on a different page do I use fwrite function

I have a form that has an action of a GET on another page.. IT also has a post method that I am using to write form data too .txt file.. When I have an action of (action="") it writes to .txt file but when I have an action on a different page (action="somepage.php?getthings") it does not write to txt file.. is there a fix to this?

<?php
//Get the email from POST
$email = $_REQUEST['EMAIL'];
$file = fopen("document.txt","a+");
fwrite($file,$email . "\n");


//redirect
?>



    <form id="subscribe-form1" action="THIS IS WHERE THE PROBLEM IS"
    method="post" 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" name="submit" value="submit" action="" >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>      
      

    </form>
php


Solution 1:[1]

I'm still a bit confused because you mention the use of both POST and GET. You cannot write to the mailchimp script, and I don't know exactly how your mailchimp form is configured so let's try a different approach. Let's try ajax, even though you did not tag this.

First, change the action of the form to your mailchimp URL instead of "". Then create a new PHP file:

// this is your ajax file, ajax.php


<?php

    $email = $_POST['EMAIL']; // your form method is post, no need for $_REQUEST
    $file = fopen("document.txt","a+");
    $write = fwrite($file,$email . "\n");

    echo $write ? "Success" : "Fail";
?>

Now change your button from type="submit button" to type="button".

You need to add some jQuery:

<script>

    function executeAjax() {
        $.ajax({
                 url: 'ajax.php',
                 type: 'POST',
                 data: {EMAIL: $("#subscribe-form1").find("input[type='email']"},
                 success: function(data) {
                     alert(data); return false;
                     document.getElementById("subscribe-form1").submit();
                 },
                 error: function(e) {
                     alert('There was a problem...please try again');
                 }
        });
    }

    $(document).ready(function () {
        $("button").click(function () {
            executeAjax();
        });
    });
</script>

After posting the email address to the ajax file on the backend, the file will be written. Then, the form action will be executed.

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