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']..
Related
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 .
I have my own MVC applicaiton where I have 3 main area: model, view, controller.
In the view, I've created a form, to insert some data to the DB. The action is set to the same page and the type is: POST.
In the model, I have some basic checking if the form has been submitted or not. There is an 1 second sleep after that.
In the controller, I'm connecting the view and the model in a very simple way, so they can commuicate between each other.
However a problem is arising, whenever the visitor clicks multiple times on the submit button. Because there is 1 second waiting (it is intentional - so it looke like the system is processing the data), they can click multiple times to the button.
There is an issue with that, because this way, the data gets inserted to the database based on the amount of clicks. If I output a simple word, like "Foo" it will get displayed only 1 time (I guess this is because the page is reloading every time), however the data gets inserted multiple times to the DB.
Is there any way around this issue? My intention was to create a session and check if this is the first submit, else do nothing with the DB. However this didn't work, because the session always got the basic value upon the page load.
Can anyone help me with this issue?
Two options:
Put some javascript on the front-end to disable the button upon being clicked and then submit the form - this is probably the easiest way but you may want to try and leave the front-end as uncluttered as possible.
Set a session variable before you load the page, then when you do the insert delete this session variable immediately after. Before doing the insert you should check for the session variable - if it is set then the insert is allowed, if it is not then it isn't. You may also want to add in code into a master controller (if all your controllers are derived from one master controller) that checks what controller has been called and then if the session variable that is set should be for a different controller then it should be deleted (so it no longer exists if the user goes to a different page without submitting 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 home.php through which i send some data which gets inserted into DB, which is done by making an ajax call to another php page, lets say function.php and a unique id is generated against the entry and stored in a variable $id.
When the user leaves the home.php page I want to delete the particular entry that was inserted into the DB, by making another ajax call to same function.php, but the problem is that the $id that was previously generated is lost as function.php is reloaded. Declaring a global in home.php doesn't work.
Any idea how can I store the id, so i can retrieve it, when needed.
Some code would be great.
Abhi
Try this way:
Put this on your pages to start a session: session_start(). Then after you inserted data into DB do $_SESSION['sql_insert_id'] = $id - this is assuming you need just one insert per visitor.
Then when you need to delete from the DB you have the $id stored in $_SESSION['sql_insert_id']
You can also close the session after the delete with:
session_unset($_SESSION['sql_insert_id']);
session_destroy();
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.