473,800 Members | 2,554 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
12 13357
clai83
41 New Member
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.
Dec 10 '07 #11
backups2007
92 New Member
What if the user refreshes the page? Wouldn't it create duplicate entries in the database?
Dec 12 '07 #12
clai83
41 New Member
What if the user refreshes the page? Wouldn't it create duplicate entries in the database?
Well think about it. If you refresh the page what will happen? There is no post data or data to enter into the database, so nothing will happen. The only way a person can submit data is by pressing the submit button. Why don't you try it first.

Also if you are worried about empty entries or invalid entries in your database then you HAVE TO validate your data as I explained in the very first example.

Making PHP do what you want isn't so difficult. Making PHP do exactly what you want takes some time.
Dec 12 '07 #13

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
5484
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
5906
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
2582
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
6526
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
9694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9553
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
10281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9095
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6824
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
5477
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
5612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4152
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3765
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.