Storing Large Form Data JavaScript - javascript

I am learning JavaScript to make a large form that stays completely local( no web server involved what-so-ever ). I am very familiar with PHP, but JavaScript is new to me. I have a form that has 100+ checkboxes and text fields. Some of the fields show as "[X] Other: [/text field/]" so when you select the "other" selection, you have to enter an input. I am making a validation to make sure you, A: enter text if you selected "other." B: make sure you selected the check box if you have entered text. When the validation function finds something wrong, it shows an alert box. Then, the page reloads( Not sure why ) and all of the data put into the form is wiped. So, I need to find a way to store this form data to load it upon page reload. I started with cookies, but then it came upon me that there is limits to the number of cookies and their sizes. What else can I do??? Please help!

Your submit button is reloading the page.
For example, if your form is set up like this:
<form name="main" method="post">
<!-- Lots of input fields -->
<button type="submit" id="subbutton">Submit!</button>
</form>
This is doing a POST to an undefined server because your form has no action
If you're sure that you don't need any server interaction at all, it's not necessary to use a form element
This means you can remove the form and submit elements from your page and use a button element instead
<!-- Lots of input fields -->
<button id="clientaction" type="button">Click me</button>
From MDN:
The HTML element represents a document section that contains
interactive controls to submit information to a web server.

Related

jQuery Submit Hidding Field on Page Load

I am trying to add some additional tracking information to a site. I want to place a hidden field on every page that pulls in the URL of the initial page the person landed on, and submit that value to a form on another page, without ever leaving the landing page.
So for example, someone lands on a blog post and this hidden field submits that value to the form but the client never leaves the page. This person clicks through to various areas of the site, and decides to submit a form. That form submission will then contain the value of the initial page that was landed on. This would only be able to happen on the initial page though, because if it re-submits the value on every page the person visits it will not show the initial page the person started on.
Hopefully that makes sense. Any ideas, or am I pretty much just looking at buying expensive software that already does this type of thing?
$(function () {
$('#form-track').submit();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="/schedule-service" method="post" id="form-track">
<input name="form-source" value="THIS URL IS DYNAMICALLY GENERATED" type="hidden">
</form>
Try this... the value will then be set in a cookie called "landingURL"
if (document.cookie.indexOf("landingURL=") < 0) {
URL = insert URL HERE...
document.cookie = "landingURL="+URL;
}

Jquery form submit behavior not understandable

I am writing code for a small webproject using js and jquery. In it, at some point, onclicking a button, i create a dialog. the dialog has a form within it with a name field and some number fields. I am supposed to check user inputs and send them to server, along with appending the name field to a list in the browser, to intimate user, one more item has been added. Two strange things are happening -
1) After posting the form, the dialog box closes on its own without me issuing a dialog('close') anywhere in the submit button handler.
2) The name entry doesn't get appended to the list. Its as if the whole page refreshes after the submit. With the original default entries of the list of names.
Anyone has any ideas on why this is happening? Would post some code for your aid.Please don't suggest to use Ajax instead. I think this reflects some fundamental flaw in my understanding of JS ways and would like to clear it first than just switching to some other technology.
<div id='dialog' title='Define New Matrix'>
<form name='form1' id='form1' method='post'>
<fieldset>
<label for="Name">Name</label>
<input type='text' name='nameofmatrix' id='Name' class='whitepanes'><br>
<label for="a11">a11</label>
<input type="text" name='a11' id='a11' class='whitepanes number-field'><br>
<label for="a22">a22</label>
<input type="text" name='a22' id='a22' class='whitepanes number-field'><br>
<button id='submit_button'>Submit</button>
<button id='cancel_button'>cancel</button>
</fieldset>
</form>
<p id='tip' style='color:red;'><i>All fields are required</i></p>
</div>
<script>
//#button_define is a button on whose clicking the dialog opens.
$('#button_define').click(function(){
$('#dialog').dialog('open');
$('#tip').html("<p style='color:red; font-size:small'>All fields are mandatory</p>");
});
$('#submit_button,#cancel_button').button();
$('#cancel_button').on('click',function(){
$('#dialog').dialog('close');
});
$('#submit_button').click(function(event){
event.preventDefault();
var name=$('input[name=nameofmatrix]').val();
//Validate is a function which returns a bool if validation proceeds correctly
var isCorrect = Validate();
if(isCorrect){
//if validated correctly then add to list
$('#form1').submit();
//$('#dialog').dialog('close');
$('#selectable').append("<li class='ui-widget-content'>",name,"</li>");
}
});
</script>
Its as if the whole page refreshes after the post. with the original entries.
That's precisely what happens. Though I'm not sure where you're submitting the POST request to since there's no action attribute on your form. But a standard non-AJAX request triggered by a form sends the request to the server and then renders the response from the server. If the response is this same page again, then this same page will be rendered again.
JavaScript isn't going to remember the state of the previous page when it loads this new response. Even if they're the same page, they're two separate responses from the server. So...
1) After posting the form, the dialog box closes on its own without me issuing a dialog('close') anywhere in the submit button handler.
The dialog isn't closing. After the page refreshes you're in an entirely new page context. It didn't close, it just hasn't been opened yet in this context.
2) The name entry doesn't get appended to the list.
There's nothing that would cause this to happen when the page loads, so in the new page context it doesn't happen. Your server-side code would need to include this content in the response to the POST request.
I think this reflects some fundamental flaw in my understanding of JS ways and would like to clear it first than just switching to some other technology.
Included in that misunderstanding is the fact that AJAX is part of JavaScript. (The "J" in "AJAX" stands for "JavaScript.") It's not "switching to some other technology." It's taking advantage of the capabilities of the technology you're already using. All AJAX does, really, is send requests and receive responses to/from the server without refreshing the page context.
You are not properly appending the name. The concatenation operator is not a comma, but a + in javascript:
$('#selectable').append("<li class='ui-widget-content'>" + name + "</li>");
Next, the form refreshes because you are submitting the form using $('#form1').submit();. If you do not want the page to refresh while submitting, use ajax.

