2

I'm trying to post the name of what the user enters to sign up in a Mailchimp form (name value is FNAME) to show in my custom thank you page (e.g. "Thank you NAME HERE,"). I haven't seen how to do this with Mailchimp's documentation other than seeing to use *|FNAME|* which doesn't work for what I'm trying to do and realized it might be best to just have posted via <?php echo $_POST["FNAME"]; ?> which I've included in my thankyou.html which is separate to where the form is located (index.html) and the PHP file (store-address.php). However, I can't seem to get it all tied together to actually print the name entered and was wondering if I'm doing anything wrong. Any help is kindly appreciated.

To clarify in an example.. Suzy signs up on my mailchimp form and clicks submit and my thank you page then pops up to say "Thank you Suzy,". Essentially, needs to read what the user entered in the input field to then output again to thank them by their entry. Any solution would be awesome!

index.html (form)

    <!-- Begin MailChimp Signup Form -->
    <div id="mc_embed_signup">
    <form action="<?=$_SERVER['PHP_SELF']; ?>" method="get" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" novalidate>
    <div class="mc-field-group">
        <input type="text" value="" name="FNAME" class="" id="mce-FNAME" placeholder="Name">
    </div>
    <div class="mc-field-group">
        <input type="email" value="" name="email" class="required email" id="mce-EMAIL"  placeholder="Email address">
    </div>
    <div class="clear"></div>
    <div class="mc-field-group">
        <input type="text" value="" name="MMERGE2" class="" id="mce-MMERGE2" placeholder="Zip Code">
    </div>
    <div class="mc-field-group">
        <label for="mce-MMERGE3" class="believeBecause">I believe because:</label>
      <select name="MMERGE3" class="dropdown" id="mce-MMERGE3">
        <option value="" class="label"></option>
        <option value="I am a parent">I am a parent</option>
        <option value="I am an educator">I am an educator</option>
        <option value="I am a student">I am a student</option>
        <option value="I just care">I just care</option>
      </select>
    </div>
        <div id="mce-responses" class="clear">
            <div class="response" id="mce-error-response" style="display:none"></div>
            <div class="response" id="mce-success-response" style="display:none"></div>
        </div>    <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
        <div style="position: absolute; left: -5000px;"><input type="text" name="b_3434334434_0dd34c33da" value=""></div>
        <div class="clear"><input type="submit" value="MAKE A MOVEMENT" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
    </form>
    </div>     

    <!--End mc_embed_signup-->
  </div>
  <!-- /.formWrapper -->      

  <div id="response">
    <? require_once('inc/store-address.php'); if($_GET['subscribe']){ echo storeAddress(); } ?>
  </div> 

store-address.php (what sends the details to via Mailchimp and outputs my thankyou.html)

 <?php
 function storeAddress(){

// Validation
if(!$_GET['email']){ return '<p class="statusJoin">No email address provided</p>'; } 

if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
    return '<p class="statusJoin">Email address is invalid</p>'; 
}

require_once('MCAPI.class.php');
// grab an API Key from http://admin.mailchimp.com/account/api/
$api = new MCAPI('12345fakeAPIkey-us4');

$merge_vars = Array( 
    'email' => $_GET['email'],
    'FNAME' => $_GET['FNAME'],
    'MMERGE2' => $_GET['MMERGE2'],
    'MMERGE3' => $_GET['MMERGE3']
);

// grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
// Click the "settings" link for the list - the Unique Id is at the bottom of that page. 
$list_id = "fakeListID12345";

if($api->listSubscribe($list_id, $_GET['email'], $merge_vars) === true) {
    // It worked!

 echo '<script>';
 echo '$( "#formWrapper" ).hide();';
 echo '$( "#response" ).hide();';      
 echo '$( "#response" ).show();';   
 echo '</script>';

 readfile("../thankyou.html");

}

else {
    // An error ocurred, return error message   
    return 'Error: ' . $api->errorMessage;
}

}

// If being called via ajax, autorun the function
if($_GET['ajax']){ echo storeAddress(); }
?>

And here is the thankyou.html to output the person's name

<html lang="en">
  <body>

    <h2 class="thankYou">Thank you, <?php echo $_POST["FNAME"]; ?></h2>

  </body>
</html>`
DoPeT
  • 299
  • 4
  • 16

2 Answers2

1

To make that work at all, you'll have to rename index.html to index.php. If it's not a PHP file, it can't execute any of the PHP code it has, so it will never include the file store-address.php.

Inside thankyou.php (renamed), you have to use the GET method, since that is the method used by your form:

<h2 class="thankYou">Thank you, <?php echo $_GET["FNAME"]; ?></h2>

To get the contents of thankyou.php, you can simply change this line:

readfile("../thankyou.html");

to:

include 'thankyou.php';
larsAnders
  • 3,813
  • 1
  • 15
  • 19
  • Thanks, I've renamed them! However, I still can't seem to retrieve the name. Any reason why this wouldn't? – DoPeT Mar 21 '14 at 20:38
  • Yes, is there any particular reason you need to use readfile to accomplish this? Could you for instance, just echo out the html from within store-address.php? Or you could just rename thankyou.html as thankyou.php and use include 'thankyou.php';. – larsAnders Mar 21 '14 at 20:40
  • Not exactly, I actually had it with an echo before like this.. `echo '

    Thank you ,

    '` Doesn't seem to work this way either, I just did a readfile because it seemed a little cleaner and sometimes ' or " messes up the echo. P.S. Thanks so much for the incredibly fast reply.
    – DoPeT Mar 21 '14 at 20:42
  • Thank you so much. I really appreciate it. Would you happen to know to only read the first word of the entry? Such as for example.. Suzy Lee types in her full name in FNAME but we want to say Thank you Suzy, to read everything before a space is entered. This is a whole new question, so maybe I should make a new question. But you seem incredibly helpful. I thank you for your time. – DoPeT Mar 21 '14 at 20:53
  • 1
    Sure, that's already answered here on SO - [How to get the first word of a sentence](http://stackoverflow.com/questions/2476789/how-to-get-the-first-word-of-a-sentence-in-php) – larsAnders Mar 21 '14 at 20:55
0

Your form method is GET but you are attempting to display a POST variable...use

<h2 class="thankYou">Thank you, <?php echo $_GET["FNAME"]; ?></h2>
David Wilkins
  • 574
  • 4
  • 19
  • Thank you David, I've just tried this with no luck. Is there anything I'm supposed to do for thankyou.html to realize what was FNAME? – DoPeT Mar 21 '14 at 20:39