php code logic for transferring data from one page to another - php

i made a (php) form, and used php session and post to transfer the form data to another page, which gives the user an option to have a look at the data and if wrong he shall move to the main form..
now here is what i did
on page1 i made a main form say form1, when user clicks on submit, i saved the data to DB and added that id to a session, and sent that session to next page using the same id
on next page i called from db matching the same id and displayed data accordingly..
now i displayed the data (on the second page) inside a div styled with css, the logic for this second page is giving the user the choice if the data is wrong he can edit, and bottom of this div i added a button with code
Edit
now my question is this, what shall be next? my target is, if user clicks on this edit button he shall be taken to main form (i.e. page1, form1) and there he shall has the option of editing the data in the same id. How can i achieve this friends?
Also i used
<?php session_start(); ?>
on top of both the pages...

Just change your code on the form1 page to; load the data from the database if the id of the record is set in your session variable.
Then if the user decides the data is wrong he just gets sent back to form1 and has the possibility to make changes.
Use something similar to: value="<?php echo $data['name']" in your input fields.
Then check for the id and load the data:
if(isset($_SESSION['record_id'])) {
// Do a query with `where id = record_id
// Fetch the data in the $data variable
}

IF you dont want use GET then use SESSION.
in file.php set
$_SESSION['your_id'] = $theid ;
and in your edit.php retrieve it like that
$id = $_SESSION['your_id'] ;
of course you must session_start in both files.

If you already have all the data in your session you can simply link back to the first page. All you have to do is set all of your fields on the first page to pre-fill with the session data as long as it isset. This way, when you go back the forms will auto fill with the data the user has already entered and they can simply change what is needed.

Related

How to go back without asking to confirm resubmission of the form

I have a web page that loads all the data from a mysql database called datalist.php
From this page I can edit record by record with a button that redirects you to an editdata.php page adapted to the fid of the record.
Once edited as they want to see the changes, I don't redirect them to the main one letting them see the changes and simply clicking back or with a button they return to the datalist.php without any problem.
The button is this
echo "<p id='parrafo'><a style='padding:1px 20px'class='button rounded-0 primary-bg text-white w-0 btn_1 boxed-btn' href='javascript:history.back() '><--</a></p>";
PROBLEM
I added a search engine where the displayed data can be filtered.
When they use the search engine from datalist.php, I direct them to a page called search engine.php where, through a post method, I store what they are looking for in a variable and the data that users want appears.
But when they edit a filtered record, it is edited without problems, but when they go back, they return to the search engine.php and the message appears:
"Confirm form resubmission In order to display correctly, this web page needs the data you entered earlier. You can submit that data again, but that will cause the page to repeat all previous actions. Press Reload to submit the data and display the page.
Hit the page refresh button to resubmit the data needed to load the page."
Of course, if they update, they come back when the filtered data comes out.
Isn't there any way to store the variable used in the search so that when I go back I don't get this error or any solution??
simple! when user will submit form for that variable instead of making post request
option1: just use get request __url__?variable=... but this will not remember the variable when you go back
option2: store the variable in the cookie and just go to next page (eg. window.location.href = '...';). and in next page access the cookie from php.
If you are wanting to show the form to the user as a confirmation, but without the possibility of another post, then remove the form element and the button. Display all other boxes as they are (with the values populated from the POST array).
And display another message telling them that it has been successful.
You are using PHP, you can achieve this easily with it. If you are unsure, then post a short version of your code in a separate question.

Multi-Page form submitted to single row - PHP MYSQL

I have a current project which I am having some troubles with. It will be written in PHP / MySQL
It is a multiple page form (3 pages)
When the user clicks 'next page' (submit button) the data needs to be stored in the database and the next page needs to open where the same thing will happen (this data is also stored in the database when 'next page' is clicked and then again same thing for the final page. I know a way where all the data can be submitted on the final page but we don't want this. We need to capture each and every step whether they finish filling out the entire form or not.
My issue is, how do I ensure that all of the data gets inserted into the single same row (under the same auto-incremented ID)?
I don't have the most experience when it comes to PHP/MySQL so go easy on me ;)
Thanks heaps in advance :)
This is one way :
On the first page, you need to put all the information that is needed by the database (primary/index keys). Then you can do your INSERT INTO the database.
Then, you can just get the id of the newly inserted row, with a SELECT id FROM my_table WHERE mandatory_information_is_same using the mandatory information you have. Keep it in something like a $_SESSION variable, or an hidden field in the form (unsafe).
Then on each new page, you just have to do an UPDATE my_table SET extra_information WHERE id=$_SESSION['row_id'], and you're done.
What you can do is store the form data in the session. Eg user enters detail and clicks next button, store the data in a session
session_start();
$_SESSION['firstpage']['fname'] = $_POST['fname'];
$_SESSION['firstpage']['lname'] = $_POST['lname'];
$_SESSION['firstpage']['username'] = $_POST['username'];
//check if data for first page stored
if(!empty($_SESSION['firstpage'])){
//redirect to second page
}else{
//stay on first page
}
Do the same for second page.
for third page
session_start();
$_SESSION['thirdpage']['address'] = $_POST['address'];
$_SESSION['thirdpage']['confirm_id'] = $_POST['confirm_id'];
//check if data for Third page stored
if(!empty($_SESSION['thirdpage'])){
//do validation for first and second page
//insert all session data to DB with same auto increment id
}else{
//stay on third page
}
The reason to use sessions is to preserve data and stored data can be used in future as well. The user can leave and continue later. Its your choice if you want to use sessions or cookies but they both do the nearly the same thing the only difference is session data is stored in the server and cookies are stored in the browser. There are so many validations that you can do on the third page.
Hope this helps.

I created a html form and using php I want to get the data and post it on another page inside a container

