How do I submit form entries to the database and redirect to the same page, with the entries still display on the fields of the form? I really need help on this. Thanks.
This is a really big topic and there are endless possibilities, but here is a little snippet for you.
Here is the basic idea.
1. use $_SERVER['PHP_SELF'] as the action of your script so that you come back to the same page when you submit the form.
2. if $_POST variables are set then process them put them in your database. then assign the $html['data'] variable the value you put into your database.
3. Then in the HTML section Line 22, the code will display the data depending if it is set or not.
4. If there are no $_POST variables set then, "probably" it is the first time the user has come to this page.
Assumptions: database used is mysql
Disclaimer: In no way does the following code deal with security issues and/or accessibility. However, this code should give you a good idea of what to do.
YOUR FORM PAGE WITH SOME PHP
[PHP]
<?php
//this is just a sample data validation function
function validdata($data ) {
//check if the data meets your requirements
if (!ctype_alnum($ data) {
//if not then return false
return false;
}
else {
return true;
}
}
//This function connects and returns a database that you specify
function db_connect ($server, $username, $password, $database) {
if (!$connection = mysql_connect($ server, $username, $password)) {
//you may want to set a better error message for security purposes
die(mysql_error ());
}
if ($db = mysql_select_db ($database, $connection)) {
die(mysql_error ());
}
return $db
}
$clean = array() //clean data container array
$html = array() //html container array
$mysql = array() //mysql container array
if ( isset($_POST['data']) ) {
if (!validdata($_P OST['data'])) {
$html['errormessage'] = htmlentities("P lease enter an alphanumeric string", ENT_QUOTES, "UTF-8");
}
else {
$clean['data'] = $_POST['data']
$mysql['data'] = mysql_real_esca pe_string($clea n['data']);
$db = db_connect("ser ver","username" ,"password","my database");
$query = "INSERT INTO tablename (datacolumn) VALUES ('".$mysql['data']."')"
if (!$result = mysql_query($qu ery)) {
die(mysql_error ());
}
############### ############### #############
##The code right below is what you should watch for
##This will be displayed in the form field
############### ############### ###############
$html['data'] = htmlentities($c lean['data'], ENT_QUOTES, "UTF-8");
}
}
//else POST variables are not set then just display the form without data
?>
[/PHP]
HTML PART OF THE SAME DOCUMENT
[HTML]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" dir="ltr" >
<head>
<title>Sample Form</title>
<style type="text/css">
#boldred {
font-weight: bold;
color: red;
}
</style>
</head>
<!--Automatically focus the first form field-->
<body onload="documen t.myform.data.f ocus()">
<!--You can use post or get. I prefer POST. -->
<!--Notice that name is not valid XHTML strict in the form tag. It just makes things easier when you use javascript though -->
<form name="myform" id="myform" method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<p id="boldred"><? php if (isset($html['errormessage'])) {echo $html['errormessage']}?></p>
<input type="text" name="data" id="data" tabindex="1"
<!--Here is your answer. Basically echo $html['data'] if it is set. If not then just leave the for field blank.-->
size="20" value="<?php if (isset($html['data'])) { echo $html['data'];}?>" />
<input type="submit" name="submitdat a" id="submitdata " tabindex="2" value="Submit Data" />
</form>
</body>
</html>
[/HTML]