Unable to store session data into next form - php

Currently I am on a question to store session and pass it to the next form, but as hard as I try to start session and store the variable inside the session, it does not work. Please shed some light on this, thanks!
MainForm.php
<?php
session_start();
require("inputValidation.php"); // This php file is just a file that does validation for my side
$validForm = true;
if ($_POST)
{
}
?>
<form class="form" action="nextform.php" role="form" method="post">
<input type="text" class="form-control" id="name" name="name" value= "<?php
if (isset($_POST['name']))
{
if (!validateRequired($_POST['name']))
{
$_SESSION['test'] = $_POST['name'];
$validForm = false;
}
if (validateRequired($_POST['name']))
{
$_SESSION['test'] = $_POST['name'];
$validForm = true;
}
}
?>
<input name="submit" type="submit" value="Submit" class="btn btn-primary">
nextform.php
print_r ($_SESSION);
The problem is even if i enter any value inside the textbox name, I will be just redirected straight to nextform.php without getting my session value. Why is this so? Is there any way I can get my session value without changing action="nextform.php"?
Thank you!
Sorry I am still new to sessions and PHP so bear with me :)

Basically what you wan't to do is move the whole
if (isset($_POST['name']))
{
//... you code
}
to the page where the form is submitted, in your case nextform.php.
Your pages should be something like:
MainForm.php
<form class="form" action="nextform.php" role="form" method="post">
<input type="text" class="form-control" id="name" name="name" value= "">
<input name="submit" type="submit" value="Submit" class="btn btn-primary">
nextform.php
<?php
session_start();
require("inputValidation.php"); // This php file is just a file that does validation for my side
$validForm = true;
if (isset($_POST['name']))
{
if (!validateRequired($_POST['name']))
{
$_SESSION['test'] = $_POST['name'];
$validForm = false;
}
if (validateRequired($_POST['name']))
{
$_SESSION['test'] = $_POST['name'];
$validForm = true;
}
}
if($validForm) {
echo "Validation success";
print_r ($_SESSION);
} else {
echo "Form validation failed.";
}
?>
Basically you can access the POST parameters in the page where the form is submitted, see action="nextform.php".
Here you can check if the form was submitted and do the proper validation, along with redirecting the user somewhere, or just showing the appropriate messages as needed, ie Validation failed or Validation success.

Related

My simple Form won't submit, to be used within PHP

I am writing a new PHP website, and have written a form, and the PHP code to handle the variables and the DB.
Trouble is, it won't even submit the form! Usually when you submit, if you hit F5 it will ask if you want to 'send again', it doesn't even do that. Something is clearly wrong, and I am a little rusty - but I thought I was ok with basic forms.
<?php
$username = isset($_POST['username']) ? $_POST['username'] : null;
echo "here: $username";
if(!isset($_SESSION['username'])) {
$_SESSION['username'] = $username;
} elseif (isset($_SESSION['username'])) {
$username = $_SESSION['username'];
}
?>
<form method="post" action="/signup2/" name="signuprocess">
<input type="text" name="username">
<input type="submit" value="Sign Up">
</form>
After submitting, the $username doesn't appear after "here:".
What I am doing that's a bit silly??
I have tried to post the code on here, but it sees it as spam, as it is a sign up form.
What can I post to show the code, without it being seen as such?
When you "hit" F5 and the browser asks you to 'send again' is because you previously did a POST request. Otherwise, if your last request was, for example, a GET, the browser is not going to ask you anything. It will simply refresh the page.
try something like this
<?php
$username = $_POST['username'] ?? null;
echo "here: $username";
?>
<form method="post" action="" name="signuprocess">
<input type="text" name="username" value="<?php echo $username ?>">
<input type="submit" value="Sign Up">
</form>

Storing form data in session with php

I'm testing a really basic PHP form, where the form data is saved in a session.
Later, i want that session data to be the default value of the form:
<strong>Test Form</strong>
<form action="" method"post">
<input type="text" name="var" value=<?php $name ?>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
$name = $_SESSION['var'];
}
echo $name;
?>
So, for example if i input "MyName" it should echo "MyName" and in the form there should be the value "MyName". The problem with the actual code is that it gives an E_NOTICE : type 8 -- Undefined variable: name -- at line 18 error. I think that the variable is not being stored, can someone help me out on this?
The first error I notice is this piece of code:
<form action="" method"post">
Where method does not contain the symbol "=" which causes the loss of the parameter post.
Furthermore, the "session_start ()" function must be placed before any other code. The code derives from this is as follows:
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
$name = $_SESSION['var'];
} else {
$name = null;
}
?>
<strong>Test Form</strong>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="var" value="<?= ($name != null) ? $name : ''; ?>">
<input type="submit" name="Submit" value="Submit!" />
</form>
Spotted a few things:
I'd put the session_start(); at the top of the page before outputting anything.
Your method was incorrectly written it needs a '=' when specifying the method - thats the main reason why nothing was being stored, the form wasn't submitting properly.
Same with how you've put in the value on the name input - it has no '=' and you don't close the input tag properly - I've left it blank and added a placeholder - you can change it to what you need.
Heres how I'd do it:
<?php session_start(); ?>
<strong>Test Form</strong>
<form action="" method="post">
<input type="text" name="var" value="" placeholder="enter name">
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
}
// Store the session in a variable after the submit - otherwise it will be forgotten on refresh
$name = $_SESSION['var'];
// check if session exists
if(isset($name)) {
echo $name;
}
else {
echo 'no name entered...';
}
?>
You can edit the above to hide the form if a name has been submitted etc. Use session_destroy(); to reset the stored session.
Cheers

