Saving form values dependant upon onclick/onchange functions - javascript

I have a form that saves user entered values on submit with php.
Some of the fields are in div's that are display:hidden until an onclick or onchange function changes that div to show.
How can I show only the divs containing fields with saved values after the form has been submitted? I have saved the values in the always visible fields but cannot trigger their functions.
I am using very little jquery because I am new to the syntax and would prefer to implement solutions I can understand and adapt. Simple jquery is acceptable if it is a better/quicker/easier solution.
Thanks
Code Example:
<input type="radio" id="customer" name="jobtype" value="customer" onclick="getJobType()" autofocus>Customer
<input type="radio" id="store" name="jobtype" value="store" onclick="getJobType()">Store
<span id="customerjobs" style="display:none">
<select id="customer" name="customer" onchange="createJobsList(this.value)">
*various options*
</select>
<span id="jobslist"><br></span>
</span>
The first span (id=customerjobs) is initially hidden. Upon selection from the radio's, all but the corresponding span is set to display:none and the selected is set to display:block. On submit, the selected radio is saved, but the onclick isn't called to show the span.
The second span (id=jobslist) content is populated by innerHTML using the results of an ajax call to PHP when a selection is made. On submit, the selected option is saved, but the onchange isn't called to fill the span.
So I need to trigger the onclick of a saved radio value to show my content and trigger the onchange of a saved select to populate additional content.
Note: I used onblur with javascript to set the focus initially so any action would trigger the content but it caused an unnecessary pause in filling the form that I didn't want.
Page loads with only a radio selection.
User clicks radio button.
Onclick function changes style of span id=customerjobs to display:block.
The select input inside the span is now visible. The user selects an option.
Onchange function makes an ajax call to request information from the server which is placed in span id=jobslist.
User submits form to same page.
Form saves entered values so they are still selected when page reloads.
Onclick and onchange functions are not triggered by PHP saved values so steps 3 and 5 never occur. Page is left with only the radio buttons unless it is clicked again.
Well, I have a jsfiddle to illustrate my problem using default selections because I cannot use PHP to save entered values.
Imagine the form has just been submitted and the values saved are the checked radio button(customer) and the selection from the drop down(1) which adds the word "customer".
Ideally, the entire form would still be visible (The selected radio, the selected option and the content added to the last span "customer").
Instead, only the selected radio is visible unless it is clicked again to unhide the select drop down. The drop down retains its' value as well, but the content in the last span will only appear onchange.
http://jsfiddle.net/L5H2u/31/
Try it out and advance thanks for any suggestions.

Can you hook a function to onload that checks the radio buttons and simulates the click by calling getJobtype()? That will get the initial case where the radio button is already the way the user wants it. Further clicks will work as you planned.
Edited to add: If I understand you right, all is well the first time the page is loaded because the user has to click something and that runs your getJobType() function. However, when the page is reloaded, the correct radio button is already checked, the user doesn't change anything, and your function doesn't run. If that's correct, running getJobType() from onload should fix it.
You may need something like <input type="hidden" id="firstrun" value="true"> The PHP would set that to false on subsequent loads of the page, and the onload function would only make things happen if "firstrun" was false. Edit: You don't need this because getJobType() has no default action; keep reading.
Edited still more: You have checked="checked' on the Customer radio button, so if the user is a customer, even the initial run doesn't reveal the hidden material.
Remove checked="checked" when the page is initially loaded so that on the initial presentation, neither button is checked.* Then add window.onload=getJobType; to the end of your JavaScript.
On the initial load, getJobType() will do nothing since it checks both buttons and has no default action. When a button is clicked, getJobType() will run and act based on the button that was clicked.
When the page is loaded a subsequent time, one of the buttons will be checked and when onload runs getJobType() it will perform the corresponding action.
The radio buttons, SELECT values, and any other form elements that are preserved and "reflected" by the server-side PHP will be correct when the form is loaded the second (and subsequent) times. Where you need an onload JavaScript function is when one of those values also changes something else, such as making a hidden DIV visible. If there are functions other than getJobType() that manipulate the DOM, it will likely be cleaner to write an init function that sets up the DOM based on the values of the form elements as preserved by the PHP process.
* I normally advocate having some button checked by default so that the user can always get back to the initial state. This case seems to be an exception.

Related

Change default "selected" option in select box after form submission with JavaScript (in Node/ejs file)

How can I make the selected option in a select element change dynamically, with the submission (after, really) of an input form which itself depends on the value of the select element?
details:
The app is a grocery list with a single input form. A select element with named categories ("fresh", "grocery," "beerandwine", etc) is there. I take the values and use them to route the form, via post, to my node/express app ("/list/fresh/add", "list/grocery/add", etc.). Everything works except for a UI issue I can't crack.
The select element has the "fresh" option selected by default:
<option value="/list/fresh/add/" selected>Fresh items</option>
I want this default selected attribute to change dynamically. So, say someone changes it to "grocery" and inputs "Wheaties," I want the the default option to change to "grocery". Ie:
<option value="/list/grocery/add/" selected>Grocery</option>
I can't figure out how to make this work dynamically. Nothing happens when I put it in the (one) function I have going on the page, which is what changes the form action depending on the selected option.
function changeFormAction(){
myForm.action = select.options[select.selectedIndex].value;
}
submit.addEventListener("click", changeFormAction);
I have tried making another function and calling that from inside the changeFormAction, but that didn't work, either. I also tried putting an event listener on the form, but I just couldn't get it to do anything.