I'm having a hard time wrapping my idea into text so basically this is how I want to do this.
I have form.html once completed it tells the user thanks for filling out and goes back to the home page. then the data taken using php posts the data to gallery.html where their pic and info are posted.
How do I go about accomplishing this? How do I post it to a specific location in gallery.html?
You don't need to all my work its just I cant seem to find it on here or anywhere else it might be really simple.
Your best bet would be to setup a simple database to store the data in, making sure that you are using safe practices for data insertion into the database. And then on the gallery page you can call a specific id from the url to display the proper data. Like gallery.php?profile=5
On the gallery.php page it would query the database looking for id #5 and get all the data out of it the database and echo it on the page.
You are wrong in understanding how php works , Here are the mistakes that I could identify in your posting ,
your calling a webpage as gallery.html , but without .php extension , Apache wont call php interpreter when it receives a request , so to accomplish this you need to rename this to gallery.php .
to accomplish your task
here is the approach
1 > in your form.html page set attribute of form tag action=store.php method = post
2 > within store.php connect to mysql database and store all data in mysql data base ,
after successfully storing data display a thank you message and redirect to home page
3 > in your gallery.php file based on the requested id display the profile
NOTE : - you need to design your database in mysql with a table and column to store profile details
Form.html
User fills in the form
User clicks submit
Form data will go for processing to a page, say thanks.php
Thanks.php
Collect form data
Store it in database
Redirect to homepage.php after successful insertion
Homepage.php
Show message saying 'Thank you for filling out the form'
Give a link in this page to view the details of user
Something like this View Profile
Gallery.php
Grab the id using $_GET $rowid = $_GET['rowid'];
Use $rowid in WHERE clause of your database query $q = "SELECT * FROM <tablename> WHERE <primary key column name> = '".$rowid."'";
Use the returned set to show that particular user's data.

PHP Sessions and have "prev" and "next" button and retain values

I have a multi-page form.
I'd like to have a "prev" and "next" button on page 2, 3 and 4 so that if an user in the middle of filling out page 2 can decide to return to page 1 to edit/enter something and be able to still see values on page 2 where he left off.
Is this possible? any pointers/links would be immensely helpful.
(Yes, I have sessions and I can see the sessions after printr but if Im in the middle of page 2 and click backwards, I still dont see these sessions).
Thanks in advance.
Start the session when the user hits page 1. Each page should look in the session to see if there's any data there for that page, and if so, render the form with those values pre-filled. When the user hits either PREV or NEXT, process the form as if they hit submit, but instead of saving the values to (for example) a database, save them to the session. Then redirect to the requested NEXT/PREV page. When the user hits DONE (or whatever) on the last page, pull all the values from the session and process them to your database (or whatever.)
For a simple approach to a "multi-step" form, simply use Javascript. That is what I do.
However, if you want to have a stateful form that remembers data in between pages, you will have to use a session array to track the values entered.
When you fill out a page, the POST array is populated on page2. Serialize the array of post data and store it in a session array. Back on page1, if that session array is set, unserialize the data and echo the values into the right form element.
You can save all of the data in a database as they move to a different page as well as print these values out when the page is loaded. This would still have to be sent to the server via AJAX in the case of a "Previous" link click.
You can put all pages in one html page, then just hide/show the correct pages when they navigate using javascript.
Save all entered data in a session, which would have to be sent to the server via AJAX if it is not submitted through a form the traditional way.

Interrupting $_POST to Preview Input

I have set up a database and have form data being submitted to it via $_POST in PHP, and I also have my page set up and working which pulls in the fields from the database and displays them. Not rocket science I know.
However what I want to do now is place a page inbetween the submission of the form and the insert in the database, to give the user a chance to check their entry.
I have created this page and used the $_POST data to display the data from the form (as it has not been entered into the database yet), however i'm wondering how, if the user approves the submission, I then INSERT it into the database. (I've tried running the INSERT query from the $_POST data on pressing a submit button, however because (I assume) ivew interrupted the flow between the original form submission and the INSERT query, all I get is a list of errors for unrecognised variables.
So what I have is this process:
form.php /user enters info using $_POST
preview.php /user is previewed info using $_POST and Session code starts (below)
submit.php /MySql query runs but returns all errors for undefined indexes
This is my session start code:
session_start();
if (isset($_POST['preview'])) {
$_SESSION['company_name'] = $_POST['company_name'];
etc etc - remaining form field names
}
POST data is not automatically passed on to other pages. Save the submitted data in the session and read it from there after the confirmation page, or insert all that data back into the page using <input type="hidden"> elements, so they can be resubmitted as POST.
A very simple illustration of the process using sessions:
form.php
<form action="confirm.php" ...>
...
</form>
confirm.php
<?php
session_start();
$_SESSION['data'] = $_POST;
?>
Please confirm:
Name: <?php echo htmlspecialchars($_POST['name']); ?>
...
Confirm
save.php
<?php
session_start();
$data = $_SESSION['data'];
// save $data to database
You should do it with 3 modes
Display
Verify
Insert
The quick an easy way is just to have a GET parameter or a hidden field that says the last state of the form. First time you display it its set to DISPLAY. Form submits, your script looks at the field, knows that after DISPLAY comes VERIFY, and presents the verification page. User submits, script looks at the field, knows that after verify you insert into the database, and you preform the relevant query.
Not sure how you were doing it before, but I assume that you were redirecting or just having the user click a link, which lost all the $_POST data
Unless I'm misunderstanding what you're saying/doing, I'd do something like this:
When the user clicks the "save" (or whatever) button, hijack that click event and use the form data to do the preview business. When they click "confirm", have that send the data to the server and save it to the database.
Again, I'm not 100% sure if that will work, but perhaps it's a place to start.
Good luck!

Categories