How to Validate Dynamic form with PHP? - 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.

Related

How to populate a multi page form if data has been entered or leave it empty if there is no current data?

Hi really hoping to get some help with a complex form issue.
I am building a real estate site where the agents will be able to log into an admin page and upload new houses that are for sale in an HTML form. I have a multi 3 page form that has Listing Info, More Listing Details and a Photo upload page. I have created these 3 pages and a nice css tab navigation to switch between the 3 pages. My issue is I would like the agent to be able to go back and forth between the 3 pages and have the data stay in the form. However if this is a brand new "Listing" then the form would be completely blank. I have the forms set up to submit through php and enter data in a mysql DB and then send you to the next page in the form.
The only way I can come up with is to have if statements that check session variables to see if a "listing_id" variable has been set. If it has it creates a form and fills in the blanks with the data from that pending "listing_id" if not it just creates a blank form. Is there an easier way to do this??? I would like to stick with PHP and HTML however simple JS would be ok too. Thanks for any incite anyone can give!
Whoa whoa whoa. Not sessions. No .. hell naw.
A very simple solution is to make it so the tabs are only visual. Don't throw away the HTML, just hide the container and show another. This way, when you go back, your data is still there.
I would save a cookie on their end with the data they have most recently put in and only clear the cookie once they have completed the form. Then you can use this data to fill the form.
For example,
Imagine the form data from their last inputs were separated by colons ":"
$prevform=$_COOKIE["lastform"]
$data=explode($prevform,":")
All the form data would be separated and reusable by setting the value of each input from $data[int]

best design practice in PHP for navigating from one form to another

I have an application in which I display a form so a user can search for client records based on last name. After entering search parameters, the record or records (there could be multiple clients with the same last name) are displayed. I then want the user to be able to select a client record, possibly with a radio button, and hit one of two buttons: Display details, or Create Reservation. The Display Details button should cause a new display with details of the selected record. The Create Reservation button should cause a new form, with its own handling, to be displayed.
Now, I know I can set things up according to this login
<?php
if (display button was pressed)
{
php code to retrieve more data and display details
}
else if (create reservation button was pressed)
{
php code to generate and display the reservation form, with appropriate handling
}
?>
display the original form with the search results
The problem is, I end up with really ugly, hard to read code because the php code to generate and display the reservation form is lengthy, and needs its own validation, database interaction, and form handling. The code, to my Java-oriented eye, looks ugly and non-modular. Plus, the code for handling the reservation form is icky, with lots of flag setting to determine if we are in form entry mode or form handling mode. I would like a much cleaner way to do this. So my question is, what is the best practice for handling the situation where there are multiple buttons and the action associated with each button is complex?
I could call a function, obviously, but I still end up with the ugly flags determining which state the script is in (are we displaying the reservation form or handling it?). I could create another php file and include it, but the ugliness persists. Or, I could use header, and pass the client record id in a session variable to the new php script. But that would mean a second, unnecessary retrieve from the database to get the client information again.
All the code examples I see on the web show very simple processing after a form button is pressed. What is the best way to do complex processing and displaying a second form based on a button press?
Have you considered using a framework like Laravel for your site. It would seem to me that you must be doing this "manually". With the complexities you described, having a system with routes and "build-in" functionality (like Eloquent ORM) might serve to simplify things for you.
I would go for using ajax and a rich jQuery plugin (or some other framework) to do what you want.
Basically you will handle lists and the functionality that you mentioned with the php reading data and jQuery scripts to dysplay it. And the information that you have to show would be through ajax. Or when you want to edit.
Here is a cleaner example of what you need:
http://jqueryui.com/dialog/#modal-form

How do I create a form without refreshing the page at the same time the data goes into a mysql data base?

Ok, I know there have been a few questions posted on this but a lot of the information are dotted around and not necessarily connected in one form that I need for my site.
Basically I want to create the style/functionality of the twitter form on their home page.
Where inside the input fields are the labels, and when you click on the input the fields the labels don NOT disappear but fade into a different colour. Its only when you type, is when it disappears.
If they don't type and press submit I want the verification signs come up ( I know how to do this but it seems to be conflicting when I put the jQuery code in)
When all fields are inputted I would like the form to fade and represent a thank you message whilst
the data gets placed into a mysql ( specifically a MAMP/PHP My Admin) database.
Very similar to this form on hbuddy.com
Would massively appreciate if this was responded in a step by step format, simply because i am at the beginning stages of learning pretty complex programming language.
Thanks!
you can try jquery ajax for form submission..
These links will help you for getting started..
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
http://www.9lessons.info/2009/04/submit-form-jquery-and-ajax.html
http://www.ryancoughlin.com/2008/11/04/use-jquery-to-submit-form/

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.

Store data whilst logging with PHP and Forms

I have a booking form that submits the chosen information to a PHP file that displays it in style with the theme on the left. The user then has the option to log in or register on the right hand side.
The problem is that if they log in or register the information that was submitted by the form to the PHP file is not there once the form submitted for signing in is complete.
I am well aware of the reason why, the question however is how to get around it.
Submitting the form data again with the log in form would be the best option but I cannot locate the file that the form submits to. The url is, ?action=login&instance=1 and ?action=register&instance=1 respectively. Any ideas chaps.
I will provide you with the link for the working example, however it is rather long winded as you have to submit certain forms to get there. Go To www.divethegap.com/update/diving-trips/adveture-training Then CLICK on beginners, choose anyone of the dates you see and press continue. Then you are there.
Many Thanks,
Many Thanks,
Why not just store the data from the first form in a $_SESSION variable? This is the cleanest, most flexible, and easiest way to pass data between multiple unrelated pages, especially when the data changes. Here's the general process.
Get variables from form 1 (adventure training form?)
Do normal form actions (validation, whatever
Put form variables into $_SESSION (e.g.: $_SESSION['tmp']['adventure-training-form'] = array('somefield' => $blah, 'someotherfield' => $foo, 'etc' => $yaddayadda);)
User either logs in or registers.
At the end of either process from step 4, get data from $_SESSION['tmp']['adventure-training-form'] and do whatever you need with it

Categories