Disable checkbox by passing the value

As the title suggests, I have a form, which as soon as the user registers he is forced to compile, then the user can return to this form, and obviously the form on the screen will present the choices that the user has previously made, now to make that the checkbox is enabled if the user has previously pressed on the checkbox, I succeed with this code here, and the checkbox, appears to me on the selected screen:
<input <?php if (!(strcmp($row_mostra_tabella_email['aut_privacy'],"S"))) {echo "checked=\"checked\"";} ?> type="checkbox" id="aut_privacy" type="checkbox" value="S" name="aut_privacy" >
the problem is that if I do the same thing with the disabled command, when the user presses enter, I don't pass the command value = "s".
I would just like the checkbox to be selected and disabled, how do I do it?
As you have discovered, when you disable the element, it is not sent to the server.
There are a couple of things you could add to the element that would prevent user interaction. One is the style property pointer-events and the other is the element attribute tabindex
If the input element has style property pointer-events: none, the user cannot access it with mouse click (or touch). WARNING: If the input has a label, the label is "clickable" too, in that case, suggest wrapping both in a <div> with that style property.
That will prevent the user from directly interacting with the element, but there is always tab. Use tabindex=-1 so that the checkbox does not "participate in sequential keyboard navigation".
Having read at Dino's answer I realise what you mean (I think). The simplest way to do that is to display the checkbox as checked and disabled then add a hidden input with the correct name and value, that way the user cannot interact with the checkbox, but the value is sent back to php in the form submission. If this isn't what you mean then you do need to re-write your question

clear dropdown values before form submit

I tried lot of posts but none seem to work for me.
I have 2 drop downs inside same form element and they should work in toggle manner, with following properties:
selecting any drop down should clear the other one and auto submit the form.
Also on form submit I am retaining the value of selecting drop down to display user what value had been selected.
For auto submit I have used .change and fformid.submit.
But issue is if i selected dropdown 1, form get submitted and page is displayed with dropdown1_value 1 selected and now when i make selection of 2nd drop down, both fdropdown 1 and dropdown 2 value gets submitted.
I tried using dropdown2_id.remove(), .emtpy(). .val("").
All these seem to visually remove elements, but form gets submitted with the value previously selected.
And i dont want to use Ajax, since I am using Django, doing Ajax will return result to js which is difficult for me to render than using html itself.

jQuery change function, how to save the state like a session

I am looking for a way with my form I am currently showing and hiding fields based on the values selected in the dropdowns, What I want to know is.
when i select yes and the field below displays I click submit on the form, if I return to the form the value is still present but the field is hidden again...
How can I prevent that from happening by default?
I want my browser to remember the jQuery change funtions state I left it at after I submit the form.
What you want to do is 'refresh fields visibility' in some cases. I suggest you to create such function refreshFieldsVisibility. Such function reads values from the dropdown and shows/hide the proper field. Then call your function:
When elements state is changed, with on('change') events.
When document is ready (this is your case as I understand), with $(document).ready
Any other situation if necessary

Bound cfselects' onClick causing issue

I have a cfselect which is working fine. I have an onclick event to refresh the binding to the cfselect because the query contents keep changing quite frequently and I want the users to have the updated list.Now i want to add "Select any Bus" as the first option which I am not able to do.
I tried adding a row to the query result ahead of it being returned from my cfc. But I am not able to select any other value other than 'Select any Bus'.
My cfselect code is:
<cfinvoke component="getCalculatorData" method="getAllBus" argumentcollection="#houseArgs#" returnvariable="qry_busList">
<cfform style="align:centre" id="frm_drpDwnBus" name="frm_drpDwnBus">
<cfinput name="hdnrr" value="#rrSbstring#" type="hidden">
<cfselect name="dpDwnBs" bindOnLoad="true"
bind="cfc:getCalculatorData.getAllBus(hdnrr.value)"
value="busType_id_pk"
display="busType_name" >
onclick="javascript:refresh(hdnrr.value)"
<option value="0" on>Select a Bus</option>
</cfselect>
</cfform>
My cfc:
SELECT busType_id_pk
,busType_name FROM tbl_bustype
WHERE busType_railroad_letter=
javascript:
function refresh(s)
{
ColdFusion.Bind.assignValue('dpDwnBs','value', dataCalcu.getAllBus(s))
}
As soon as i remove the onclick event all works fine. Please help me out here.
Having misread the question originally, let's take another swing.
A standing onclick bind event on a select box will never work. To use a select box the way most people do, you click it twice: once to drop it down, and once to select your item. However, on the first click you reset the item. Even if the box stays open (I suspect this might be browser dependent, but hanven't really tested), the second click will also reset it, meaning that you'll lose your selection, as the selected item is destroyed and recreated as a new DOM node.
I can think of three solutions.
First, you can add a refresh button to the form, and refresh the values in the select box then.
Second, you can use jQuery or similar to bind the event, and then onclick refresh the option list and then remove the onclick event binding. This means that the user can only refresh the options once, but (hopefully) that should be enough. If the page uis used multiple times (via AJAX for example) you can rebind the onclick refresh event on submission.
Finally, display the form item as an uneditable text field (onfocus="blur();" IIRC). Add a button, or change the focus event to launch a modal window that pulls the options from your CFC via AJAX or similar and displays them, so that anytime they choose to edit this item, they get a new set of choices.
All of these have the disadvantage of not preserving previous selections, as the bind refresh destroys all the option odes and builds a new option list.

Categories

Resources