Submit hidden fields in a html form

For a basic HTML form, I would like to seperate the form into three tabs, each tab will contain certain fields, and when submit the form I wish all data in the three forms will be able to submit.
So I have a menu created by <ul> and <li>
<ul class="subnav">
<li class="subnav0 current">Tab1</li>
<li class="subnav1">Tab2</li>
<li class="lastItem subnav2">Tab3</li>
</ul>
and below this menu, I have three divs that represent each of the tab:
<div class="tab1"></div>
<div class="tab2 displayNone"></div>
<div class="tab3 displayNone"></div>
The input controls elements will be put into each of the tab divs. And the javascript in the menu nav bar will control which tab to display by call show() & hide() method of each div. (Using jQuery).
Now my problem is:
1) I want to be able to submit the whole form (all controls within three divs). However, html forms won't submit input controls within a displayNone div, which means I will only be able to submit the data within the tab which I am currently viewing but not the other two tabs.
2) I also want to do some javascript functions on hide elements when initialize the form in tab2 or tab3. However, since they are display:none, the javascript will not have any effect.
So is there any way that I can somehow hide the div, but also be able to submit the form and do any javascript operation on it?
According to the W3C display:none controls may still be sent to the server, as they are considered successsful controls
17.13.2 Successful controls
A successful control is "valid" for submission. Every successful
control has its control name paired with its current value as part of
the submitted form data set. A successful control must be defined
within a FORM element and must have a control name.
However:
Controls that are disabled cannot be successful.
If a form contains more than one submit button, only the activated
submit button is successful. All "on" checkboxes may be
successful. For radio buttons that share the same value of
the name attribute, only the "on" radio button may be
successful. For menus, the control name is provided by a
SELECT element and values are provided by OPTION elements. Only
selected options may be successful. When no options are
selected, the control is not successful and neither the name nor
any values are submitted to the server when the form is
submitted.The current value of a file select is a list of
one or more file names. Upon submission of the form, the contents
of each file are submitted with the rest of the form data. The file
contents are packaged according to the form's content
type. The current value of an object control is determined by
the object's implementation.
If a control doesn't have a current value
when the form is submitted, user agents are not required to treat it
as a successful control.
Furthermore, user agents should not consider the following controls
successful:
Reset buttons. OBJECT elements whose declare attribute has been set.
Hidden controls and controls that are not rendered because of style
sheet settings may still be successful.
For example:
<FORM action="..." method="post">
<P>
<INPUT type="password" style="display:none"
name="invisible-password"
value="mypassword">
</FORM>
will still cause a value to be paired with the name
"invisible-password" and submitted with the form.
In any case if that doesnt seem to be working why not try jQuery serialize() or serializeArray() on each form and concatenate the values and ajax them back to the server.
On your first point, just because an input is display none, doesn't mean that it will not submit those fields.
On your second point, I don't quite follow. Are you saying that when you open one of the tabs, you want to do some action on the content? If so, then JQuery UI allows you to do this:-
http://jqueryui.com/demos/tabs/#event-show
Can you give a more complete example, including the form tag and some inputs?

