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?
Related
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 ...
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
}
This is really weird because all of my other email activation form is working when I include the !isset function but not for this file.
I even tried to echo all the get variables and everything seems to be working fine...
<?php
include_once __DIR__ .'/../header2.php';
if (!isset($_GET['email']) || !isset($_GET['userid']) || !isset($_GET['id']) || !isset($_GET['reply_registration_expirydate'])) {
echo "<meta http-equiv='refresh' content='0;url=../index.php?activate=missinglink'>";
exit();
} else {
include_once __DIR__.'/dbh.php';
$reply = 0;
$email = strip_tags($_GET['email']);
$username = strip_tags($_GET['userid']);
$userid = strip_tags($_GET['id']);
$reply_registration_expirydate = strip_tags($_GET['reply_registration_expirydate']);
if (empty($_SESSION['key'])) {
$_SESSION['key'] = base64_encode(openssl_random_pseudo_bytes(32));
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link href="https://fonts.googleapis.com/css?family=Lobster"
rel="stylesheet">
<link rel="stylesheet"
type="text/css"
href="style.css">
</head>
<body>
<div class="reply_registration">
<h2>Reply Registration against spam</h2>
</div>
<div class="reply_registration">
<form class="signup-form" action="../reply_registration_form.php" method="POST">
<label>Why do you want to join PianoCourse101?</label>
<br></br>
<textarea name="joined" placeholder="Why do you want to join PianoCourse101?"></textarea>
<br></br>
<input type="hidden" name="username" value="<?php echo htmlspecialchars($username); ?>">
<br></br>
<input type="hidden" name="email" value="<?php echo htmlspecialchars($email); ?>">
<br></br>
<input type="hidden" name="expirydate" value="<?php echo htmlspecialchars($reply_registration_expirydate); ?>">
<br></br>
<input type="hidden" name="csrf" value="<?php echo $_SESSION['key']; ?>">
<button type="submit" name="submit">Submit Reply Registration</button>
</form>
I'm stuck at one point .I have a php form and after filling all details, user have to press submit button . If data does not submitted properly then I am reloading the page .
so My question is .
I want to set all previous values entered by user into respected
fields without request method.
how can i achieve this?
What I have done so far
My user.php
<?php
$name = '';
if(isset($_REQUEST[name]))
{
$name = $_REQUEST[name];
}
if(isset($_POST["submit"]))
{
$name = $_POST["txtname"];
header('Location:user.php?name=<?php echo $name; ?>');
}
?>
<hrml>
<body>
<form name="form1" id="form1" method="POST">
<label>Name:</label>
<input type="text" name="txtname" id="txtname" value="<?php echo $name; ?>">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
EDIT
apply_now.php
<?php
$CLASS__BL = 'apply_now_bl';
require_once('apply_now_bl.php');
?>
<form name="form1" id="form1" action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="input-label">Full Name: *</label>
<input name="txtname" id="txtname" type="text" class="form-control" value="<?= #$page_bl->full_name; ?>">
</div>
</form>
apply_now_bl.php
<?php
require_once('admin/db_lib/candidates.php');
require_once('includes/general_include.php');
$page_bl = apply_now_bl();
page_load();
class apply_now_bl
{
var $full_name = '';
function page_load()
{
$this->obj_candidates = new candidates();
if(isset($_POST["submit"]))
{
$this->submit_data();
}
}
function submit_data()
{
$this->full_name = get_post_val("txtname");
$this->obj_candidates->full_name = $this->full_name;
redirect(apply_now.php); //common function for page redirect
}
}
No need to redirect through php header location . because action is blank and that means it will redirect to user.php(current page) once form submit.
so below is the updated code
<?php
$name = "";
if (isset($_POST["submit"])) {
$name = $_POST["txtname"];
}
?>
<html>
<body>
<form name="form1" id="form1" method="POST">
<label>Name:</label>
<input type="text" name="txtname" id="txtname" value="<?php echo $name; ?>">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
I have two .php files as such:
test1.php
<html>
<head>
<title>Sample</title>
</head>
<body>
<form action="test2.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
</body>
</html>
test2.php
<html>
<head>
<title>Sample Display Page</title>
</head>
<body>
<?php
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
?>
</body>
</html>
I want to know how can I get it all to display on a single page, i.e. on the test1.php, without having to have two files.
Check if $_POST["userNumber"] isset, and echo the form if it's not.
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if(isset($_POST["userNumber"]){
echo "You have chosen ".$_POST["userNumber"];
}else{
echo '<form action="test1.php" method="post">';
echo 'Please enter a number <input type="number" name="userNumber"><br>';
echo '<input type="submit">';
echo '</form>';
}
?>
</body>
</html>
Your test1.php will need to look like this
<html>
<head>
<title>Sample</title>
</head>
<body>
<form action="test1.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
<?php
if(isset($_POST["userNumber"])) {
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
}
?>
</body>
</html>
You simply need to remove the action="test2.php" section and combine both pages into one just like the following:
<html>
<head>
<title>Sample</title>
</head>
<body>
<form method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
<?php
if(isset($_POST['userNumber'])){
$user_number = $_POST['userNumber'];
echo "You have chosen $user_number";
}
?>
</body>
</html>
Pretty simple actually:
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if(isset($_POST) && isset($_POST['userNumber'])) {
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
}
?>
<form method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
</body>
</html>
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if (isset($_POST['submitted'])) {
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
}
else {
?>
<form method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit" name="submitted">
</form>
<?php
}
?>
</body>
</html>
Just use isset() for submit button, note that i added name="submit" to your button since it was missing
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if(isset($_POST['submit'])
{
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
} else {
?>
<form action="test2.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit" name="submit">
</form>
<?php } ?>
</body>
</html>
This one is fully tested and allows you to revise the value after submission. You still have to worry about cross-site scripting attacks, etc... but that is outside of the scope of the question.
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
$user_number = '';
if (isset($_POST['userNumber'])) {
$user_number=$_POST['userNumber'];
?>
<p>You have chosen <?php echo $user_number ?></p>
<?php
}
?>
<form method="post">
Please enter a number <input type="number" name="userNumber" value="<?php echo $user_number ?>"><br>
<input type="submit">
</form>
</body>
</html>
set the variables up before the form as placeholders and check for isset
<?php
$user_number = '';
if(isset($_POST["userNumber"])) {
$user_number = $_POST["userNumber"];
}
?>
<form action="test2.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit" name="submit">
</form>
<p>You have chosen: <?php echo $user_number ?></p>
Sample
<form action="<?php $_PHP_SELF ?>" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
</body> </html>