Forum Moderators: open
I am very very new to web programming and I want to retain data once the form is submitted. I tried other says but no luck. Could someone point it out for me please?
Below is the code:
<?php
session_start(); // Start the session where the code will be stored.
?>
<html>
<head>
<title>Securimage Test Form</title>
</head>
<body>
<?php
if (empty($_POST)) { ?>
<form method="POST" >
<strong>Name:</Strong><br />
<input id="text" name="usname" />
Code:<br />
<input type="text" name="code" size="12" /><br /><br />
<input type="submit" value="Submit Form" />
</form>
<?php
} else { //form is posted
echo "<center>Sorry, the code you entered was invalid. <a href=\"javascript:history.go(-1)\">Go back</a> to try again.</center>";
if (isset($_POST[username])) return $_POST[username];
}
?>
</body>
</html>
First you start a session. But you do not store any data in session variables.
You start a form, but the "action" attribute is missing. <form action="" method="POST"> </form>
When someone submits data, all you do is display an error message, that the code was invalid.
What is this: <input id="text" name="usname" />
Doesnt't make sense at all. Do you want to create a textfield for a username? This should be <input type="text" name="username" />. Not "id".
Then - what does - return $_POST[username] - mean? The return statement is used to end execution of a current function, and to return an argument in a function. But there is no function here.
Perhaps we could help you better if you told us what you want to do with the $_POST data. Do you want to store it in a session variable?
$_SESSION['code'] = $_POST['code']
or in a file or in a database or display it on the screen?