I'm passing a value 'name="idUser"' through a Form with POST from pageA to pageB.
Then in pageB I have a query that uses this value for different things, like select, updates, inserts,etc.
Ej:
$updateIDU=$_POST["idUser"];
Select * from table where idUser = $updateIDU;
The pageB has a table in a Form that it can be updated (you can add values, with the form you send the values to the database).
After the information is updated to the database i refresh the page with this:
$link="LOCATION:pageB.php?ok=1";
header($link);
The problem is when i refresh the page with that method the querys for the tables crashes with errors:
Notice: Undefined index: idUser in C:\wamp64\www\pageB.php
Can somebody help me fix my problem or is there another way to pass this value (ID) from pageA to pageB without losing it on refresh (updating the form-table)?
Note When I refresh the page (F5) the page dosen't show me any errors, but when I update the table (with the form) it does show me the error.
Instead of using Post you can set a cookie to retain data across all the pages. Set cookie using this:
setcookie("TestCookie", $value);
And then access them using this:
$_COOKIE["TestCookie"];
You can have a hidden form field in page b , when ever user lands on page b update the form field .
Related
I have a simple php form that submits (POSTS) data on pressing the SUBMIT button and a 'thank you' page is displayed and the data is stored in a database.
Usually on this thank you page if you press the BACK button on the browser and then the FORWARD button on the browser you are brought back to the same thank you page but the form is not submitted again.
In the last few days when I do the BACK and FORWARD on the browser the form resubmits the data and there's a duplicate entry in the database. This happens only in Chrome.
Have I made some errors in the settings in Chrome or is there some other problem somewhere?
The typical solution is known as POST–Redirect–GET. Essentially, your form posts to a page which inserts the data into the database or whatever other actions are necessary and then redirects to another page. That other page doesn't actually do anything but just displays a success message or something. This way, you have two entries in the history: the form and the success page. The form-posting page is never added to the history; pressing back or forward will skip the submission.
Generate a value and put that inside a hidden field. If the user submits the form store that value (must be unique). If one tries to submit the form again with the same generated value, then do not execute your insert or update.
You could set a cookie or session that says the form has already been submitted, and, if that is set don't resubmit the form, but that is basically a band-aid and may not even work...
What you should REALLY be doing is avoiding duplicates by checking the input values against existing values in the db, such as email or username. You should also set your email and username fields to UNIQUE in your database so you'll never get duplicate email addresses or usernames - solving your problem.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')) {
// 1. check if $_POST['email'] already exists in the database
// 2. if email doesn't exist, insert data
}
Th issue is, when you reached the success page and refreshed, the browser has to resubmit the cached data; because the page where it reached is dynamically generated. Now when you click on the okay, the data which was previously stored in the $_POST variable is resubmitted. To stop it, you would have use dynamic binding instead of static binding.
A simple solution to this issue is:
Make the action attribute of the form blank i.e <form action="">.
Call a javascript method onclick of the intended button.
Add the Action attribute in the JS method and submit the form.
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.
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 have a webpage that stores the user ID of someone who clicks a button on the page. Once the button is clicked, that user needs to be able to see content (just another button) that only that user can see. Other users on that page won't be able to see that button.
The problem I'm having is I don't know how to use that unique user ID, which is superglobal, to recognize on load what the user ID is, and how to execute if its the correct user, show the otherwise hidden inputs.
It is same problem that arised to me
You have two option to solve this problem
use a PHP globals like $_COOKIE[] or $_SESSION{]; To store the id of that user
or you can use a database field of array type.It will store the value of button clicked by a particular user(using a database will more efficient because you can use it any time and it do not expires )
I have the following snippet:
foreach($_POST['status'] as &$status){
mysql_real_escape_string($status);
}
How do I wrap this in a session like $_SESSION['status'] or something so I can insert this into the database? This code is situated on page 2 of a 5 page form so thats why I can't just insert it using $_POST['status'].
As you have a five page form you need the status variable to be inserted either in the session or in database so that you can use them till the end...
Both the things are quite easy..And there exists one third method also..
To store it in session create a session variable like $_SESSION['status'] and insert the value you have got for the status and on all subsequent pages call them as:
if(isset($_SESSION['status']) && $_SESSION['status'])
To insert into database just insert this info along with what you have got in first page, but I won't suggest that, as user may abort registration after first page..
Third method is to make them as
">
By this you can get the values on next form submit place also with $_POST['status_again']..