Robin
Robin

Reputation: 2723

Send email to and then redirect in javascript

I have a website with a form in it. The form takes name, phone, email, company and a message.

I want it so that when the user clicks submit, an email is send to a predefined email account and that the user then is send to a new page.

The code that I have so far is this:

<form method="post" action="mailto:[email protected]">

This however just opens up the email provider prompt. This wouldn't be that bad, but I can't redirect the user after wards then.

Is there a way to do this with just javascript, or would it be easier to just use PHP?

Upvotes: 0

Views: 414

Answers (1)

noinstance
noinstance

Reputation: 761

You can't do this solely on client side javascript. You need to use a server language, such as PHP, to get the posted data and send it by email. You then have a choice, to redirect also on the server side or doing it client side.

Here's what your PHP code would look like (note I didn't test it):

// email the posted data
mail('[email protected]', 'New message from site',
   'Name: ' . $_POST['name'] .
   'Phone: ' . $_POST['phone'] .
   'Email: ' . $_POST['email'] .
   'Company: ' . $_POST['company'] .
   'Message: ' . $_POST['message']);

// redirect the visitor
header('redirect-url-here');

Upvotes: 2

Related Questions