moosefoot
moosefoot

Reputation: 143

HTML/PHP: Form data for multiple redirections

I have made a PHP script that retrieves a username from a text field, and confirms it using three different buttons, where each button redirects to a different page.

A fellow user gave a solution, it an HTML form with the textfield and three buttons, then an if statement in PHP to determine which button was pressed and it will do the appropriate redirection.

There is a problem, however. This method doesn't seem to pass any form data...

Code:

<!-- Current solution for redirection does NOT pass form data. -->
<?php
echo "<div style = 'font:33px/54px Arial'> StreamViewer</div>";
echo "Please enter your username. If you are not registered, submit and click the add/remove author button once redirected.";

if(isset($_POST['button1']))
{
    header('location: page1.php');
}
elseif(isset($_POST['button2']))
{
    header('location: page2.php');
}
elseif(isset($_POST['button3']))
{
    header('location: page3.php');
}
?>

<br><br>

<form action = "" method = "POST">
Username: <input type = "text" name = "username" value = "">
<br>
<button type = "Submit" name = "button1" value = "b1">b1</button>
<button type = "Submit" name = "button2" value = "b2">b2</button>
<button type = "Submit" name = "button3" value = "b3">b3</button>
</form>

Here is some code from one of the pages that is not receiving the form data:

$username =  $_POST['username'];
echo "Hello $username";

The output for this page is simply "Hello ".

Upvotes: 2

Views: 56

Answers (2)

BNeumann
BNeumann

Reputation: 76

I'm still kind of a beginner myself, but I'd use $_SESSION variables. So it would look something like this:

<!-- Current solution for redirection does NOT pass form data. -->
<?php
session_start(); //be sure to include this line at the beginning of any pages references session variables too
echo "<div style = 'font:33px/54px Arial'> StreamViewer</div>";
echo "Please enter your username. If you are not registered, submit and  click the add/remove author button once redirected.";

if(isset($_POST['button1']))
{
$_SESSION['username'] = $_REQUEST['button1'];
header('location: page1.php');
}
elseif(isset($_POST['button2']))
{
$_SESSION['username'] = $_REQUEST['button2'];
header('location: page2.php');
}
elseif(isset($_POST['button3']))
{
$_SESSION['username'] = $_REQUEST['button3'];
header('location: page3.php');
}
?>

<br><br>

<form action = "" method = "POST">
Username: <input type = "text" name = "username" value = "">
<br>
<button type = "Submit" name = "button1" value = "b1">b1</button>
<button type = "Submit" name = "button2" value = "b2">b2</button>
<button type = "Submit" name = "button3" value = "b3">b3</button>
</form>

Then on the page receiving the data, do something like this: $username = $_SESSION['username']; echo "Hello $username";

Upvotes: 0

Beny Hdez
Beny Hdez

Reputation: 92

This is because when you redirect to the page1 POST is empty again. you are using POST to the php with the IF but when you use redirect into the if, post goes empty. Example of your flow : FORM POST > CATCH PHP (Post Catched) > IF, Redirect wihout POST > PAGE1

you need to resend the info by POST into the IF's or use Session Variables, etc.

Upvotes: 1

Related Questions