HTML Nested Form Problem

I'm making a web page using CGI scripting which has a form users need to fill out. The general layout is:
<form>
textfield (username)
textfield (password)
textfield (email)
submit button
</form>
What I would like to do is add a button that checks to see if the username they've entered is available. My problem is the way I'm trying to go about doing this is by writing:
<form>
<form>
textfield (username)
submit button
</form>
textfield (password)
textfield (email)
submit button
</form>
This doesn't work, the submit button instead submits the outer form. Here are the things I've considered trying but have not worked:
Put a form at the end of the first form. Problem: I have no idea how to align the "validate" button next to the username text field button without making it float which causes a bunch of other issues with the page.
Put values on the submit buttons and make the submit do different things based on which button was clicked. Problem: the web page that I want to make a "POST" request to is different based off which button is pressed. Seeing as I put the action="mypage.cgi" in the portion of the code, and not the button portion, I don't know how to make it go to different sites based on which button I press.
First of all it is a good idea to give all forms names.
So you can easily distinguish between forms.
Next, attach onClick even to each button that would call a function with a different paramenter: 1,2,3. Each button would send its own parameter. In the function you just look at the paramenter and submit appropraite form.
<form name='form1'>
....
<button type="button" onClick=doIt(1);>Submit</button>
</form>
<form name='form2'>
....
<button type="button" onClick=doIt(2);>Submit</button>
</form>
<form name='form3'>
....
<button type="button" onClick=doIt(3);>Submit</button>
</form>
<script>
function doIt(formid)
{
if(formid==1)
{
document.form1.submit();
}
if(formid==2)
{
document.form2.submit();
}
...
}
</script>
Have multiple submit buttons with different names. Check for each on the post back.
The approach that I have used for nested forms is to use tag instead of tag,
and then appending the form tags at the time of clicking submit buttons.
I have written a small JQ Plugin-'dynaForm' for the same, and its very easy to implement.
Please refer to ==> http://anupampdhyy.wordpress.com/2014/09/29/dynaform/
and
you can also watch a demo for same at :
https://www.youtube.com/watch?v=CFQia8EsoPQ&feature=youtu.be
I hope this helps you to implement nested forms in your HTML code. :)
You probably need to make two different form actions, so that you don't submit two completly different values.

How do you write strings to the middle of a web page?

I'm trying to have users enter info into a form (via radio buttons), manipulate the input data, and write resulting text onto the middle of a web page--beneath the radio buttoned form. So I have variables assigned to whenever a user selects a radio button, the onClick event calling a function something like:
function saveValue1(value) {
someVariable=value;<br>
}
And when users click a Submit button, a function works like it's supposed to, ultimately writing an output string. The problem is how to write the string value in the middle of the page. I have this [pseudo]code at the end of the function (pretend the string I want to write to the page is named aVariable):
document.getElementById('aPlace').innerHTML=aVariable;
And of course there's HTML in the displayed page like this:
<div id="aPlace"></div>
After a user pressed the form's Submit button the correct output variable is displayed very briefly, and then disappears. Why is this? And how should I be writing this code instead?
Thanks for helping a newbie, as always.
The form is probably submitted. put a "return false" at the end to stop it submitting the form
It seems that the browser is refreshing? How is the form data handled?
If the form is needed only to add the text to the page, I would add a button
<button onclick="saveValue1("+value+");")>
and avoid submitting the form.

Categories