How to handle multiple php forms using one php page - php

I'm a student and I know nothing about PHP. but I have to do one of my assignment using PHP.
Here is the problem which I faced.
On my index page, there are 3 links that direct to 3 different forms. when the user chooses one form, then fill and submit it result.php file shows the output using values that the user enters in the form.
all the 3 forms should germinate its result using the same result.php file.
I cannot figure out how to generate the result page by identifying which form the user selects.
Here is my code,
form1.php
<!DOCTYPE html>
<html>
<head>
<title>PHP form handling</title>
</head>
<body>
<form name="form1" action="result.php" method="post">
<label for="pullDownMenu">Title</label>
<select name="pullDownMenu" id="pullDownMenu" size="1">
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Mrs">Mrs</option>
<option value="Rev">Rev</option>
</select>
<p>Name: <input type="text" name="firstname" value="" /></p>
<p>Reg No: <input type="text" name="lastname" value="" /></p>
<p>Email Addr: <input type="text" name="Email" value="" /></p>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
form2.php
<!DOCTYPE html>
<html>
<head>
<title>form 2</title>
</head>
<body>
<form name="form2" action="result.php" method="post">
<p>Registrationa no: <input type="text" name="RegNO" value="" /></p>
<p>NIC number <input type="text" name="NIC" value=""></p>
<p>Telephone number: <input type="text" name="Telephone" value="" /></p>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
I tried the result.php file, but it didn't work. here is the result.php
<!DOCTYPE html>
<html>
<head>
<title>PHP demo</title>
</head>
<body>
<?php
if(!empty($_POST['form1'])){
$title=$_POST['pullDownMenu'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$Email = $_POST['Email'];
echo"<h1>student information</h1>";
echo'title is : ' . $title . '</br>';
echo 'first name is : '. $firstname . '</br>';
echo 'lastname is : '.$lastname;
}
if (!empty($_POST['form2'])) {
$regNo = $_POST['RegNO'];
$NIC = $_POST['NIC'];
$tel = $_POST['Telephone'];
echo "<p>Following details are saved to database</p>";
echo 'reg No\t:\t' . $regNo. '</br>';
echo 'NIC\t:\t' . $NIC. '</br>';
echo 'Tel No\t:\t' . $tel. '</br>';
}
?>
</body>
</html>

Consider using isset() to check for a specific variable. It can be better then checking with empty().
<!DOCTYPE html>
<html>
<head>
<title>PHP demo</title>
</head>
<body>
<?php
if(isset($_POST['form1'])){
echo "<h1>student information</h1>\r\n";
echo "title is : $_POST['pullDownMenu']<br />\r\n";
echo "first name is : $_POST['firstname']<br />\r\n";
echo "lastname is : $_POST['lastname']\r\n";
}
if (isset($_POST['form2'])) {
echo "<p>Following details are saved to database</p>\r\n";
echo "reg No\t:\t$_POST['RegNO']<br />\r\n";
echo "NIC\t:\t$_POST['NIC']<br />\r\n";
echo "Tel No\t:\t$_POST['Telephone']<br />\r\n";
}
?>
</body>
</html>
If you have more forms, consider using switch() instead of if().

You could set different values for button submit for each form. Or you could use a input tag hidden to set type of form. Example:
Form1 : <input type="hidden" name="type" value="form1">
Form2 : <input type="hidden" name="type" value="form2">
And in php form
if($_POST["type"]=="form1")
{
//code here
}else if($_POST["type"]=="form2"){
//code here
}

Related

PHP sessions not working on multiple page form when going back to the first page

I don't get the PHP $_SESSION working when I try to echo the result on the first page. The error message states basically that the variables are undefined.
The path i want to use:
1st page = form 1
2nd page = form 2
Go back to 1st page = form 1 with all input filled from previous submit + all data from the 2 forms in a text.
Is that possible ?
Page 1 = index.php:
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<form action="overview.php" id="regForm" method="post">
<div class="tab">
<h2>1. Your info</h2>
<p><input type="text" placeholder="Prénom" name="fname" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
<p><input type="text" placeholder="Nom" name="name" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
<input type="submit" value="Valider">
</div>
</form>
<?php echo "Your name is :",$_SESSION['name'], "and your first-name is ", $_SESSION['fname'];?>
<?php echo "Your e-mail is :", $_SESSION['email'] ;?>
</body>
</html>
Page 2 = overview.php:
<?php session_start(); ?>
<?php
$_SESSION['fname'] = $_POST['fname'];
$_SESSION['name'] = $_POST['name'];
?>
<form action="index.php" id="regForm" method="post">
<h2>1. Tes informations personnelles</h2>
<p><input type="text" placeholder="e-mail" name="email"></p>
<input type="submit" value="Valider">
</form>
Back to Page 1 = index.php:
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<form action="overview.php" id="regForm" method="post">
<div class="tab">
<h2>1. Your info</h2>
<p><input type="text" placeholder="Prénom" name="fname" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
<p><input type="text" placeholder="Nom" name="name" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
<input type="submit" value="Valider">
</div>
</form>
<?php echo "Your name is :",$_SESSION['name'], "and your first-name is ", $_SESSION['fname'];?>
<?php echo "Your e-mail is :", $_SESSION['email'] ;?>
</body>
</html>
Do you guys see any issue that prevents the code to run ?
Just found out the "WHY".
I basically blocked cookies when I started the project which prevent $_sessions to work properly.
So make guys sure that cookies are enabled on your website. The kind of things that waste an hour of work ...

Cookies function not saving the Data

This code is intended to just take an ID and Pass without any kind of authentication and remember the data if the checkbox were to be checked. I can not figure it why is it not saving the data to cookies.
<?php
if(isset($_POST["chk"],$_POST["id"],$_POST["pass"])) {
$id=$_POST["id"];
$pwd=$_POST["pass"];
if (isset($_POST["chkbx"])){
setcookie("id","$id",time()+3600);
setcookie("pwd","$pwd",time()+3600);
$id=$_COOKIE["id"];
$pwd=$_COOKIE["pwd"];
}
print "Your ID " . $id;
print "Your PASS ". $pwd;
}
?>
<html>
<head>
<title>
Remember Me
</title>
</head>
<body>
Please Enter ID and PASS
<form method="post" >
Enter ID
<input type="text" name="id" />
Enter PASS
<input type="text" name="pass" />
<input type="submit" value="submit" /><br>
<input type="checkbox" name="chkbx" />Remember Me
<input type="hidden" name="chk" value="true" />
</form>
</body>
</html>
You code is correct but it needs to clean it up
in this part I add a condition to check is there is anything first in the $_COOKIE
before print it
if(isset($_COOKIE['id']) && isset($_COOKIE['pwd'])){
print "Your ID: " . $_COOKIE['id'] . '<br>';
print "Your PASS: ". $_COOKIE['pwd'] . '<br>';
}
your code will be like this
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$id = $_POST["id"];
$pwd = $_POST["pass"];
if (isset($_POST["chkbx"])){
setcookie("id", $id ,time()+3600);
setcookie("pwd", $pwd, time()+3600);
}
}
if(isset($_COOKIE['id']) && isset($_COOKIE['pwd'])){
print "Your ID: " . $_COOKIE['id'] . '<br>';
print "Your PASS: ". $_COOKIE['pwd'] . '<br>';
}
?>
<html>
<head>
<title>
Remember Me
</title>
</head>
<body>
Please Enter ID and PASS
<form method="post" >
Enter ID
<input type="text" name="id" value="<?= isset($_COOKIE['id'])? $_COOKIE['id']: '' ?>" />
Enter PASS
<input type="text" name="pass" value="<?= isset($_COOKIE['pwd'])? $_COOKIE['pwd']: '' ?>" />
<input type="submit" value="submit" /><br>
<input type="checkbox" name="chkbx" />Remember Me
<input type="hidden" name="chk" value="true" />
</form>
</body>
</html>

