0

I have a form created by Mailchimp (email campaign service) that allows users to subscribe to a newsletter. I want to send an email of the form contents after form submit AND post the original form action.

Ive used the php mail() function to do this taken from this link, and it worked fine: Send email with PHP from html form on submit with the same script

But I do not know how to submit the original post action from my subscription form:

<form action="http://mailchimpurl.com/subscribe/post" method="POST">

Again, I want to send an email of the form contents AND also have the subscription post request sent.

Thanks for the help!

Community
  • 1
  • 1
stevelaaaa
  • 85
  • 6
  • Are you using jQuery on the site? This would be quite simple if thats an option. – Steve May 15 '14 at 14:16
  • Yes I am using jQuery. Care to explain how? – stevelaaaa May 15 '14 at 14:27
  • See the answer by John (maybe ask him to expand on how the ajax is handled), his answer is what i was aiming at. Though Quentin is correct that you would be better using the mailchimp api if you have access – Steve May 15 '14 at 14:34

4 Answers4

0

I'd look into triggering this client side with JS/jQuery/ajax if your action is pointing to a location you can not add any php mail() or similar to.

var sendEmail = 'youremailpage.php?email=you@youremail.com&subject=....'

$("#submit").click(function(){
  // use ajax to trigger an external php file with mail()
  $.ajax({ url: sendEmail, data: {action: sendEmail}, type: "post", success: function(output) {  } }); 
 });

In the PHP file I just pull all the url parameters using $_GET('email') etc...

Something like this or this are similar techniques usint $.post instead, although I haven't tested them out myself.

John
  • 11,985
  • 3
  • 45
  • 60
  • "send email here"? How? jQuery doesn't have any email sending functionality. – Quentin May 15 '14 at 14:26
  • From my understanding you cannot send email through javascript. How do you propose to do this? – stevelaaaa May 15 '14 at 14:27
  • "use ajax to send email" - That won't work. The form will submit and the JS execution environment will go away before the HTTP request is sent. – Quentin May 15 '14 at 14:33
  • I use something similar to this to trigger php files without page refresh, but not specifically on form submits. Maybe someone better versed at jQuery could supply a better answer. – John May 15 '14 at 14:42
  • @StephenL elaborated a bit more in my answer. – John May 15 '14 at 14:44
0

It is generally better for any form for submit to one end point, and one end point only.

Set the action to point to your own server. Then have the program that handles the request do two different things (sequentially).

  1. Send the email to you, using the script you already have
  2. Subscribe the user to the mailing list, using your service provider's API
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks this makes sense. But is there a way to use the form that Mailchimp generated for me so that I do not have to use the API? – stevelaaaa May 15 '14 at 14:34
  • Not one I would recommend using. You could use JS to stop the form being submitted, then use JS to gather up the data in it and send it to your server, then wait for the response, then trigger the form submission with JS … but that's twiddly and depends on JS. – Quentin May 15 '14 at 14:36
0
$('#yourform').submit( function(ev) {
    ev.preventDefault(); //stop form submitting

    theform = this;

    $.post( "example.php", $(this).serialize()).done(function() {
        theform.submit();
    });

}); 
Steve
  • 20,703
  • 5
  • 41
  • 67
  • This didnt work out for me. Got a "Uncaught TypeError: object is not a function" from `form.submit();` . And if I try `$('#yourform').submit();` in the `.done(function(){})`, I get an infinite loop. – stevelaaaa May 15 '14 at 15:10
  • @StephenL please try with a different name for the global variable, i have changed it to 'theform' in my edit. I believe that may be the source of your error – Steve May 15 '14 at 15:19
  • I dont think that is the problem, I've tried `form1=this` and got the same error. – stevelaaaa May 15 '14 at 15:23
  • @StephenL Yes but do you have ANY element on the page where id="form1"? If yes thats confusing the parser. Choose a completely unique name for the variable, eg `hskfyyeknnso = this;` to double check. – Steve May 15 '14 at 15:32
  • I have only ever seen this error with either variable name collisions (as i expect) or bad syntax (missing semicolons etc). – Steve May 15 '14 at 15:34
  • @StephenL Your form has a button with the name 'submit' doesnt it! Rename that and this will work. Regardless, if your cUrl solution worked for you, you should mark your own answer as the correct one. – Steve May 15 '14 at 20:34
  • Yes the Curl solution worked perfectly. I will accept it as an answer when SO lets me. Thanks for your input! And yes, my button had name=submit. – stevelaaaa May 17 '14 at 00:57
0

I have found a solution for this problem. Using PHP CURL, I was able to send a post request to my mailchimp url. Here is a link to the blog post I used.

http://davidwalsh.name/curl-post

stevelaaaa
  • 85
  • 6