php prevent form resubmission using sessions

So I am trying to prevent form resubmission using sessions
and this is my code :
<?php
session_start();
if(isset($_GET['unid']))
{
if ($_GET['unid']==$_SESSION["uid"])
{
echo "Form is Submited do something";
}
else
{
echo "no you can't do that";
}
}
$unid = md5(uniqid());
$_SESSION["uid"] = $unid;
?>
<form method="GET">
<input name="name" value="test">
<input name="unid" value="<?php echo $unid;?>">
<input type="submit">
and it works ...but if the user opens another tab then it will break so how can I fix it ?
I'm not sure about this but may be assigning a new global session variable will work, say $_SESSION['checkSub']. So once the form is submitted, set it to 1 or true and only let form submission if it isn't 1 or false.
You can check to see if the unid is set in the session before generating a new unique id. See updated code below:
<?php
session_start();
if(isset($_GET['unid']))
{
if (isset($_SESSION['unid']) && $_GET['unid']==$_SESSION['unid'])
{
//form has been submitted with matching unid - process it as needed
unset($_SESSION['unid']);
}
else
{
// Form was resubmitted or some other logic error
}
}
$unid = ''; //declare here for good scope
if (isset($_SESSION["unid"])) {
$unid = $_SESSION["unid"];
}
else {
$unid = md5(uniqid());
$_SESSION["unid"] = $unid;
}
?>
<form method="GET">
<input name="name" value="test">
<input name="unid" value="<?php echo $unid;?>">
<input type="submit">
Rather than using form method GET, try to use POST. It will work for you. The $_POST array will only have data in it when form is submitted, so you should not have to use the session to know whether form is submitted or not.

Login Page Username and Password cannot be posted (PHP)

Username and password not appear on Page 2.PHP although I post it to Page2.PHP
Page1.PHP
<form name="form1" method="post" action="Page2.php">
<input type="text" name="txtLogin">
<input type="password" name="txtPWD">
<input type="submit" name="btnSub" value="go">
</form>
Page2.PHP
<?php
if(isset($_REQUEST['txtLogin']))
{
session_start();
$_SESSION['login']=$login;
}
if(isset($_SESSION['login']))
header('Location: detail.php');
else
header('Location: index.html');
?>
put this on page2.php
if(isset($_POST['txtLogin']) && isset($_POST['txtPWD']))
{
//get values & do other scripts like saving values on sessions
$user = $_POST['txtLogin'];
$pass = $_POST['txtPWD'];
echo $user.'<br>'.$pass;
}
else
{
//event here
}
The problem is here:
$_SESSION['login']=$login;
You are using the $login variable, but it isn't actually being set anywhere.
A few lines further up, we see that the login name is actually in $_REQUEST['txtLogin'], not $login. So you should be using that.
$_SESSION['login']=$_REQUEST['txtLogin'];
Hope that helps.
Check settings: enable_post_data_reading, request_order, variables_order, gpc_order on http://www.php.net/manual/en/ini.core.php

php form validation issues - cannot print to header

I have a php form that saves the info to my database and sends an email upon completion. however it will not validate the fields to see if they are null, instead it prints both the set and not set options. Any ideas as to why this could be happening? It worked perfectly before i added the form field validation to it.
As a side note it works in FF and Chrome due to the html 5 aria-required, but not in IE
html
<form id="contact" name="contact" action="register1.php" method="post">
<label for='Cname'>Camper Name</label>
<input type="text" name="Cname" maxlength="50" value="" required aria-required=true />
<input type="hidden" id="action" name="action" value="submitform" />
<input type="submit" id="submit" name="submit" value="Continue to Camp Selction"/>
</form>
php
<?php
//include the connection file
require_once('connection.php');
//save the data on the DB and send the email
if(isset($_POST['action']) && $_POST['action'] == 'submitform')
{
//recieve the variables
$Cname = $_POST['Cname'];
//form validation (this is where it all breaks)
if (isset($Cname)) {
echo "This var is set so I will print.";
}
else {
echo '<script type="text/javascript">alert("please enter the required fields");</script>';
}
//save the data on the DB (this part works fine)
<?php
$Cname = isset($_POST['Cname']) ? $_POST['Cname'] : null;
if (isset($Cname)) {
echo "This var is set so I will print.";
}
// OR
if (isset($_POST['Cname'])) {
// Perform your database action here...
}
?>
Consider using PHP's empty function
PHP.Net Manual Empty()
You can update your code to the following:
if(!empty($Cname)) {
echo "This var is set so I will print.";
}
Do you just need an "exit()" in the else?

Categories