html form wtih some inputs as JSON string - html

I have a servlet expecting two parameters param1 and param2. param1 must have a 'value' as JSON string whereas param2 'value' is a normal input. i.e.
param1 = {"id":"userid","name":"username","status":"userstatus"}
param2 = add
My question is how can I achieve this using the following html form
User ID: <input type="text" name="id"><br>
User Name: <input type="text" name="name"><br>
User Status: <input type="radio" name="status" value="active">Active
<input type="radio" name="status" value="inactive">Inactive<br>
Action: <input type="radio" name="param2" value="add"> Add
<input type="radio" name="param2" value="update"> Update
<input type="radio" name="param2" value="delete"> Delete
<input type="radio" name="param2" value="get"> Get<br>
<button type="submit" >Submit</button>

You cannot achieve it with only html, you need to use some javascript also.
Here is the sample code. (there must be json javascript library)
User ID:
<input type="text" name="id" id="userId">
<br> User Name:
<input type="text" name="name" id="username">
<br> User Status:
<input type="radio" name="status" value="active" id="statusActive">Active
<input type="radio" name="status" value="inactive" id="statusInactive">Inactive
<br> Action:
<input type="radio" name="param2" value="add" id="param2Add"> Add
<input type="radio" name="param2" value="update" id="param2Update"> Update
<input type="radio" name="param2" value="delete" id="param2Delete"> Delete
<input type="radio" name="param2" value="get" id="param2Get"> Get
<br>
<form action="MyServlet" onsubmit="return assignedBeforeSubmit();">
<input id="param1" type="hidden" name="param1" value="" />
<input id="param2" type="hidden" name="param2" value="" />
<button type="submit">Submit</button>
</form>
<%-- this will not work without needful json lib --%>
<script src="json.js"></script>
<script src="json2.js"></script>
<script type="text/javascript">
function assignedBeforeSubmit() {
var param2Add = document.getElementById("param2Add").checked;
var param2Update = document.getElementById("param2Update").checked;
var param2Delete = document.getElementById("param2Delete").checked;
var param2Get = document.getElementById("param2Get").checked;
var param2 = (param2Add == true) ? document.getElementById('param2Add').value :
(param2Update == true) ? document.getElementById('param2Update').value :
(param2Delete == true) ? document.getElementById('param2Delete').value :
(param2Get == true) ? document.getElementById('param2Get').value : '';
var statusActive = document.getElementById("statusActive").checked;
var statusInactive = document.getElementById("statusInactive").checked;
var status = (statusActive == true) ? document.getElementById('statusActive').value :
(statusInactive == true) ? document.getElementById('statusInactive').value : '';
var jsonData = {
"id" : document.getElementById('userId').value,
"name" : document.getElementById('username').value,
"status" : status
};
document.getElementById('param1').value = JSON.stringify(jsonData);
document.getElementById('param2').value = status;
console.log('status '+status);
return true;
};
</script>

Related

get HTML DOM index by Id

Eg. document.form[0].elements[23] will result some input <input id = test2> .
I need to get that 23 index value by id or name = 'test2' like
var n = document.getElementById('test2').getDOMNodeIndex(); // n = 23
This solution may be slightly clunky, but it works as demonstrated below.
I generated 25 input elements and gave them unique IDs.
Then I selected a random index among the Form.elements and printed it to the console.
Then I iterated over the form.elements until I found the matching .outerHTML values (JSON.stringified). When a match is found, return that index.
const theForm = document.forms[0];
const randomIndex = Math.floor(Math.random() * theForm.elements.length);
console.log(randomIndex);
const theInput = theForm.elements[randomIndex];
const formElementsIndexOf = (form, element) => {
let search = JSON.stringify(element.outerHTML);
for (let i = 0; i < form.elements.length; i++) {
if (JSON.stringify(form.elements[i].outerHTML) === search) {
return i;
}
}
};
console.log(formElementsIndexOf(theForm, theInput));
<form>
<input type="text" id="text0">
<input type="text" id="text1">
<input type="text" id="text2">
<input type="text" id="text3">
<input type="text" id="text4">
<input type="text" id="text5">
<input type="text" id="text6">
<input type="text" id="text7">
<input type="text" id="text8">
<input type="text" id="text9">
<input type="text" id="text10">
<input type="text" id="text11">
<input type="text" id="text12">
<input type="text" id="text13">
<input type="text" id="text14">
<input type="text" id="text15">
<input type="text" id="text16">
<input type="text" id="text17">
<input type="text" id="text18">
<input type="text" id="text19">
<input type="text" id="text20">
<input type="text" id="text21">
<input type="text" id="text22">
<input type="text" id="text23">
<input type="text" id="text24">
</form>

