Working with multiple forms at once in PHP - php

I need to make an application which dynamically generates some number of forms and processes them. The user should be able to deal with each form individually without resetting the entered values in the other forms.
What I have right now is a master form that uses JS to generate some number of forms. The forms are all the same (i.e. I iterate on include("myForm.php").
My problem is that every time I submit one form, the values in the other forms get reset. How do I fix this?

This is a three-step process:
Ensure that each form element on the page has a unique name and ID.
Store submitted values in the user's session.
Use PHP to insert the previously-entered values into the form fields when a form is posted.

Post the form with AJAX, this doesn't refresh the page so you won't lose the values entered in the other forms

Related

Connecting forms in php

I have a form which has details like name,number,email,etc... I have 3 forms like this.
So I have created a file called form.php which has the form and I am including it in my page.
Each of the form has some details that is not in the other forms like:
One form has Number of slots while the other 2 don't have it.
I explicitly added the uncommon part of the form in each page differently as a form.
Now I have 2 submit buttons. So how do I avoid two buttons and connect the 2 forms as the data from the complete form? It has to added to a single table.
Use $_SESSION in the first 2 forms to get the data to the third form.
http://php.net/manual/en/reserved.variables.session.php
Then retrieve data from the session in the third form and place it as a hidden field.
Then the submit button on the last form will submit all the data i.e from the fields in the third form as well as the data carried from the first two forms.

PHP/Javascript: Remembering state of form/form fields with cookies

In this question, I found out how to dynamically add form fields.
Now, how can I set a cookie to remember the amount of form fields that were generated, and their content?
eg. First visit, user types this:
Input one
Input two
Input three
Then he/she visits the page again. There are 3 form fields, containg Input one, Input two and Input 3.
Is this possible using a server/client side solution?
Thanks a lot,
Harley
assuming that the input textfield class is "textfield1" -
This snippet will capture the data into a cookie when the value is modified
$(".textfield1").change(function() {
$.cookie("textfield1", $(".textfield1").val(), {expires: 7});
});
This snippet will read the cookie data and populate the field on page load -
$(".textfield1").val( $.cookie("textfield1") );
Put both these snippets inside $(document).ready(function(){ }); and you are good to go !
there is a useful plugin of jquery "Jquery Populate" you can store your json in cookie/session and use it to fill your forms.

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).

Form autofill if value exist in database using AJax

I have a form I need to auto fill it using Ajax and php. Suppose My unique field is mobile number. So when form appears firstly person has to fill mobile number. If mobile number exist in the database than all the rest field retrieve its value that is his name, email etc, making all the text fields disable.
my approach to this work is on blur effect I can send value through AJAX. but how call values in array. I have called only single value through echo; but have not called array back from java script page.
Secondly I need jquery to fill all form with respective values and disable particular fields in form.
Please give me some more idea to make this approach better and provide some hint to implement it efficiently.
have you tried Autocomplete from JQuery UI?
http://jqueryui.com/demos/autocomplete/
it is very easy to use, it uses ajax and can be easly used with php. Tell if you will have any problems.
A simple way to do this is to separate your page into 2 forms. The first form will contain only the mobile number field, while the second form will not be initially visible. This way, the user will be forced to fill in his mobile phone before doing anything else.
Submitting the first form will trigger an ajax call to your php file which will check whether the mobile phone exists in the database or not. If it exists, it will return a pre-filled form. If not, it will return an empty form. Therefore, there is no need to change the values of the fields with javascript. Now, all you have to do is to take the response of the php and put it into a div under the first form.

Post Array Data from Multiple Selects to Mysql

I have a form-1 which has 4 fields. when the user inputs data in these and hits submit, he is taken to form 2 for further selection of more items from multiple selection box. after selections are complete, he is prompted to update and on updating, all the data has to go to a third form for processing.
currently i am passing the single fields data from second form to third form by <input type="hidden" name="abc" value="<?php echo $x[0] ?>">
I am getting stuck as to how to retreive all multiple selected items from the array, perform a calculation on them and then post to mysql and then update the user with posted information.
or is there a better way of doing this, pl. guide me. my fields are:-
first page
customer id - single selection field
date - input
segment selection - single selection field
second page
items inputs - itemid, quantity,price (these are in one row and user will dynamically add or delete rows based on requirements. i have done this through Javascript)
now after all this, i want to gather all details of customer, segment, items(id,quantities,prices) and then post them to mysql.
If you want to use separate pages you can use either hidden inputs fields or sessions to pass along their selections. With sessions, you'd just store the array of data in $_SESSION and use session_start() on each page to get the session from the previous page. With hidden inputs, you can store them just like you would with session, and when they click POST you will rewrite them into the form. Are you stuck on specific aspect of doing this?
On the final page use either the session or the hidden fields (depending on your chosen method) + the final POST, to query MySQL.
Note: As Zirak mentioned in the comments, you could also do this using a single page. You'd use one of the same methods described above, except it would post to itself rather than to another page. This might be a faster/better way to code the page... If you opt for the single page method just ensure that you make it possible to go back, both through their browsers back button and a link you provide.

Categories