Store values to database without using form action - php

I have several web pages created that captures information from user inputs and stores them into session variables in PHP tags inside the html. Finally I echo back the values from the session all on the same page and ask the user if all inputs are correct. I would like to include a submit button at the bottom of the page for the user to submit the session values into the database. I am not sure how to take the values from inside the current page in session variables and store to database. Here is an example of code I currently have.
<?php
session_start();
$name = $_SESSION['name'];
echo "<br>Is this name right?: ".$name;
?>
I have already created the database and respective columns. I am familiar with storing values in form submission, however I have always done this by embedding a file in the form action and obtaining the values using $_POST[''] to get values of inputs of current form.

So, in your queries, just substitute $_POST with $_SESSION. Or am I missing something?

Related

How can I send an Object/Array through form and hide the PHP form input from the user?

I'm trying to send an object or a array from one page to another through form. Json coding could help, but I need to send the info when the user press the form submit button. I've searched a little and tried to use serializing/coding through:
<input type="hidden" name="geracoesc" value='. base64_encode(gzdeflate(serialize($generations))).'>,
but the code is still shown at the HTML Code (because of the input), so, how can I hide it from an user at the moment the form is sent?
Couldn't you use the $_SESSION super-global?
On your first page, start a PHP SESSION with:
session_start();
Then assign the object you want to pass to the $_SESSION like so:
$_SESSION['somedata'] = $object_I_want_to_hide;
On any following pages, you can access the data like so:
session_start();
echo $_SESSION['somedata'];
you can create a table and save that data with session_id of that user and use from that everywhere which you need with unique session id which each visitor has.

PHP $_POST variable not set when going back to the form when user input is not correct

I have a form on one php page. Input values are submitted to another php page where they are processed. When values are incorrect or missing I go back to the form using:
header('Refresh: 3; url=biertjetoevoegen.php;')
The problem is that at this time the $_POST variables are not SET anymore, so the user has to re-enter all the data....
How can I go back to the form without losing the $_POST variables, not using $_SESSION variables?
If you want to redirect user to another page that has posted data to, you have to send data yourself to that page (by $_SESSION or $_GET or any other ways), but you should not do it yourself.
The best way is to set your action property of your form to be the same as your html form page and the page that you validate data (page that contains your php code).
Note that all PHP frameworks do this.

A form with 3 submits; how to keep data for final submit?

I have created a PHP form which requires the user to select a postcode from a list of postcode values.
The user presses submit two times:
- once to go to address select menu which will display a select drop-down with values
- second presses "ok" button to select the address corresponding to his postcode value
I need to keep the value of the selected postcode value for when the form gets submitted. I have tried setting up the postcode drop-down value chosen in a SESSION... but it gets lots when user presses form submit.
How can I keep all the form values even after refreshing the page when the user presses one of the submits?
"How can I keep all the form values even after refreshing the page when the user presses one of the submits?"
Reading your question I didn't understand if each of the form submits actually gets submitted to the server, but I'm going to assume so. I'm also assuming you're trying to use PHP sessions to accomplish this.
When the user submits the form, save the values server-side in a PHP session
//Start the session
session_start();
//Save the values
$_SESSION["foo"] = $_POST["bar"];
...
If, after choosing the address, the user gets redirected to the initial form and you want to populate that:
//Start the session
<?php
session_start();
?>
<!-- Populate HTML form based on previously submitted values -->
<input type="text" name="foo" value="<?php echo $_SESSION["foo"] ?>" />
...
After the final submit you should have all the submitted values saved in the $_SESSION array. Don't forget to always session_start() before trying to handle anything session related.
The short answer is I don't think you can track form data across multiple forms.
I haven't seen your project and so might not fully understand the requirements, but I would suggest you consider using AJAX. Check out the jQuery post() manual, it's really simple actually. This has the advantage of allowing you to update the page once your first form has been completed.
EDIT: Sorry I meant you can't access multiple form data in a single $_POST. Of course you could store it in $_SESSION (remember to start your session properly).

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!

storing a TextBox value(in a html page) in php array variable which is then stored in a session

I am totally new to coding and i would need some help regarding this question of mine.
the question is that,
I have around 5 html pages.I am trying to walk the user through each phase and thats the reason i have diffent page for entering the info.Each html page has certain text fields.For ex
Page1:lastname,firstname,mi...
Page2:address,phone,city,state,zip
Page3:date of join,time of joining,person who referred you
Page4:insurance info,insurance number
Page5:directions to the place, and so on.
Now the thing is that, i need to store the field values using sessions in php.
Suppose the user goes from page 1 to page 2(after filling all the details), and then if he wants to visit page 1,he should have all the fields populated with the data which he sent before(these details can be changed now).I want to store all the fields in a PHP array and then store the array using sessions.
My question is that how do we set the value of the php variable to the Value in the Textbox(the textbox will have the value entered by the user) and how do i design the array, and store it in the session.
Also can i use the same session variables across all the pages,so that i could save all the details entered by one particular in the array(which is then stored in the session).
Any help is greatly appreciated.
You can access the data passed to you in the $_GET and $_POST variables. eg
$lastname = $_POST['lastname'];
You can store the value away in a session like this...
session_start(); // call this once on your page
$_SESSION['lastname'] = $_POST['lastname'];
You could even do something like this...
// Store all the posted values in the session
$_SESSION['page1'] = $_POST;

Categories