checkbox form validation - jquery

$(document).ready(function(){
$('#update2').click(function(){
var checkboxes = $('input[name="checkbox1"], input[name="checkbox2"]');
checkboxes.on("change", function(e){
checkboxes[0].setCustomValidity(checkboxes.filter(":checked").length ? '' : 'please select at least one option to procees')
}).change
});
});
$(document).ready(function(){
$('#select2').click(function(){
var checkboxes2 = $('input[name="choose1"], input[name="choose2"]');
checkboxes2.on("change", function(e){
checkboxes2[0].setCustomValidity(checkboxes2.filter(":checked").length ? '' : 'please select at least one option to procees')
}).change
});
});
<form>
<input type="radio" id="update" name="update-button" value="1">
<input type="radio" id="update2" name"update-button" value="2">
<input type="checkbox" id="check1" name="checkbox1" value="3">
<input type="checkbox" id="check2" name="checkbox2" value="4">
<input type="submit" name="submit-button">
</form>
<form>
<input type="radio" id="select" name="select-button" value="10">
<input type="radio" id="select2" name"select-button" value="11">
<input type="checkbox" id="box1" name="choose1" value="12">
<input type="checkbox" id="box2" name="choose2" value="13">
<input type="submit" name="submit-select">
</form>
Im trying to set custom validation to the checkboxes. so when i select the radio button with the id "select2", the checkboxes with diffrent name "choose1" and "choose2" one of them is required before submitting the form. it is working perfieclty in the first example with "update2" and "checkbox1","checkbox2" checkboxes. but i have no idea why it is not working in the second example with the "select2" and "choose1", "choose2" checkboxes.

html calculated field not accepting an empty field

I am currently trying to get my calculated fields to show a total in a bottom field but in order for that to happen I have to have a value in each field. I am lost on how to get it to work with an empty field being accepted without an error. This is what i have so far:
<script type="text/javascript">
function update()
{
var form = document.forms[0];
var sum = eval(form.x.value)*60 + eval(form.y.value)*110 + eval(form.z.value)*180;
form.sum.value = isNaN(sum) ? "" : sum;
}
</script>
<form name="form1" onsubmit="false">
<fieldset>
<legend>Bloodwood</legend>
<label for="f1">Vault ($60)</label>
<input id="f1" name="x" type="number" onchange="update()">
<label for="f2">Tray ($110)</label>
<input id="f2" name="y" type="number" onchange="update()">
<label for="f3">Tower ($180)</label>
<input id="f3" name="z" type="number" onchange="update()">
<label for="f4">Total</label>
<input id="f4" name="sum" readonly="readonly">
</fieldset>
</form>
I have removed eval from the sum and it works on tab out
function update()
{
var form = document.forms[0];
var sum = form.x.value*60 + form.y.value*110 + form.z.value*180;
form.sum.value = isNaN(sum) ? "" : sum;
}
<form name="form1" onsubmit="false">
<fieldset>
<legend>Bloodwood</legend>
<label for="f1">Vault ($60)</label>
<input id="f1" name="x" type="number" onchange="update()">
<label for="f2">Tray ($110)</label>
<input id="f2" name="y" type="number" onchange="update()">
<label for="f3">Tower ($180)</label>
<input id="f3" name="z" type="number" onchange="update()">
<label for="f4">Total</label>
<input id="f4" name="sum" readonly="readonly">
</fieldset>
</form>

Applying HTML5 Validation only if checkbox is checked

