Muhammad_Bilal Posted August 13, 2013 Share Posted August 13, 2013 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 More sharing options...
terryds Posted August 13, 2013 Share Posted August 13, 2013 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 1 Link to comment Share on other sites More sharing options...
justsomeguy Posted August 13, 2013 Share Posted August 13, 2013 There's not really a point to using the session for this, you already have the data in $_POST. <input type="text" name="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" /> 1 Link to comment Share on other sites More sharing options...
Muhammad_Bilal Posted August 14, 2013 Author Share Posted August 14, 2013 hello! i'll be very thankful to Terryds, Justsomeguy to give me the reply on my topic.that's great! All the code works very fine.Thank's again, for your reply, now my page works fine and displays no error. that's just because you.i'll appriciat you for your assistant thank's. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now