Stephen R
Stephen R

Reputation: 3917

Open email client AND submit a form using PHP / JavaScript

I know how to submit a form. I know how to use a mailto: link. What I can't figure out is how to do both when the user clicks a single link (or button).

Is there a way to both submit form data and open the email client? Was thinking some variation of a mailto: link with an onclick JavaScript attached; but I can't figure out how to submit form data without clicking a Submit button. (Or the flipside -- opening the user's email client with JavaScript attached to the Submit click?)

Upvotes: 0

Views: 291

Answers (2)

Condorcho
Condorcho

Reputation: 503

If what you want is to first open the email client, and then send automatically the email through the email client, that is not possible.

Otherwise, Niraj's answer suits well, but it will:

  1. Open the email client
  2. Submit the email
  3. Leave the email client opened

Upvotes: 0

Niraj Shah
Niraj Shah

Reputation: 15457

The easiest way to achieve this is to use a combination of JavaScript / jQuery and HTML. Create a link using a and add the onclick parameter to it, which can trigger the form submission:

Using jQuery:

<a href="mailto:[email protected]" onclick="$('#myform').submit();">Submit</a>

Using JavaScript:

<a href="mailto:[email protected]" onclick="document.getElementById('myform').submit();">Submit</a>

Upvotes: 1

Related Questions