Html button and values - html

I am a backend developer so I need help with this front end part.
<input id="entered_value" type="number" name="entered_value">
<input id="entered_valuetwo" type="text" name="entered_valuetwo">
<select id="selected_value" name="selected_value"> <option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button type="button" id="send-data">Click me</button>
when the button is clicked its already triggering something I have in JS. But what is the best way to pass "entered_value" & "entered_valuetwo" & "selected_value" all 3 at the same time to the button and they get passed to the js function thereafter.

just add this to your js
document.getElementById('send-data').addEventListener('click',buttonFunction)
function buttonFunction(){
let enteredValue = document.getElementById('entered_value').value
let enteredValue2 = document.getElementById('entered_valuetwo').value
let selectValue = document.getElementById('selected_value').value
console.log(enteredValue,enteredValue2,selectValue)
}
<input id="entered_value" type="number" name="entered_value">
<input id="entered_valuetwo" type="text" name="entered_valuetwo">
<select id="selected_value" name="selected_value"> <option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button type="button" id="send-data">Click me</button>

If you want to put it into a databas i would use post request like this:
<form action="your url" method="Post">
<input id="entered_value" type="number" name="entered_value">
<input id="entered_valuetwo" type="text" name="entered_valuetwo">
<select id="selected_value" name="selected_value">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button type="button" id="send-data">Click me</button>
</form>
But if you want to use it temporarylie use document.getElementby(𝗜𝗗) or better document.querySelector(#𝗜𝗗)

Related

Collecting select values into an array (jQuery)

I've been attempting to collect the selected data value of each of the dropdowns in a form and pass them into an array so that I can eventually add them together. However with my current code, the array only receives the value of the first dropdown, and not any of the others. I haven't got to the addition part yet, so I'm just joining the array with a comma, but here's my code so far:
HTML
<form id="questions">
<select name="q1" id="q1" tabindex="1">
<option data-calculate="0" value="No">No</option>
<option data-calculate="0.5" value="Partially">Partially</option>
<option data-calculate="1" value="Yes">Yes</option>
</select>
<select name="q2" id="q2" tabindex="2">
<option data-calculate="0" value="No">No</option>
<option data-calculate="0.5" value="Partially">Partially</option>
<option data-calculate="1" value="Yes">Yes</option>
</select>
<select name="q3" id="q3" tabindex="3">
<option data-calculate="0" value="No">No</option>
<option data-calculate="0.5" value="Partially">Partially</option>
<option data-calculate="1" value="Yes">Yes</option>
</select>
<input type="hidden" id="calculator" value=""/>
</form>
JQUERY
var calculation = [];
jQuery("#questions").change(function() {
calculation.push(jQuery(this).find(":selected").data('calculate'));
jQuery("#calculator").val(calculation.join(', '));
});
Thanks in advance for the help!
Consider the following.
$(function() {
$("#questions > select").change(function(event) {
var calc = [];
$("#questions > select").each(function(i, el) {
calc.push($(el).val());
});
$("#calculator").val(calc.join(", "));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="questions">
<select name="q1" id="q1" tabindex="1">
<option data-calculate="0" value="No">No</option>
<option data-calculate="0.5" value="Partially">Partially</option>
<option data-calculate="1" value="Yes">Yes</option>
</select>
<select name="q2" id="q2" tabindex="2">
<option data-calculate="0" value="No">No</option>
<option data-calculate="0.5" value="Partially">Partially</option>
<option data-calculate="1" value="Yes">Yes</option>
</select>
<select name="q3" id="q3" tabindex="3">
<option data-calculate="0" value="No">No</option>
<option data-calculate="0.5" value="Partially">Partially</option>
<option data-calculate="1" value="Yes">Yes</option>
</select>
<input type="hidden" id="calculator" value="" />
</form>
Each change to a select element triggers the callback. Making a more global variable will cause it to get updated over and over again if the User makes many changes or changes their selection. This will capture all 3 values and join them. If the user changes a selection, the hidden field is updated with just the values currently selected.

HTML submit form from select but comma separate values in GET

I have a simple HTML form that has a dropdown.
<form method="get">
<select name="category[]" multiple class="form-control">
<option value="1">First Value</option>
<option value="2">Second Value</option>
<option value="2">Third Value</option>
</select>
<input type="submit" value="Submit">
</form>
Upon submission of this form, it redirects to https://WEBSITE/?category[]=1&category[]=2. How do I have it redirect to https://WEBSITE/?category=1,2
I was looking for a simple solution instead of using jQuery to intercept the form submission, load in every submitted value, comma separate it, and use window.location.href to redirect.
<select multiple class="form-control" onclick="document.getElementById('cat').value=Object.values(this.options).filter(option=>option.selected).map(option=>option.value).join(',')">
<option value="1">First Value</option>
<option value="2">Second Value</option>
<option value="3">Third Value</option>
</select>
<form method="get">
<input id="cat" type="hidden" name="category" value="">
<input type="submit" value="Submit">
</form>

Bootstrap 4 form not sending contents of select box

I've written a simple bootstrap 4 form with a GET method, and it only sends the values in the Submit button, not in the tag above it.
This is eventually to be handled in a POST request, but I've changed it to GET to more easily see what data the form is sending.
Here is the form code:
<form action="/picknumbers" method="GET">
<div class="form-group">
<label for="numberselect">Select one or more numbers</label>
<select multiple class="form-control mb-3" id="numberselect" size="6">
<option>1</option>
<option selected>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select>
</div>
<button name="submission" value="numberspicked" type="submit" class="btn btn-primary btn-block">Choose numbers</button>
</form>
The output in the browser address bar is simply: /picknumbers?submission=numberspicked, without including the numbers picked by the user. How do I get the submitted form to include them?
You need to give a value on the options also you need to give a name to the select. To clarify, see the code below:
<select multiple class="form-control mb-3" name="select-name" id="numberselect" size="6">
<option value="1">1</option>
<option value="2" selected>2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
Your select options appear to be missing value attribute tags. It should look like the below:
<div class="form-group">
<label for="numberselect">Select one or more numbers</label>
<select multiple class="form-control mb-3" id="numberselect" size="6">
<option value="1">1</option>
<option value="2" selected>2</option>
...
</select>
</div>
<button name="submission" value="numberspicked" type="submit" class="btn btn-primary btn-block">Choose numbers</button>
</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
}
}
?>

Booking mask - Changing the type="date" value to type="text" when submit

I'm working on a hotel website and received a code from TravelClick for the booking mask. My client didn't like the "pop up calendar" so I tried implementing a "type="date" in the code instead. But when I click submit, it doesn't recognized the date anymore. I think the problem is that before the type value was "text" and now it's "date", but how can I change it back to "text" without losing the calendar?
<form action="https://booking.ihotelier.com/istay/istay.jsp" method="get" name="resform" id=resform"">
<input type="hidden" name="HotelID" value="12401" />
<input type="hidden" name="LanguageID" value="3" />
<input type="hidden" name="Rooms" value="1" />
Check-In Date:
<input type="date" name="DateIn" />
Nights:
<select name="Length">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="10">11</option>
<option value="10">12</option>
<option value="10">13</option>
<option value="10">14</option>
</select>
Adults:
<select name="Adults">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
Children:
<select name="Children">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input type="submit" value="Submit" />
</form>
<!-- This javascript code populates the Check-In Date field with today's date. It must be located after the form close tag. -->
<script type="text/javascript" language="javascript">
function getDateStr() {
var today = new Date();
var todayStr = (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear();;
document.resform.DateIn.value = todayStr;
}
getDateStr();
</script>