I have a PHP application form for medical health plans where the user fills in his details and selects some options from various controls, one of which is the plan type (private of family plan). Sometimes, although the user fills in his siblings/spouse details, they forget to also select the 'family' option and leave the drop down at it's default value which is the 'private' plan.
How can I make sure that if the user fills in his sibling's details, that the 'plan' dropdown will change to the proper option? I know this can be done easily with js but I'm not sure what event I should call it on, onFocus and onClick won't work because the user might simply click on the Sibling Name field but not enter a value, fill in something but later on clear it out etc.
I think that a script that counts the length of the 4 available siblings fields and if one of them is not empty, then change the 'plan' select value from 'private' to 'family'.
Any ideas would be great, thanks.
You've givin the answer partially yourself: you'll need a function that checks if the fields are not empty (or some other random condition). Lets call that function checkCondition()
Next you'll need a validation function that calls on it and changes the fields if appropriate, you can also combine these two functions but the code usually looks cleaner if you seperate them. E.g. ensureValidFieldPlan()
Then you can hook that function to run on the appropriate times. The simplest way to do it is in the forms onChange. A better way to do it would be to hook it to the appropriate form field, but for simplicity sake lets hook it to the form.
Your total code will look something like this:
function checkCondition(){
return form.field1.value.length != 0;
}
function ensureValidFieldPlan(){
if( form.plan.value != 'family' && checkCondition()){
form.plan.value = 'family'
}
}
form.onchange = function(){ form.onChange(); ensureValidFieldPlan(); };
Note that this does not guarantee that a browser client will transfer a valid form (e.g. it can be spoofed), you should always also perform server-side validation.
Depends on how you want to handle it, server side? Indeed, detect the
strlen($_POST['siblingname']) > 0
and then set your plan automatically to familly from the php code before processing something else.
If you want to do it on the client side. I suggest to use the popular JQuery library and intercept the value change event on the sibblingname field. Then, in the event handler, check the length of the sibbling name field value, if there is something, just set the plan to family.
Good luck
Related
This is the situation, I've got a big array ( 280 items ) which creates a really big form by going trough loops.
I want the user to be able to disable certain items, so they can 'pre make' their form ( and skip the not used ones ), You are probably going to say i can just remove what's not needed. But they need different items from the array every time they use it.
So how would i make a page where they can just use checkbox to 'change' the form. ( if possible at all.)
Thanks,
Mike
edit:
Did not think of sharing code, here:
The array : http://pastebin.com/EnwHsqtK
the form : http://pastebin.com/y2XSFBG4
the page which makes a .txt file from the given answers. http://pastebin.com/UaUcsj2z ( not sure if anyone would need this. )
Might be a bit messy. I'm new in PHP, and only programming for a year. Also dont mind the language please.
If you want to permanently record form preferences/settings for each user, you'd want to create an additional table or column(s) in your database for this, give the users additional checkboxes on the form to indicate their preferences, receive this input and store it in the database, and of course finally disable certain fields based on their settings.
But if you just want to give the users a temporary way to disable certain fields (with no preferences saved permanently), you would use JavaScript in your output. You would add more form controls (checkboxes or buttons or whatever) to the HTML and then add JavaScript code snippets into that HTML to disable form elements when the user clicked on the controls. This kind of making changes when users click is called "triggers that fire based on events". The most commonly-used event is called "OnClick()" and the JavaScript code for it will execute when a user clicks on something.
Many folks who use JavaScript also find it helpful to use the functions in the jQuery library instead of raw JavaScript. To do this, you just add one line of HTML to the top of your page to include the JavaScript libraries from a publicly-hosted server, then you can use jQuery commands in your page.
The only thing to remember when you first start using JavaScript/jQuery is that it only runs on the client browser - its code cannot talk directly to the server, the database, or many things you can access in PHP. JavaScript/jQuery is specifically used for making your HTML pages more interactive. Plain HTML doesn't have much razzle-dazzle. JavaScript allows users to do things like enable and disable form fields on-the-fly.
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.
I want to know that is there any way to add own javascript code which will be execute in the form processing of Alt-Doc.php.
I need to change a selectbox value depend on other selectbox(select event).
Is it possible ??
You can use the $TCA['tt_content']['ctrl']['requestUpdate'] = "yourfieldname" to let TYPO3 add the js code (onchange event) to the given field. You can then write different palettes to yout TCA with different values. It's done this bay by tt_content (field CType) and also in the pages table (type sysfolder vs. "normal page").
The other form elements completely change in those two cases. Of course, this will cause a reload of the whole form, so you should place a field with this behavior at the top of the form.
On php page in a form, One combo box has list of my Customer from mysql table customer. Another combo box has invoiceno stored in invoice table which has respective customer records. I want to select customer from first combo box and filter invoiceno from the second one according to the customer. Any one help me for php or java or Jquery or both codeings? Means if I select customer1 then in the second combo box should show all invoiceno respective to the custermer1. No Refresh or ReLoad or Post form Pl. If I get the first selection in a php variable format example $customer, it is enough for me. Thanks for any one help me.
Based on what is given, i think one is only restricted to pushing you in the right direction. In a case where you there a large number of customers, it most likely be that you are working with a database, thus the following process:
The Page of the selects is where you will need to make an AJAX request on change of the select element.
Using your JavaScript of your favorite JavaScript library you'll make the request passing a value that you'll query your database for.
Ofcourse you'll need to configure your PHP for GET or POST depending of you AJAX request, then query the database.
Format the databases output to be a valid HTML of your selected element.
Earlier you'll have configured your AJAX script to populate the proper element once the request has been successful.
You dont want to POST to the script. You dont want to invoke any server side activity. And you want to get the value user selects into a PHP variable. From what I understand, this means, you dont clearly get where PHP plays a role. The way you want it, you might want to use XAJAX : http://www.xajax-project.org But even this causes various REQUESTs to the PHP script internally.
I would suggest the below:
Do a natural join in the SQL query.
For a customer C1, there might be 100 Invoice numbers I1. The result of the query may be outputted in a JSON format. Something like this:
"RESULT" : [
"C1" : ["I1", "I2", I3"],
"C2" : ["I11", "I22", I33"]
]
ALL the data will be sent to the browser. This data can be stored as a Javascript Object. Use JSON.parse("<PHP response here>");
Whenever the users selection changes in a combo box, have a function in Javascript to load the corresponding values in the second list.
EDIT: In case, you are dealing with a larger database, and you expect a larger dataset, I would HIGHLY recommend XAJAX -- Simple and easy! :-)
If AJAX is not an option, you must load all the things (include customer & invoice), then you can use pure javascript/css to do that. It's a dirty work around, but it works.
First, let says that you have n customer, so you will have 1 combo box to select the customer; and n combo box for the invoices that associated with them. Those invoice combobox may have id = their ids in database.
Hide all the invoice combobox, with css : display: none
Use javascript: onchange to get the change event in the customer combobox, then show the approriate invoice combobox(display:block). This can be done easily by css property of Jquery, or simply manipulate by javascript replace function (to replace the html class of the combobox)
I'm sure that this way works, but the price is that you must load all data of customers & invoices, which maybe huge. So that if possible, you should try the AJAX approach like Thrustmaster & Babiker propose.
Currently, when I design my forms, I like to keep the name of the submit button equal to the id of the form. Then, in my php, I just do if(isset($_POST['submitName'])) in order to check if a form has been submitted and which form has been submitted.
Firstly, are there any security problems or design flaws with this method?
One problem I have encountered is when I wish to overlay my forms with javascript in order to provide faster validation to the user. For example, whilst I obviously need to retain server side validation, it is more convenient for the user if an error message is displayed inline, upon blurring an input. Additionally, it would be good to provide entire form validation, upon clicking the submit button.
Therefore, when the user clicks on the form's submit button, I am stopping the default action, doing my validation, and then attempting to renable the traditional submit functionality, if the validation passes. In order to do this, I am using the form.submit() method but, unfortunately, this doesn't send the submit button variable (as it should be as form.submit() can be called without any button being clicked). This means my PHP script fails to detect that the form has been submitted.
What is the correct way to work around this? It seems like the standard solution is to add a hidden field into the form, upon passing validation, which has the name of form's id. Then when form.submit() is called, this is passed along in place of the submit button. However, this solution seems very ungraceful to me and so I am wondering whether I should:
a) Use an alternative method to detect which form has been submitted which doesn't rely rely on passing the submit button. If so what alternative is there? Obviously, just having an extra hidden field from the start isn't any better.
b) Use an alternative Javascript solution which allows me to retain my non-Javascript design. For example, is there an alternative to form.submit() which allows me to pass in extra data?
c) Suck it up and just insert a hidden field using Javascript.
UPDATE:
I've accepted the correct answer but I just wanted to clarify my mistake here so it would be more helpful for others. I use Mootools and I very naively believed that when I used addEvent('submit' ...) I needed to immediately call event.stop() in order to prevent the from from being submitted. Actually this is not the case and I can just call event.stop() only if validation fails. Otherwise, the default submit gets fired, as usual, and using form.submit() becomes completely unnecessary.
You could send the forms to different handlers with action=file1.php and action=file2.php.
Are they processed using a bunch of the same code? Put that into separate files, include the commonalities, and write the unique bits in each of the handling files. Don't hack, organize.
For Javascript validation, don't halt the default action then resume, instead do this:
if (validation != valid) {
return false;
}
That way if JS is turned off or the validation fails, the form action/event is intact and it behaves as expected, otherwise it bonks. And certainly, certainly retain server-side validation. That's the "real" validation, the client-side is only to please the user and save them time. Never rely on it for YOUR sake.
You can customize the action of the form to add a get key/value; such as action="formhandle.php?formid=10"