Display text after submit button is clicked. PHP

I am trying to catch some text from the textbox, to save it in a variable and then post it on my browser if I click the submit button. My code is this:
<?php
$var1=$_POST['text1'];
$var2=$_POST['display'];
if(isset($var2)){
var_dump( $var1);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</body>
</html>
I don't know what is wrong. The code on rextester
For starters you need to implement a form on you HTML page and set the action to a new .php page.
<form action='display.php' method='post'>
*your input fields go here*
Now create a new php page (in this case display.php)
Declare the variables as you did....
$var1=$_POST['text1'];
$var2=$_POST['display'];
if(isset($var2)){
var_dump( $var1);
then you can simply echo each variable accordingly...
Final Result
HTML PAGE:
<form action='result.php' method='post'>
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</form>
result.php
<?php
$var1 = $_POST['text1']; // create variables of your form elements
$var2 = $_POST['display'];
echo '$var1 . ' ' . $display . '.';
?>
Basically the php page is saying get text1 and display from the form (names) and create variables of them...
Then echo (print) these variables on the screen. (in plain english xD)
p.s
Your rextester isn't working because you haven't specified a form.
You need to setup the FORM tags so they have the correct attributes for POST. Try:
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</form>
</body>
</html>
You need to use a form, otherwise you aren't posting anything.
Something like:
form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form method="post" action="yourphpfile.php">
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="text" value="display" name="display" id="display" /><br /><br />
<input type="submit" value="submit"><br /><br />
</form>
</body>
</html>
yourphpfile.php
<?php
if(!empty($_POST['text1']) and !empty($_POST['display'])){
$var1=$_POST['text1'];
$var2=$_POST['display'];
echo "$var1 $var2";
}
?>
Check this complete example
To display text box and button on clicking submit button
<form>
<input type="submit" value="OK" name="btn_name"/><br />
<?php
if(isset($_GET['btn_name']))
{
echo "<input type=text name=txt_userinput />";
echo "<input type=submit value=Area name='btn_calculate'/>";
}
?>
</form>

Invoke Combobox from PHP in HTML

I have a PHP file that parses a JSON list and feeds it to a combobox:
<?php
$jsonData = '{"marco":"marco#test.it", "giovanni":"giovanni#mail.it"}';
$json = json_decode($jsonData, true);
$opts = '';
foreach($json as $name => $email)
{
$opts .= '<option value="'.$email.'">'.$name.'</option>';
}
echo ' <select name="Team1">'.$opts.'</select> <br> ';
echo ' <select name="Team2">'.$opts.'</select> <br>';
?>
I'm trying to include it in an HTML page, so that when it loads it will show the combobox:
<html>
<head>
<title> Invio Mail </title>
</head>
<body>
<form name="mail" action="mailer.php" method="post">
<p>
<center><b> Invio mail </b>
<br>
<br>
<? include("combobox.php") ?><br>
Messaggio: <input type="text" name="name" rows="5" required><br>
<br>
<center><input type="submit" value=" Invia "></center><br>
</form>
</body>
</html>
But I can't manage to make them appear!
I don't need a real time update, it's enough that the list is updated when the page is created.
Also, I can't manage to read the associated value from the combobox.
Make sure that the HTML page has the .php extension instead of .html and check if your webserver supports short open tag for php.
Edit:
You can check the phpinfo() if short_open_tag has the value on.
Edit 2:
You can access the selected value in your mailer.php with $_POST['Team1'] and $_POST['Team2']
Edit:
<? include("combobox.php") ?>
to
<?php include("combobox.php") ?>
So, html code will be:
<html>
<head>
<title> Invio Mail </title>
</head>
<body>
<form name="mail" action="mailer.php" method="post">
<p>
<center><b> Invio mail </b>
<br>
<br>
<?php include("combobox.php") ?><br>
Messaggio: <input type="text" name="name" rows="5" required><br>
<br>
<center><input type="submit" value=" Invia "></center><br>
</form>
</body>
</html>

How process two forms in 1 PHP file?

I've got 1 single PHP file, which contains 2 variables... what I want to achieve is this, first get the first name from the user, and store in in variable $name, then show a form to ask the user for his lastname, THEN.. print both variables, the thing is, when the second form is submitted the first variable disappears, is there a way to keep it in memory?
<?php
$title = rand(100, 300);
?>
<!doctype html>
<html>
<head>
<?php
//functions to call
function nameform() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="name"><br>
<input type="submit" value="name">
<input type="hidden" name="val1">
</form></center>
<?php
}
function lastname() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="lastname"><br>
<input type="submit" value="lastname">
<input type="hidden" name="val2">
</form></center>
<?php
}
?>
<meta charset="utf-8">
<title><?php
echo ("$title"); ?></title>
</head>
<body>
<?php
if(!isset($_POST['name'])){
nameform();
}
if (isset($_POST['name'])) {
$name = $_POST['name'];
lastname();
}
if (isset($_POST['lastname'])) {
$lastname = $_POST['lastname'];
echo "Your name is $name and your last name is $lastname";
}
?>
</form>
</body>
</html>
How about waiting until the form is complete to submit it? I would use a little javascript to take care of this.
<?php
$title = rand(100, 300);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title><?php
echo ("$title"); ?></title>
</head>
<body>
<center>
<?php
if (isset($_POST)) {
$name = $_POST['name'];
$lastname = $_POST['lastname'];
echo "Your name is $name and your last name is $lastname";
} else {
?>
<form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<div id="firstname">
<input type="text" autofocus name="name"><br>
<a href="javascript:;"
onclick="document.getElementById('firstname').style.visibility='hidden';document.getElementById('lastname').style.visibility='visible';">Next</a>
</div>
<div id="lastname" style="display:none;">
<input type="text" name="lastname"><br>
<input type="submit" value="lastname">
</div>
</form>
<?php } ?>
</center>
</body>
</html>
Just set the correct info in your input type hidden, when your second form is send you can get the name value in $_POST['val2'].
See the example:
<?php
$title = rand(100, 300);
?>
<!doctype html>
<html>
<head>
<?php
//functions to call
function nameform() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="name"><br>
<input type="submit" value="name">
<input type="hidden" name="val1">
</form></center>
<?php
}
function lastname() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="lastname"><br>
<input type="submit" value="lastname">
<input type="hidden" name="val2" value="<?php echo $_POST['name']; ?>">
</form></center>
<?php
}
?>
<meta charset="utf-8">
<title><?php
echo ("$title"); ?></title>
</head>
<body>
<?php
if(!isset($_POST['name'])){
nameform();
}
if (isset($_POST['name'])) {
$name = $_POST['name'];
lastname();
}
if (isset($_POST['lastname'])) {
$lastname = $_POST['lastname'];
/*
Here you can get the name that come from the input type hidden named "val2"
*/
$name = $_POST['val2'];
echo "Your name is $name and your last name is $lastname";
}
?>
</form>
</body>
</html>
Or as sjagr says you can use SESSIONS, take a look to this example:
How preserve my inputs values each time that I refresh my page?

Categories