-
-
Save 1stevengrant/1589101 to your computer and use it in GitHub Desktop.
<?php | |
/*/////////////////////////////////////////////////////////////////////// | |
Part of the code from the book | |
Building Findable Websites: Web Standards, SEO, and Beyond | |
by Aarron Walter ([email protected]) | |
http://buildingfindablewebsites.com | |
Distrbuted under Creative Commons license | |
http://creativecommons.org/licenses/by-sa/3.0/us/ | |
///////////////////////////////////////////////////////////////////////*/ | |
function storeAddress(){ | |
// Validation | |
if(!$_GET['email']){ return "No email address provided"; } | |
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) { | |
return "Email address is invalid"; | |
} | |
require_once('MCAPI.class.php'); | |
// grab an API Key from http://admin.mailchimp.com/account/api/ | |
$api = new MCAPI('dd1af3a9705e8c60f6ab86ce0918d7b7-us1'); | |
// 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 = "b590c7d525"; | |
if($api->listSubscribe($list_id, $_GET['email'], '') === true) { | |
// It worked! | |
return 'Woohoo! You\'re now keeping current with happenings at Greenview'; | |
} else { | |
// An error ocurred, return error message | |
return 'Error: ' . $api->errorMessage; | |
} | |
} | |
// If being called via ajax, autorun the function | |
if($_GET['ajax']){ echo storeAddress(); } | |
?> |
Your template is including the file which is looking for a GET index of 'ajax' immediately. Using the isset()
method clears away the error and allows it to still work when your ajax call occurs (assuming your javascript using that as a GET key). isset()
is one of a few options that could be used here.
The form needs to include an ajax
field to trigger the call to the storeAddress
method:
<form>
<input id="ajax" name="ajax" type="hidden" value="y" />
</form>
Stephen, it looks like he already has ajax being passed via jQuery in his functions.js file. Would the form field still be required for non ajax calls?
Previous comment deleted after confusing myself for 5 minutes...
You're right, Erik. functions.js
does add the ajax
parameter (I hadn't seen that file).
In that case, the submit
field needs to be given a name, so the test in [the form gist]:https://gist.github.com/1589144 will work. That should all work then, will a non-AJAX fallback (I think). The way this is organised could be a lot clearer though.
You can get rid of your PHP notice by changing line 41 above to:
if(isset($_GET['ajax'])) { echo storeAddress(); }