Jump to content

How can I save form input on submit?

- - - - -

  • Please log in to reply
4 replies to this topic

#1
system32

system32

    Newbie

  • Members
  • PipPip
  • 24 posts
I have a simple form where I want to validate the input. When the submit button is pressed it validates the form and if there are any errors shows them, but on my form all input is lost. How can I keep the input if there are errors shown? This is my code:

<?php
    //$SESSION = "";
    
    session_start();
    
    if ($_POST)
    {
        $_SESSION['POST'] = $_POST;
    }

    if (isset($_POST['submit'])) 
    {
        $error = "";
        
        if ($_REQUEST['UserName'] == NULL) 
        {
            $error .= "Please Enter a User Name";
        }
        if ($_REQUEST['firstName'] == NULL)
        {
            $error .= "Please enter a first name";
        }
        
        if (!empty($error))
        {
        echo '<p class="error"><strong>Your message was NOT sent<br/> The following error(s) returned:</strong><br/>' . $error . '</p>';
        }
        else
        {
            echo "Valid";
            session_unset();
        }
    }
?>
<html>
    <head><title></title>
    </head>
        <body>
            <form method="post" action="myForm.php">
            User Name : <input name="UserName" type="text" id="UserName" size="20" maxlength="30"/>
            First Name: <input name="firstName" type="text" id="firstName" value="<?php echo $_SESSION['firstName'] ?>" />
            <input type="submit" name="submit" value="Submit">
            </form>
        </body>
</html>

Thanks

#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
You do not need to use $_SESSION variable for this
use this
First Name: <input name="firstName" type="text" id="firstName" value="<?php echo $_POST['firstName'] ?>" />
This should work

But you should always sanitize your data before reposting it, if someone post 'hello " world' it could break your code

#3
system32

system32

    Newbie

  • Members
  • PipPip
  • 24 posts
Thank you. That worked. However I get this PHP message when I first go to the page:

Notice: Undefined index: firstName in C:\myForm.php on line [I]34[/I] Call Stack #TimeMemoryFunctionLocation 10.0002673728{main}(  )..\myForm.php[B]:[/B]0  " />

Its talking about this line:
<input name="firstName" type="text" id="firstName" value="<?php echo $_POST['firstName'] ?>" />

Any idea?

Thanks

#4
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Before the post, the variable firstName dosen't exist in post, my bad
try something like


<?php

$firstName = '';

if (isset($_POST['firstName']))

  $firstName = $_POST['firstName']

?>

<input name="firstName" type="text" id="firstName" value="<?php echo $firstName ?>" />




#5
system32

system32

    Newbie

  • Members
  • PipPip
  • 24 posts
Thank you very much!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users