Chaya Cooper
Chaya Cooper

Reputation: 2530

Redirect User based on link they've clicked

Is there a way to specify the header location based on which link the user has clicked? If possible, I'd prefer doing this in PHP.

I'd like to submit forms when the user clicks on any of a few buttons (i.e. Next, Back, Save, etc.), and they each need to redirect the user differently once the form is submitted.

Example

HTML - form2.html

<form name="form2" id="form2" method="post" action="form2-exec.php">        
    <!-- Form Elements -->       
    <a href="form1.php"><input type="submit" value="BACK" /></a>
    <a href="form3.php"><input type="submit" value="SUBMIT" /></a>
</form>

PHP - form2-exec.php

// Connect to database
// Insert & ON DUPLICATE KEY UPDATE Statements
header("location: form3.php");

Upvotes: 2

Views: 157

Answers (2)

Gardner Bickford
Gardner Bickford

Reputation: 1963

You do not need the anchor tags. Can you try something like:

<input type="submit" name="whereto" value="BACK" />
<input type="submit" name="whereto" value="SUBMIT" />

PHP - form2-exec.php

// Connect to database
// Insert & ON DUPLICATE KEY UPDATE Statements
if ($_GET['whereto'] == 'BACK') {
    header("location: form1.php");
} elseif ($_GET['whereto'] == 'SUBMIT') {
    header("location: form3.php");
}

You can find out more about PHP predefined variables at http://php.net/manual/en/reserved.variables.php

Upvotes: 5

Nick Andriopoulos
Nick Andriopoulos

Reputation: 10643

you can simply use $_POST['submit'] which will contain the value as a value. Use the name attribute on your submit buttons as well to make it sure.

Upvotes: 0

Related Questions