tomarrell
tomarrell

Reputation: 155

HTML action="" attribute >> multiple actions

I am trying set a form submit to trigger my php mail script and redirect the user to another page/confirmation that their message has got through.

I have setup the action attribute currently with:

<form id="contactform" action="send.php" method="post">

And was wondering if there was a way to add a redirect to the form action on top of the mail script.

Thanks in advance!

Upvotes: 0

Views: 968

Answers (3)

Jon
Jon

Reputation: 12992

In your send.php file you could do this:

<?php
// Send the email
$sent = mail("[email protected]", "Subject", "Hello!"); 

if ($sent) {
    // This sends a header redirect to the client's browser
    header("Location: http://mysite.com/success.php");
    // This causes the redirect to happen immediately,
    // instead of continuing to process the page
    exit; 
} else {
    header("Location: http://mysite.com/failure.php");
    exit;
}
?>

Upvotes: 2

x4rf41
x4rf41

Reputation: 5337

you cant put it into the form but you can easily do the with php

header('Location: yourtarget.php');

if you want to be more dynamic you could add a GET parameter to the form action:

<form id="contactform" action="send.php?redirectto=yourtarget" method="post">

and then in php:

header('Location: '.$_GET['redirectto']);

Upvotes: -1

Quentin
Quentin

Reputation: 943217

No.

Any redirect would have to be initiated by the resource the action points to. (Using header() to return a Location response header).

(You could do something crazy like adding a dependancy on JavaScript, submitting the data using Ajax, then setting location if the request was successful, but that would be much more complex and much less reliable then having send.php issue a redirect response).

Upvotes: 0

Related Questions