How do I send a POST request through a form while also sending an email of the form contents?

StackOverflow https://stackoverflow.com/questions/23680783

  •  23-07-2023
  •  | 
  •  

Pregunta

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!

¿Fue útil?

Solución 4

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

Otros consejos

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

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.

$('#yourform').submit( function(ev) {
    ev.preventDefault(); //stop form submitting

    theform = this;

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

}); 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top