I have a form that offers a user a choice of whether they would like to receive Email updates. How do I go about making sure that the Email is only 'required' (HTML5 Validation) if the checkbox to receive Email updates is checked.
Here is the form:
<form>
<fieldset>
<legend><b>Details</b></legend>
<label>First Name </label><input id = "fname" type="text" autofocus="" placeholder="Enter first name" name = "fname"><br><br>
<label>Last Name </label><input type="text" placeholder="Enter last name"><br><br>
<label>Email </label><input type="email" placeholder="Enter valid email"> <button onclick="myFunction()">Help</button><br><br>
</fieldset>
<fieldset>
<legend><b>Rating</b></legend>
<label>Website Rating:</label>
<input type="radio" name="Rating" value="1">* (1 Star)
<input type="radio" name="Rating" value="2">* * (2 Star)
<input type="radio" name="Rating" value="3">* * * (3 Star)
<input type="radio" name="Rating" value="4">* * * * (4 Star)
<input type="radio" name="Rating" value="5">* * * * * (5 Star)<br>
</fieldset>
<fieldset>
<legend><b>Comments</b></legend>
<label>Additional Comments:</label>
<textarea id = "feedback1" name="feedback1" rows="2" cols="60"></textarea><br>
</fieldset>
<fieldset>
<legend><b>Updates</b></legend>
Do you want to receive updates via Email?<br>
<input type="checkbox" name="updateYes" value="Yes">Yes
<input type="checkbox" name="update" value="No" checked>No<br>
</fieldset>
<br>
<input type="reset" value="Reset">
<button onclick="myFunction2()" type = "submit">Submit</button>
</form>
<script>
function myFunction() {
alert("Please enter a valid Email adress into the 'Email' field");
}
function myFunction2() {
var checkedRadioButton, inputs, rating;
inputs = document.getElementsByName("Rating");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
checkedRadioButton = inputs[i];
break;
}
}
if (checkedRadioButton) {
rating = ("You have rated the website " + checkedRadioButton.value + " Star")
} else {
rating = ("Not Rated")
}
alert("Thank you for your feedback " + document.getElementById('fname').value + "\n" + rating + "\n" + "Your comment was " + document.getElementById('feedback1').value);
}
</script>

clear html form with angularjs

I want to clear my input values in html form when you press submit.
the function is in the controller.
i tried to do: $scope.name = "" or null (for every input) but its don't work.
This is the html file:
<label for="messageName">Message Name:</label>
<input type="text" ng-model="message.name" name="message.name" required/>
<label for="text">Text:</label>
<input type="textarea" ng-model="message.text" name="message.text" required/>
<label for="images">Images:</label>
<input type="text" ng-model="message.images" name="message.images" required/>
<label for="template">Template:</label>
<input list="template" ng-model="message.template" name="message.template" required/>
<datalist id="template">
<option value="template1">
<option value="template2">
<option value="template3">
</datalist>
<label for="millisecToShow">Millisec To Show:</label>
<input type="number" min="1000" max="10000" ng-model="message.millisecToShow" name="message.millisecToShow" required/>
<label for="timeFrame">Time Frame:</label>
<label for="TimeStart">Start Time:</label>
<input type="datetime-local" ng-model="message.startDateTime" name="message.startDateTime" required/>
<label for="DateStart">End Time:</label>
<input type="datetime-local" ng-model="message.endDateTime" name="message.endDateTime" required/>
<label for="days">Days:</label><br>
<form>
<input type="checkbox" ng-model="message.sunday" name="message.days" value="Sunday">Sunday<br>
<input type="checkbox" ng-model="message.monday" name="message.days" value="Monday">Monday<br>
<input type="checkbox" ng-model="message.tuesday" name="message.days" value="Tuesday">Tuesday<br>
<input type="checkbox" ng-model="message.wednesday" name="message.days" value="Wednesday">Wednesday<br>
<input type="checkbox" ng-model="message.thursday" name="message.days" value="Thursday">Thursday<br>
<input type="checkbox" ng-model="message.friday" name="message.days" value="Friday">Friday<br>
<input type="checkbox" ng-model="message.saturday" name="message.days" value="Saturday">Saturday<br>
</form>
<label for="screenId">Screen ID:</label>
<input list="screenId" ng-model="message.screenId" name="message.screenId" required/>
<datalist id="screenId">
<option value="screen1">
<option value="screen2">
<option value="screen3">
</datalist>
<button ng-click="createButton()">Create</button>
And this is the controller:
angular.module('CreateCtrl', []).controller('CreateController', function($scope, superService) {
$scope.createButton = function() {
superService.insert($scope.message).then(function (result)
if (result == "true") {
alert('Create successful');
$scope.tagline = 'Create successful';
return result.data;
}
else {
$scope.tagline = 'Cannot create message';
}
});
};
});
I want to remove the input after sending the file.
thanks (:
Considering that all of the fields refer to $scope.message.*, wouldn't it be easier to just clean up the whole message object like this?
if (result === "true") {
alert('Create successful');
$scope.tagline = 'Create successful';
// cleaning up
$scope.message = {
name: null,
text: null,
images: null,
template: null
};
return result.data;
}
In fact, if you want to wipe the whole object clean, if it were me I would just go for $scope.message = {}; and be done with it.