Form autofill if value exist in database using AJax - php

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.

Related

dynamically filled forms with Jquery & Ajax

I have a bit of a problem achieving what I'm trying to do.
I'm doing a mailbox like page and I'd like to add a "Respond" button which will fill a form with some values.
I'm generating my page message by message in php. For each message I generate an input button with its own id and when I click on the button, I'd like to open a form in a new window, with the recipient and title prefilled and not changeable by the user, with another textarea that the user can fill with his message.
For that, I have a general template for a form containing 3 values (recipient, title and message). All are (for the moment) text or textarea values.
Is it possible to use one form to send a message to anyone, or do I have to generate a form for each message with pre filled values ? The last solution seems quite horrible to do.
I tried to find something that I could do with only javascript, since I want the form to be submitted asynchronously by Ajax.
I can make an onClick event and pass the values to that function, but in that case I won't have the values of the textarea the user will have entered.
Is there a way to do, for example, an entire form in Javascript with the values I'll pass through the onClick event, get the value of the textarea from the form that would have been populated with these values, and then send everything through Ajax ?
Well, here is what I did.
For each Respond button generated, I set an onClick event passing the fields I wanted to be pre-filled in my form to a JS function RespondMessage()
I also made a form at the bottom of my page that I hid with css.
This form contains as much hidden fields as I want pre-filled datas, and as much spans as I want to show infos.
When I call the RespondMessage function, I manually set the hidden fields in my form with the arguments I called the function with.
After that I fill my spans with the infos I want to show (for example, I set the recipient's name for my message like that).
Once all of that is done, your form is ready to be displayed. So after setting the fields in the form, I just pass the form from display:none to display:block, and do a lot of css tricks to make that smooth and beautiful.
So now you have your pre-filled form, you just need another function that will make an Ajax call to avoid reloading the page after each response, and you'll call it with the onClick event of your form's submission button.
I hope that this process will help someone.
I'm not quite sure that it's the best way to do it and I find it quite dirty and exploitable so you need a bit more server side checking than usual, but it does the job.
I don't provide code because it's mostly all really specific to the application you're setting up.

Save/restore form data after locale change

I have a database app written in PHP (jQuery/JS on the front end) that has bilingual labels/text. Currently one can only change one's language on a maintenance page (form submission, then PHP updates a session variable with their new language choice), but the users would like me to add a language pulldown that would appear in the corner of all pages. When the page contains a form, I don't want users to lose their partially entered data if they happen to change the language, so I need to save/restore the form data somehow. Is there an easy way to do that? I know I can use jQuery to serialize the form, but then what? Send that added onto the URL and pick it up in PHP? Then what? Write some routine to loop through the form fields and handle them properly (inputs, selects, radio boxes, etc. are all different)? It seems like there should be an easier way. I don't mind restricting myself to HTML5-supported solutions or adding jQuery plugins.
How about localStorage?
If user has filled any input fields, save them to localStorage and delete the data after user submits the form.
My suggestion is to:
Submit the Language and any wanted user data when changing language to the server using $.ajax or $.post

How to Validate Dynamic form with PHP?

I have searched over SO for quite awhile so I apologize if a similar question is asked and answered but I don't believe there is.
I am using a PHP validation script and jQuery Validate to process all of the forms on my site. With known fields, no issues. However, there are pages which a user can enter additional person(s) information.
A user is asked to enter his family member's names in a form. The user can submit the form or choose to add additional family members to the form. I have not quite figured out how to properly cycle through all the fields and display unique messages based on the users input in jQuery but know I can do so with metadata and the validation plugin.
My issue lies with PHP. I can take each array and run though foreach loops, however, if an error is found in ANY field, an error is generated and the form is displayed again with all fields' previously entered data. I am unable to determine how to point each field's original data to it given the fields are built dynamically when errors appear.
The SIMPLEST METHOD I can come up with for demonstration is this:
A user must enter the percentages of pie he wants his friends to eat.
He must give out 100% of his pie. The user can choose to dynamically
add friends to his pie eating extravaganza but each person must
receive atleast 1% of the pie and they must all together eat 100%.
Sample code
I have created a sample script here which shows the form setup but obviously there is no PHP backend to test. You will need to use your imagination the backend works (I have added into the comments in the html). The same principle would apply with PHP SESSION variable since this data would need to be passed from page to page (if a user revisits this page, the amounts entered for each piece of pie should display). Also, I think it goes without saying but this data MUST BE validated server-side.
Will you not be reloading the page in the event of a PHP error? In which case you know the index of the error in the foreach loop, saved off to some variable - most likely an array to handle multiple errors. When you print the options back out and you are looping through the submitted array again can you not add an additional css class to the text box when you reach a count that is in your error array.

Working with multiple forms at once in 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

Find out what type of form field was used to enter info in a form with PHP

Using PHP; is there a way to check what type of form field was used to enter info in a form.
For example: was it submitted via a list/menu, radio button, text field, textarea, or checkbox.
I have this info stored in the database; but I'm trying to see if there is a way to do it without querying the database or using hidden form fields to pass the field type. Basically is there a function that already does this?
I don't know of one though I'm sure someone else might pop up with an answer. But if the forms in question are of your own design you could name the inputs as checkbox_ or textarea_ prepended to your normal name. Then parse them on the form processing side.
The data entered into a form will be submitted as a set of key:value pairs only.
With standard HTML form elements only there is no way of telling what type of form field was used to gather a particular value.
Only name/value pairs are passed through the Post data, so you would need to mark it in the field name to give your server script a hint of what it is. You could do this with a prefix/postfix. Depending on where you are in your project, you may want to look into using a framework and taking advantage of the advanced form handling options that they can give you.

Categories