Reputation: 61
I'm building a website with a simple login interface. The user can log in as one of 3 different users types, A B and C using 1 form. The code inside the form for handling this is:
<input type="radio" name="user_type" value="a" id="a">
<label for="a">A</label>
<input type="radio" name="user_type" value="b" id="b">
<label for="b">B</label>
<input type="radio" name="user_type" value="c" id="c">
<label for="c">C</label>
Depending on the user type, the login page should redirect the user to one of three PHP files on click of the form submit button. I know in PHP you can use
<form method="post" action="otherFile.php">
In the login page for leading to one other file. And use
<?php
if(isset($_POST['submit']))
{
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
echo $firstname . "<br/>";
echo $lastname;
}
?>
In otherFile.php to retrieve form data. But I want to do this for one of three PHP files, depending on the user's selection of user_type, and
<form method="post" action="otherFile.php|otherFile2.php|otherFile3.php">
clearly doesn't work.
Upvotes: 1
Views: 996
Reputation: 4378
You can use $_SESSION
to store data for use in other areas of your site.
To simplify it, you could do something like:
session_start();
$_SESSION['firstname'] = $_POST['firstname']; // store firstname, lastname as sessions
$_SESSION['lastname'] = $_POST['lastname'];
And then, inside of another page, you can call these sessions to output:
$firstname = $_SESSION['firstname'];
print("$firstname"); // will print the first name.
I hope this helps :)
EDIT: Before you create a $_SESSION
and try to print it into another page, create a core.php page and include
it into both page1.php & page2.php
Inside of your core.php script, you need to start up a session, like so:
session_start();
Once you have included the core.php file into both of your pages, try printing out the desired $_SESSION
variable again.
Upvotes: 1
Reputation: 292
Using javascript:
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script type="text/javascript">
jQuery("#myForm").submit(function( event ) {
var radio = jQuery('input[name=user_type]:checked', this).val();
if(radio == 'a')
this.action = 'otherFile.php';
else if(radio == 'b')
this.action = 'otherFile2.php';
else if(radio == 'c')
this.action = 'otherFile3.php';
else
return false;
});
</script>
You form:
<form method="post" action="" id="myForm">
<input type="radio" name="user_type" value="a" id="a">
<label for="a">A</label>
<input type="radio" name="user_type" value="b" id="b">
<label for="b">B</label>
<input type="radio" name="user_type" value="c" id="c">
<label for="c">C</label>
<input type="submit" value="Submit">
</form>
Upvotes: 0
Reputation: 106
I'm not sure if this is you want, but you can create a single php, and if is a value, require it using include or require functions.
For example, processor.php
<?
$user_type = $_POST["user_type"];
switch($user_type) {
case "a":
require_once 'otherFile.php';
break;
case "b":
require_once 'otherFile2.php';
break;
case "c":
require_once 'otherFile3.php';
break;
default:
die("Not allowed");
break;
}
?>
Upvotes: 0