Add to user input value before storing in the database - php

I have a form whereby a user fills it in and clicks submit. This then stores the information in the database. I have a field called height. And I want the user to type in something like 10, but I dont want them to type in the measurement aswel. I want this to be added to '10' before storing in the database
So when they click 10 and submit, i want it to show in the database as 10inches or 10".
I tried to google it but I get nothing because I have to be quite specific with keywords and I just dont know how to search it.
Does anybody know how I can achieve this?

Why do you want to store this in the database as text? That should not be a database concern, but instead be a UI concern. Store the 10, and just add the inches when you pull the data out?
Or you could store the 10 as a value alongside a mapping to what type it is (such as inches/cm/etc). This design will give you much more flexibility

You can use javascript to validate the textbox value on form submit (onSubmit)
In server php script, you could also check the value again.

Related

Best way to store data about problems in fields of filled form

As I am writing my program by PHP programming language and Mysql DB in WordPress CMS, I built a plugin to create and handle a customize form in a special workflow to the needs of our customer.
After form being filled by the user, one of the editor users of the site would check the data of the filled form and validate them.
The screen shot of the validation page that is show to the editor users is like this.
As you see I fetch data of the form from Data Base and print them to the editor users to validate them. At first all the field are display in green background. By using jQuery I give the editor users the ability to click on the filled that has problem and change the color of the field to red. like the below screen shot.
My question is, how could I store the data about which fields are filled with wrong data (The field that are showing with red background) and use this kind of meta data to create an edit page for end user to correct that fields?
Of curse there are a lot of way to do that. The easiest but in my idea not optimize way is to add Boolean fields match to the form fields in Data Base and submitting hidden field according to that Boolean fields to store the information that each field is correct(TRUE) or not correct(FALSE).
The forms that I am working on have about 174 field that most of them are varchar(254). So the size of data that is storing in data base is huge and I don't wanna make it larger.
Ultimately could you please help me. Is there any other way to store the jQuery actions of the editor users and show that stored data to end users.
I am looking for your help.
Bests.

Validate / restrict hidden fields for modification

Problem
I am developing an application (in PHP) in which, I will show the user a report of some derived values based on previous entries he had done and the user will check for correctness - if correct, then the user can press Save button to submit it to database. Otherwise, the user should be able to edit previous entries, but he should not be able to change derived values in that report.
For that purpose, I have enclosed these values in a hidden input field, so a normal user can not alter these values, but if web developers could inspect the element in their browser and change these values.
Can we prevent these type of attack? If so, how?
My thoughts
I can make a string of all hidden fields, encrypt it and save in another hidden field - after postback I can cross-verify actual value and encrypted value.
Is this the correct way to achieve this? and how to do this
What do you think?
It's very hard to get a good idea of what you are trying to do but perhaps a solution could be to display a form with input fields disabled (http://www.w3schools.com/tags/att_input_disabled.asp) then have at the bottom of your page e.g. Is this information correct? .. Then 1 option to Save, and one option for No/Edit. Then do an if(isset($_POST['edit'])) { .. run form again but with input fields not disabled }
Only have your SQL update code in the 'edit' section of the code, and have a seperate SQL update code for the save section which just moves the values from wherever you are grabbing them from, to wherever they need to be.
Hope that helps.
If there are derived values that you need to fill out your form, but don't want to use hidden fields - I suggest using session variables that would contain those derived values as needed. Depending on how many of these forms there are, and how many users will be using them - the overhead is usually negligible with this number of fields (in the 30s)... if you discard them when not needed of course.

PHP save dynamic fields to MYSQL database

I have a form with fixed elements like radio button, text boxes, drop downs, etc.
There is also an area of dynamically generated dates and text boxes passes to the Post as an array to be saved into a MYSQL database.
The form is quite big and I would like to give the user the possibility to save it avoiding to have to retype data all the time.
I would also like to give the user the possibility to save their choices by storing a date and a description of their selection.
The problem is that I don't know how to tackle this problem as I don't know how to handle the dynamic part of the form. i am trying to generate a sort of automatic process but I cannot think of a workable solution.
Any idea?
If by dynamic you refer to xml requests ( ajax ) then you can easily save all the data in session cookies, as this very site does when you're asking a question. :P

how do retain value of dropdown box in php

I will try to make it simple as possible. I know how to select a default value in dropdown. My problem is how to make the last option in the dropdown as default "interactively" for next time. so that means that I cannot hardcode the default value.
Lets say I want to multiply 2 numbers. I have two drop down boxes and contain number 1 to 10; at the very first time it will say 1 and 1 since if no default is mentioned, then the default is the number at 1st position. Lets say if somebody picks 2 in the 1st and 6 in the second. Once the user hits calculate, the page will refresh, the answer 12 will be displayed. I still want 2 and 6 stil to be there, and NOT go back to 1 and 1
Please note that if I have action set to "answer.php", and display the answer on a new page and have a back button to come back to initial page. There is no problem. but I don't want that. I want to stay on the same page.
I am not using mysql. All have is a simple for loop and I dynamically load values 1 to 10
I believe I need something like selected = ?????? please fill the blank and where would I put this in the for loop
I have spent whole day on this on google but no luck to what I want
Thanks
Amit
As I see it, there are several ways to do this:
On the page that receives the form data, check which option the user selected and store it in a cookie. When you generate your form, check if this cookie is present; if so, use its data as the default value. This is probably the simplest option.
Store the selected option in a PHP session variable. This will work the same way as #1, except default values will not be saved if the user closes his browser.
Use a login system and store the default value in a database.
There are many ways to do this, the differences come down to how 'deep' you want the storage to be. Without wasting too much time here is a quick list of storage options:
POST vars (this is the solution that best suits your scenario)
Cookies
Session
Database
Server Filesystem
etc etc
Basically the simplest technique is to take the data that is passed and redisplay it, so for your form you have 2 inputs, leftval & rightval, the user hits submit on the form and you multiply them to show the result.
When you build the page to show the result use the $_POST['leftval'] & $_POST['rigtval'] to populate them on the new page. Make sure to sanitize the values and escape them on the way out!

Multi page ordering form php or ajax

I'm looking to create a multi page ordering form the first page would contain some dropdown and text fields, the second page would contain more text fields, the third page would be an order summery with paypal payment option.
I'm just wondering what the best way to create this order form is. I've used sessions in the past but never with users entering in text and picking items from drop downs, does anyone have any resources for doing this? Or does anyone know of a jquery or other ajax example or plugin I might be able to use and modify.
any insight would be a big help.
thanks
The simplest technique might be to use hidden form fields to carry fields from previous screens through to the final screen.
Just make sure you validate all the values when the final screen is submitted to make sure that the user hasn't twiddled the data.
You don't need to do pagination at all if you don't won't to. Just use css to show/hide the "pages". It doesn't sound like you have to save the "state" at any point.
But if you want to do multiple pages, use a session or a cookie to track the user. Then save the data to a database a mark it as incomplete. On the final page, retrieve it all and show it on the page. The server can't tell if a request is ajax or not, so it doesn't matter what you use for submission.

Categories