Converting values on HTML form to a string - html

<fieldset>
<legend>Making a String</legend>
Test1:<br/>
<input type="text" name="Test1Field"/><br/>
Test2:<br/>
<input type="text" name="Test2Field"/><br/>
Test3:<br/>
<select name="Test3Field">
<option value="test1">0</option>
<option value="test2">1</option>
</select>
<br/>
Test4:<br/>
<select name="Test4Field">
<option value="test1">0</option>
<option value="test2">1</option>
</select>
<br/>
Test5:<br/>
<select name="Test5Field">
<option value="test1">0</option>
<option value="test2">1</option>
</select>
<br/>
<br/>
<button> Submit </button>
</fieldset>
I'm fairly new to HTML and trying to make some simple applications. I want to be able to convert the values that are inserted into this HTML <form> into a string once the submit button is pressed.
For example:
"Test1FieldValue, Test2FieldValue, Test3FieldValue, Test4FieldValue, Test5FieldValue"
Is there any way that this can be done?

You can do this with javascript. Basically, add an eventListener on your button, find all fields (inputs and selects) and create the string. The following will do that for you (I've added some comments for what the code does):
document.getElementById("submit").addEventListener("click", function() { // add an listener to the button
var fieldset = document.getElementById("fieldset"); // find the fieldset
var asArray = Array.from(fieldset.querySelectorAll("input, select")).map(v => { // loop over all found input and select elements
return v.name + ":" + v.value; // return the name and the value
});
console.log(asArray);
var asString = asArray.join(); // join the resulting array to a string
console.log(asString);
})
<fieldset id="fieldset">
<legend>Making a String</legend>
Test1:
<br/>
<input type="text" name="Test1Field" />
<br/> Test2:
<br/>
<input type="text" name="Test2Field" />
<br/> Test3:
<br/>
<select name="Test3Field">
<option value="test1">0</option>
<option value="test2">1</option>
</select>
<br/> Test4:
<br/>
<select name="Test4Field">
<option value="test1">0</option>
<option value="test2">1</option>
</select>
<br/> Test5:
<br/>
<select name="Test5Field">
<option value="test1">0</option>
<option value="test2">1</option>
</select>
<br/>
<br/>
<button id="submit"> Submit </button>
</fieldset>

HTML:
<input type="text" name="textfield1" id="textfield1" value="value" />
JS:
var nameValue = document.getElementById("textfield1").value;

Related

HTML specified for the submit button, php post prameter not passed

If a class is specified for the submit button, the post method parameter is not passed to php.
<form class = "delete_button", action="deleted.php" method="post">
<select name="nation" size=1>
<option value="KR">KR</option>
<option value="JP">JP</option>
<option value="US">US</option>
<option value="UK">UK</option>
<option value="GM">GM</option>
</select>
<input name = "email" type="email" placeholder="Enter your email">
<input class="delete_button_content" style="color:white" type="submit" value="delete">
</form>
and when I delete class for submit, it's working. what's problem ??

can we show content of datalist in input field rather than it's value

I want my calculation to run without showing it to the person using it so I want him to see only the content of input option, not value
example:
<option value="66.48">25 LIGHT</option>
when someone select this option his input field should show 25 LIGHT instead of 666.48.
NOTE: I cant use select, I have to use datalist because I want the user to get options by typing, as there are 100s of them. can I do this with data list?
please guide.
my code:
<form onsubmit="return false" oninput=" totalamount.value = Math.round(((Item.value * Quantity.value - (Item.value * Quantity.value *Percentage.value /100))+ Number.EPSILON) * 100) / 100 ;">
Percentage<input name="Percentage" id="Percentage" type="number">
<br />
<section class="container">
<div class="one">
Item<br/><input list="items" type="text" name="Item" id="Item" width="70%">
</div>
<div class="two">
Quantity <br/><input name="Quantity" id="Quantity" type="number">
</div>
</section>
<br/>
Total amount <output name="totalamount" id="totalamount" for="Item Quantity Percentage"></output>
</form>
<datalist id="items">
<option value="66.48">25 LIGHT</option>
<option value="88.64">25 MEDIUM</option>
<option value="103.41">25 HEAVY</option>
<option value="93.54">Regal 19/1.0</option>
<option value="69.8">Regal 19/1.2</option>
<option value="69.8">Regal 19/1.4</option> </datalist>
Use the selectedIndex property of the select field to get the selected <option> element as a node. Then get the innerHTML of that node:
var select = document.querySelector("select");
select.onchange = function() {
var html = this.options[this.selectedIndex].innerHTML
console.log(html);
}
<select>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
In case you are using a <datalist>, you need to manually lookup the option whose value matches the value of your <datalist>:
var input = document.querySelector("input");
var list = document.getElementById("nums");
input.onchange = function() {
var selected = Array.from(list.options).filter(opt => {
return opt.value == input.value;
})[0];
console.log(selected.innerHTML);
}
<input list="nums"/>
<datalist id="nums">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</datalist>

Show second value from select option

I want show in input text from select option where in value have 2 data
function changeHiddenInput (objDropDown)
{
document.getElementById("hiddenInput").value = objDropDown.value;
}
<form name="ff">
<select id="dropdown" name="dropdown" onchange="changeHiddenInput(this)">
<option value="j.hotmail.com|3">Jens</option>
<option value="a.hotmail.com|4">Adam</option>
<option value="d.homtail.com|5">Dan</option>
</select>
<input type="text" type="hidden" name="hiddenInput" id="hiddenInput" />
</form>
I want get value 3,4,5 and show in input text form when each option select.
split your value using " | "
function changeHiddenInput (objDropDown)
{
document.getElementById("hiddenInput").value = objDropDown.value.split('|')[1];
}
<form name="ff">
<select id="dropdown" name="dropdown" onchange="changeHiddenInput(this)">
<option value="j.hotmail.com|3">Jens</option>
<option value="a.hotmail.com|4">Adam</option>
<option value="d.homtail.com|5">Dan</option>
</select>
<input type="text" type="hidden" name="hiddenInput" id="hiddenInput" />
</form>

Select field or Text field

I have a select with many options. If a user doesn't find value in option, He could fill a "other" field :
<select name="value">
<option>Choose...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="text" name="value">
In this case, if I choose a value in the select, it appeared blank beacause of the text fiel.
Thanks
I imagined it, needs to be improved
http://jsfiddle.net/neiesc/jv6qgn03/
<select name="value">
<option>Choose...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br />
<label for="chkOther">
Other
<input id="chkOther" type="checkbox" value="0" />
</label>
<input id="txtOther" type="text" name="value" disabled>
Javascript code:
$( "#chkOther" ).click(function() {
$( "#txtOther" ).prop( "disabled", !$( "#txtOther" ).prop( "disabled"));
});
Use different names for select and text input.
<form method="post">
<select name="select">
<option>Choose...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="text" name="field">
<input type="submit" name="submit">
</form>
And in PHP check what was chosen:
<?php
if (isset($_POST['submit'])) {
// form was sent
if (!empty($_POST['select'])) {
// select or both, use select
} elseif (!empty($_POST['input'])) {
// empty select txt input filled
} else {
// nothing chose in select and text input is blank
}
}
?>

"checked" and "selected" in HTML for input do not work with AngularJS

I am learning AngularJS and user input. In my code, I tried to set default state for drop down menu and radio button with "selected" and "checked".
However, they do not work together with "ng-model" attribute.
Also, for the first radio button (ascending), the empty value attribute seems to hinder with "checked" attribute.
Could anyone explain why this happens and how to bypass this problem?
<div class="search">
<h1>Artist Directory</h1>
<label> Search: </label>
<input ng-model="query" placeholder="Search for artists" autofocus>
<label class="formgroup">by:
<select ng-model="listOrder" name="direction">
<option value ="name" selected="selected"> Name</option>
<option value="reknown"> Reknown</option>
</select>
</label>
<label class="formgroup">
<input ng-model="direction" type="radio" value=" " checked> Ascending
</label>
<label class="formgroup">
<input ng-model="direction" type="radio" value="reverse"> Descending
</label>
</div>
tie your button and select to an ng-model. Default values are the values you put in the model when the scope is first created. Then values are updated when user clicks :
function test($scope) {
$scope.selected = ['1','3'];
$scope.checked = "green";
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
<div ng-app>
<h2>Todo</h2>
<div ng-controller="test">
<select name="" ng-model="selected" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<br><br>
<input type="radio" ng-model="checked" value="red"> Red <br/>
<input type="radio" ng-model="checked" value="green"> Green <br/>
<input type="radio" ng-model="checked" value="blue"> Blue <br/>
<br>
Selected : {{selected}}
<br>
Checked : {{checked}}
</div>
</div>
See it in action : http://jsfiddle.net/913n30zf/
Select in AngularJS looks likes this:
<select ng-model="selectedOpt"
ng-options="opt for opt in listOrder">
</select>
And back in the controller you set
$scope.selectedOpt = $scope.listOrder[0];
which option you want to be select by default.