Forum Moderators: open

Message Too Old, No Replies

I can't retain form data

retain data

         

zhtway

9:52 pm on Dec 18, 2009 (gmt 0)

10+ Year Member



Hi,

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>

incrediBILL

10:03 pm on Dec 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



maybe this typo fix will solve the problem:

<input id="text" name="username" />

zhtway

10:08 pm on Dec 18, 2009 (gmt 0)

10+ Year Member



Hi incrediBill,

I have corrected the typo (username) now. But getting the same result.
any idea?
rgds,

incrediBILL

10:36 pm on Dec 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Insert this above your error echo:

echo $_POST[username];

You will see the content of username displayed, it's your logic that's wrong.

You immediately display an error if the form was posted, you don't check the form values passed before claiming they are invalid.

jecasc

5:10 pm on Dec 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually this code does not make much sense at all, I am afraid. It's full of logical and syntactical errors.

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?