Jump to content

Php SESSION Function with the html form


Muhammad_Bilal

Recommended Posts

Hi, guys!

i wan't to ask you how to set a session in our input fields, so the user can't type again and again if he done a single mistakes.For example, if the user, type his/her name and click on the submit button the value of his name is disappear, in our input field.how to create asession function with the help of PHP to keep this value.i can create a code for it, let tell me, what's mistake in it.Below is the all of the code of my page.<?phpsession_start();$_SESSION['name'];?><!doctype html> <html><head><title>php scripting</title></head><body><center> <form method="post" action="practice4.php">Name:<br /><input type="text" name="name" value=" <?php echo $_SESSION['name']; ?>" /><br /><input type="submit" name="submit" value="submit" /></form></center><?phpif(isset($_POST['submit'])){

$name=$_SESSION['name']=$_POST['name'];}

?></body></html>

Link to comment
Share on other sites

This is the fix for yours...

 

<?php
session_start();
?>
<!doctype html>
<html>
<head>
<title>php scripting</title>
</head>
<body>
<center>
<?php if(!isset($_SESSION['mistake'])): ?>
<form method="post" action="check.php">
Name:<br /><input type="text" name="name" value="<?php
if (isset($_SESSION['name'])) {
echo $_SESSION['name'];
} ?>" /><br />
<input type="submit" name="submit" value="submit" />
<?php endif; ?>
</form></center>
<p>If you see a blank page, it means you entered a wrong name </p>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$name = $_SESSION['name'] = $_POST['name'];
}
?>
And, this is for the check.php
<?php
/** If the mistake is when the user type 'mistake' in the form */
if ($_POST['name'] == 'mistake') {
session_start();
$_SESSION['mistake'] = 1;
}
header('Location: session.php');
I dont think that that's the best code but it works
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...