473,789 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

submit form entries to the database and redirect to the same page

92 New Member
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.
Dec 7 '07 #1
12 13353
clai83
41 New Member
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]
Dec 7 '07 #2
backups2007
92 New Member
First of all, thank you for responding to my post.

Though I was looking for a more simple solution, I'm pretty sure that your suggestion would work. It's just that I'm quite confused. I'm not really that of a hardcore php programmer. I know how to program in php but I haven't really mastered it. I haven't even used functions in my project before.

And the database that I use is mysql.

Do you have any other suggustion that is not too advanced?

Thanks.
Dec 7 '07 #3
clai83
41 New Member
First of all, thank you for responding to my post.

Though I was looking for a more simple solution, I'm pretty sure that your suggestion would work. It's just that I'm quite confused. I'm not really that of a hardcore php programmer. I know how to program in php but I haven't really mastered it. I haven't even used functions in my project before.

And the database that I use is mysql.

Do you have any other suggustion that is not too advanced?

Thanks.
Sorry about that I got carried away.


here are the main points

1. In the action attribute of the <form> use this:
<form action="<?php $_SERVER['PHP_SELF']?>" id="myform" name="myform" method="POST">
When you submit this form, it will redirect back to itself.

2. When it redirects back to the same page, you then process the data, with the code on the top. If the $_POST variables don't exist then that means the user has come to your page for the first time.

3. After you have processed your data and entererd it into your mysql database put that same data into some variables. For my example I chose $html['data'].

4. <input type="text" name="data" id="data" tabindex="1" value="<?php if (isset($html['data'])) { echo $html['data'];}?>" />

This is the text field for the form in my example. Take a look at the value attribute. It contains a conditional statement

[PHP]
<?php
//if there is data to display then display it
if (isset($html['data'])) {
echo $html['data'];
}
//otherwise do nothing
?>
[/PHP]

Basically if you inserted data into mysql and put that same data into the $html['data'] variable then it should show up.
Dec 7 '07 #4
clai83
41 New Member
Here is some simplified code.

1. I took out validation
2. I took out CSS and javascript to make it easier to read.

dataentry.php
[PHP]
//Check to see if you got some POST data from a form submission
if ( isset($_POST['data'])) {
//you really should validate your $_POST variables
//but I left validation out for simplicity

//insert data into mysql database

//set the html data variable to display the values in the submitted form
$html['data'] = $_POST['data']
}
//if you don't then just simply display the form

[/PHP]
dataentry.php
[HTML]
<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 dataentry.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>
[/HTML]

hopefully this is the solution you are looking for
Dec 7 '07 #5
backups2007
92 New Member
Just what I was looking for. Thank you very much.

I have one more question.

I want to be able to redirect and insert to the database at the same time. When the user submits the form, the data gets inserted to the database and redirects to itself with the data present in the fields.

One more thing, I have more than a hundred fields in the different forms of the program that I'm developing.
Dec 7 '07 #6
clai83
41 New Member
Just what I was looking for. Thank you very much.

I have one more question.

I want to be able to redirect and insert to the database at the same time. When the user submits the form, the data gets inserted to the database and redirects to itself with the data present in the fields.

One more thing, I have more than a hundred fields in the different forms of the program that I'm developing.
I'm not really understanding your question I think. Are you saying you want to

(A) redirect from a "DIFFERENT" page to the dataentry page and have data displayed,

or are you saying

(B)

1. Person comes to form
2. enters data and submits
3. comes back to the same page with data displayed in fields.

because if its (B) the code above will do exactly that.

Please elaborate. Maybe explain your system a little bit.
Dec 7 '07 #7
backups2007
92 New Member
I'm not really understanding your question I think. Are you saying you want to

(A) redirect from a "DIFFERENT" page to the dataentry page and have data displayed,

or are you saying

(B)

1. Person comes to form
2. enters data and submits
3. comes back to the same page with data displayed in fields.

because if its (B) the code above will do exactly that.

Please elaborate. Maybe explain your system a little bit.
Welll, here's how it goes:

1. The person comes to the form.
2. Enters data and submits.
3. The data gets inserted into the database. And comes back to the same page with data displayed in the fields.

It will redirect to the same page so that the user can still be able to edit/update whatever he needs to edit/update in the form before printing.
Dec 8 '07 #8
backups2007
92 New Member
I kinda understand your first post already. Although I don't understand how the data gets inserted into the database.
Dec 8 '07 #9
backups2007
92 New Member
Could somebody please respond to my post. I really need help on this.
Thanks.
Dec 10 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
12412
by: lsarg | last post by:
i've been trying forever to figure out a way to use a regular text link in place of a submit button at the bottom of this. can't get it. i'm just starting to learn php, so i'm stuck. any help at all would be amazing. <?php # Script 12.7 - login.php // This is the login page for the site. // Include the configuration file for error management and such.
6
9647
by: HH | last post by:
I'm learning to design web applications with php, mysql, and apache from a book. I copied a sample application called guestbook 2000 that came with the CD in the book to my htdocs folder, but couldn't get the sign guestbook page (sign.php) to work. This page first checks the value of the $submit variable. If it is not "Sign", the page displays a blank form for the guest to sign. If it is "Sign", the page process the form information and...
8
3054
by: Matt | last post by:
I want to submit the form to the server without opening another page. When we do the following, it will submit the form data in myform to the IIS, and open page2.asp. <form name="myform" action="page2.asp" method="post"> But I don't want to open another page, I just want to submit the form data. Should I do the following?? myform.submit();
4
3343
by: Keith | last post by:
Is it possible to submit a form as soon as the page it is on loads and then redirect to another page? Thanks
1
14165
by: Paul Oakfleet | last post by:
The script below will disable Submit button until user accept terms, and will redirect user to another page after clicking on Submit button. The script seems to work fine on my PC (Windows XP, IE6), however I don't know if it's written well. I would like the opinion of someone who knows how to write these codes properly. Please let me know if there're any logic errors. PS: Please keep in mind that I didn't write this script, I found it
4
5482
by: houstoncity2004 | last post by:
Hi, I need help to JavaScript. I am new to this, and am trying to the page online ASAP. I have two submit buttons on one form, and each button will have to do different things. I named both button "but1". The first button has the value "Continue", the second button has the value "new page". If someone click on the button with the value "Continue", I need to check if certain information has been filled in before proceeding, and if...
5
5904
by: Codeman II | last post by:
Hi there, I am building a form where the user must upload a picture and fill in his details. Now I have a problem as all of this is on the same form. How will I be able to have the Browse button to open the "file browse" dialog and the Submit button to submit the form data.
1
2581
by: kkuniya | last post by:
Situation : - A form (method : POST, action : itself, onsubmit : alert 'Submit' ) - Got 2 submit button ( 'Save' , 'View') - Got navigation 1|2|3|4 What I want to do : - Once clicked on the navigation page, it will save the page without need to click on 'Save' button. Currently, if i manually clicked on the 'Save' button; it will save the page.
6
6524
by: planetthoughtful | last post by:
Hi All, I have a C# ASP.NET page that submits back to itself to insert details from a form into a database table. When / if the user refreshes the page (and gets the standard warning that POST data will be resubmitted), the previously submitted record is sumbitted again, and a duplicate record is inserted into the table. In PHP I would have avoided this by submitting the form to a processing page, which would then automatically...
0
9499
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10374
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10121
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5404
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5539
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.