Could somebody please respond to my post. I really need help on this.
Thanks.
Ok let me elaborate further on this. The first piece of code, is pretty much solid. Not tested but in theory everything should work. The second piece of code took out a lot of things 1. validation of the data being input 2. the mysql stuff.
If you notice in the 2nd piece of code I put in a comment that says [color=#808080]
//insert data into mysql database.[/color]
I left the mysql stuff to you, because in the initial example I posted it was hard to read with all the other stuff.
Here is a more detailed explanation.
USER EXPERIENCE
These are the steps that the user will go through.
1. The user will go to www.example.com/myform.php
2. User will enter the data.
3. User will press the submit button
4. Once the submit button is pressed the action attribute in the form tag you have will be activated and should be action="<?php $_SERVER["PHP_SELF"]?>" . This will redirect the user to the EXACT SAME PAGE THE USER WAS JUST ON. This is equivalent to action="myform. php". You can use either one; however, if you decide to change your myform.php filename to say... myentryform.php then $_SERVER["PHP_SELF"] will automatically match that. $_SERVER is a superglobal array about the user. In this case PHP_SELF simply refers to the page that the user is at. In this case it is myform.php. If you don't know what a superglobal is go to www.php.net or look up some tutorials.
5. The data the user submitted is now in the form fields
BACKGROUND
This is what actually happens
1. When the user comes to www.example.com/myform.php the first part of the code in my second example checks to see if the user submitted the form via the POST METHOD.
In the HTML if the method="POST" then after user submits the form all the form field data will be stored in another super global called $_POST. This is an associative array that stores all the form field data. Lets say for example you had an input tag <input name="data" id="data" type="text" size="20" />. The name or id is data right? When this form is posted the data is stored in the $_POST["data"]. The key association in the array is EXACTLY the same as the name or id of the input field.
2. Now if the user has come to myform.php for the first time, there is no POST data right? So if we go through the code I posted If (isset($_POST["data"])) should return false. In my code in there is no POST data, then there is no else clause in the if statement, thus NOTHING is processed.
3. Now think about this. What if the user submitted the form myform.php and it redirected to itself? If(isset($_POST["data"])) would then be TRUE right? This is probably what you are not understanding. What I have done is, I'm using the SAME PAGE to process AND display a form. If there is no POST data then just display the form. If there is POST data then process the form, insert it into mysql and display a form with the data.
4. Now here is the part where we process the data. Here is a sample code
WARNING: The mysql code I'm putting in here is very basic. There are better ways to do this now with PEAR, or mysqli. Please study the www.php.net documentation for further details.
[PHP]
<?php
if (isset($_POST["data"])) {
//set connection variables
$server = "myserver";
$user = "myuser";
$password = "password";
//connect to mysql
if (!$connection = mysql_connect($ server, $user, $password)) {
die(mysql_error ());
}
//select your database
if (!$database = mysql_select_db ("mydatabase ") {
die(mysql_error ());
}
//PLEASE READ THIS PART CAREFULLY
// the EXACT same data is being put into $mysqldata and $html
//the POST data that is assigned to mysqldata is going into the database
//the POST data that is assigned to html["data"] will be in your html form.
$mysqldata = mysql_real_esca pe_string($_POS T["data"]);
$html["data"] = $_POST["data"];
//form your sql query
$query = "INSERT INTO tablename (data) VALUES ({$mysqldata})"
//send your query
if (!$result = mysql_query($qu ery)) {
die(mysql_error ());
}
//close mysql connection. This is optional.
mysql_close();
}
//else if there is no post data just display the form
?>
<html>
<head>
<title>My Data Entry</title>
</head>
<body>
<!--The action attribute is set to $_SERVER['PHP_SELF'] which is equivalent to the current page url myform.php-->
<form name="myform" id="myform" method="POST" action="<?php echo $_SERVER['PHP_SELF']?>" >
<!--Notice the value is that $html['data']. The same data you put into your data base. The code says if it exists then it will put it into the form field. If not then it will be blank.-->
] <input type="text" value="<?php if (isset($html['data'])) {echo $html['data']}?>" size="20" name="data" id="data" />
<input type="submit" value="Submit Data" name="submitbut ton" id="submitbutto n" />
</form>
</body>
<html>
[/PHP]
the PHP and the HTML sections are in ONE PAGE myform.php or whatever you want it to be.
So once again.
1. User enters page for first time
2. The script checks for POST data
3. The POST is not available because the user has not done anything
4. Thus the script does nothing and the form is displayed.
5. User enters data
6. User presses submit button
7. User redirected to the same page.
8. BEFORE the page is displayed the script checks for POST variables.
9. The POST variables are now available so we enter the data into our mysql database.
10. The same POST data is set to another variable to display in the form.
11. After the data is sent the form is displayed with the data inside the form field.