Is there a way to make a second Input field? - php

Is there a way to make a second Input field so that the 1st time value is in one input field and the second is in the other? I am trying to post the times to a database so I need then in separate fields.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" />
<div class='amount-time-sch'>
<label for="amount-time">Time</label>
<input type="text" name="amount-time" id="amount-time" style="border: 0; color: #666666; font-weight: bold;" value="10:00 - 20:00"/>
<div id="slider-time"></div><br>
</div>
<script>jQuery(function() {
jQuery('#slider-time').slider({
range: true,
min: 0,
max: 1000,
step: 15,
values: [ 600, 1200 ],
slide: function( event, ui ) {
var hours1 = Math.floor(ui.values[0] / 60);
var minutes1 = ui.values[0] - (hours1 * 60);
if(hours1.length < 10) hours1= '0' + hours;
if(minutes1.length < 10) minutes1 = '0' + minutes;
if(minutes1 == 0) minutes1 = '00';
var hours2 = Math.floor(ui.values[1] / 60);
var minutes2 = ui.values[1] - (hours2 * 60);
if(hours2.length < 10) hours2= '0' + hours;
if(minutes2.length < 10) minutes2 = '0' + minutes;
if(minutes2 == 0) minutes2 = '00';
jQuery('#amount-time').val(hours1+':'+minutes1+''+hours2+':'+minutes2 );
}
});
});
</script>

Yes, you can have your jQuery write the value to two different inputs with unique ids.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" />
<div class='amount-time-sch'>
<label for="amount-time">Time</label>
<!-- <input type="text" name="amount-time" id="amount-time" style="border: 0; color: #666666; font-weight: bold;" value="10:00 - 20:00"/> -->
<input type="text" name="start-time" id="start-time" value="10:00">
<input type="text" name="end-time" id="end-time" value="20:00">
<div id="slider-time"></div><br>
</div>
<script>jQuery(function() {
jQuery('#slider-time').slider({
range: true,
min: 0,
max: 1000,
step: 15,
values: [ 600, 1200 ],
slide: function( event, ui ) {
var hours1 = Math.floor(ui.values[0] / 60);
var minutes1 = ui.values[0] - (hours1 * 60);
if(hours1.length < 10) hours1= '0' + hours;
if(minutes1.length < 10) minutes1 = '0' + minutes;
if(minutes1 == 0) minutes1 = '00';
var hours2 = Math.floor(ui.values[1] / 60);
var minutes2 = ui.values[1] - (hours2 * 60);
if(hours2.length < 10) hours2= '0' + hours;
if(minutes2.length < 10) minutes2 = '0' + minutes;
if(minutes2 == 0) minutes2 = '00';
jQuery('#start-time').val(hours1+':'+minutes1);
jQuery('#end-time').val(hours2+':'+minutes2);
}
});
});
</script>

I would advise using an html time input for each time value. Here is a hypothetical example:
<form action="#" onsubmit="sendTimes(event)">
<label for="first-time">first time</label>
<input type="time" name="first-time" id="first-time"><br>
<label for="second-time">second time</label>
<input type="time" name="second-time" id="second-time"><br>
<input type="submit" value="submit">
</form>
<script type="text/javascript">
function sendTimes(event) {
// keep browser from refreshing
event.preventDefault();
const formData = new FormData();
// get the values of the times
const firstTime = $("#first-time").val();
const secondTime = $("#second-time").val();
// add the times to the form data to POST
formData.append("first-time", firstTime);
formData.append("second-time", secondTime);
// insert logic here for sending the form data to PHP
}
</script>
The time values will be empty strings if the user leaves them empty. Otherwise, they will be 24-hour times in the form "hh:mm". On the server-side, you can validate each time value in PHP before saving them to the database.

Related

Disabled a button based on credit date expiry date validation in jquery

I have fields of credit card expiry date.Below code user enter a month and year
<input class="expiry-month exp" name="card_exp_month" id="card_exp_month" required>
<input class="expiry-year exp" name="card_exp_year" id="card_exp_year" required>
I am getting current month and year from php date() function.
<?php
$month= date('n');
$year= date('y');
?>
<input type="text" name="curr_month" id="curr_month" value="<?php echo $month;?>"/>
<input type="text" name="curr_year" id="curr_year" value="<?php echo $year;?>"/>
This button will be disabled when user enter a expiry month or year less to current month or year
<input type='button' class='btn btn-next btn-fill btn-rose btn-wd' name='next' value='Next' id="billing_nxtBtn" />
and jquery code
$( ".exp" ).keyup(function() {
var exp_month = $("#card_exp_month").val();
var exp_year = $("#card_exp_year").val();
var curr_month = $("#curr_month").val();
var curr_year = $("#curr_year").val();
if(exp_month < curr_month || exp_year < curr_year){
$("#billing_nxtBtn").attr("disabled",true);
}else if(exp_month > curr_month || exp_year > curr_year) {
$("#billing_nxtBtn").attr("disabled",false);
}
});
I want that when user enter a expire month or year less then to current month or year then button will be disabled and when user enter a expire month or year greater then to current month or year then button will be enabled.
Issue is that button is not disbaled when user enter a expire month or year less then to current month or year
There's a couple of issues. One is because you're not removing the disabled attribute; you need to use removeAttr(). Your logic is also flawed, as if I selected 01/2019 then the button would be disabled as the month is less than the current month - you're not taking in to account years.
There is also a couple of logical improvements you can make, such as using input instead of keyup (in case people copy+paste values, or use other entry methods for accessibility) and use prop() instead of attr(). That way you can simply provide the boolean condition as the argument.
Also just FYI, this is not secure in the slightest; it's ridiculously easy to break. You should provide this client side validation as a courtesy to users only and do the business critical date check on the server side.
With all that said, try this:
$(".exp").on('input', function() {
var exp_month = $("#card_exp_month").val();
var exp_year = $("#card_exp_year").val();
var curr_month = $("#curr_month").val() || 0;
var curr_year = $("#curr_year").val() || 0;
$("#billing_nxtBtn").prop('disabled', exp_year < curr_year || (exp_year == curr_year && exp_month < curr_month));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="expiry-month exp" name="card_exp_month" id="card_exp_month" required>
<input class="expiry-year exp" name="card_exp_year" id="card_exp_year" required><br />
<input type="text" name="curr_month" id="curr_month" value="6" />
<input type="text" name="curr_year" id="curr_year" value="18" />
<input type='button' class='btn btn-next btn-fill btn-rose btn-wd' name='next' value='Next' id="billing_nxtBtn" disabled/>
You should place your jquery constant value with PHP constant date, not get it from input field (which is dynamic value by user input), example code as below:
$( ".exp" ).keyup(function() {
var exp_month = $("#card_exp_month").val();
var exp_year = $("#card_exp_year").val();
var curr_month = parseInt('<?= date("n");?>');
var curr_year = parseInt('<?= date("Y");?>');
if (exp_year < curr_year) {
$("#billing_nxtBtn").attr("disabled", true);
} else if (exp_year > curr_year) {
$("#billing_nxtBtn").removeAttr("disabled");
} else {
if (exp_month >= curr_month) {
$("#billing_nxtBtn").removeAttr("disabled");
} else {
$("#billing_nxtBtn").attr("disabled", true);
}
}
});
Have u try to use Jquery.change Method
$("#card_exp_month,#card_exp_year").change(function(){
var exp_month = $("#card_exp_month").val();
var exp_year = $("#card_exp_year").val();
var curr_month = $("#curr_month").val();
var curr_year = $("#curr_year").val();
if(exp_month < curr_month || exp_year < curr_year){
$("#billing_nxtBtn").attr("disabled",true);
}else if(exp_month > curr_month || exp_year > curr_year) {
$("#billing_nxtBtn").attr("disabled",false);
}
});

Change input number by step on button click

I have the following code in my functions.php file, however the script does not seem to be doing what it is intended to do.
My plus and minus buttons have classes .plus and my .minus respectively.
PHP / JQuery :
/*
========================================
Change qty by step on button click
========================================
*/
function kia_add_script_to_footer(){ ?>
<script>
jQuery(document).ready(function(){
jQuery(document).on('click', '.plus', function(e) { // replace '.quantity' with document (without single quote)
$input = jQuery(this).siblings('.quantity .qty');
var val = parseInt($input.val());
var step = $input.attr('step');
step = 'undefined' !== typeof(step) ? parseInt(step) : 0.5;
$input.val( val + step ).change();
});
jQuery(document).on('click', '.minus', // replace '.quantity' with document (without single quote)
function(e) {
$input = jQuery(this).siblings('.quantity .qty');
var val = parseInt($input.val());
var step = $input.attr('step');
step = 'undefined' !== typeof(step) ? parseInt(step) : 0.5;
if (val > 0) {
$input.val( val - step ).change();
};
});
});
</script>
<?php };
add_action( 'wp_footer', 'kia_add_script_to_footer' );
Any help would be greatly appreciated!
EDIT: Apologies for the bland question. I have erased the generic input arrows for input.qty and added two buttons (.minus and .plus). What is meant to happen, is that on click of either .minus or .plus, the input.qty number should decrease or increase by var step amount.
As for the question of whether .qty is nested inside of .quantity, that would be correct. I was not sure if maybe .qty was being used elsewhere, so to confirm my coding was correct, I added the parent element's class.
HTML :
<div class="quantity">
<input class="minus" value="-" type="button">
<input class="input-text qty text" step="0.5" min="0.5" max="10" name="quantity" value="0" title="Qty" size="4" pattern="[0-9]*" inputmode="numeric" type="number">
<input class="plus" value="+" type="button">
<span>press - or + to add/remove meterage per half meter</span>
</div>
You need to use parseFloat() instead of parseInt()
Note: I've changed
step = 'undefined' !== typeof(step) ? parseInt(step) : 0.5;
to
step = step != null ? parseFloat(step) : 0.5;
only in the "plus function" to show another way how you could check the value.
jQuery(document).ready(function(){
jQuery(document).on('click', '.plus', function(e) {
$input = jQuery(this).siblings('.quantity .qty');
var val = parseFloat($input.val());
var step = $input.attr('step');
step = step != null ? parseFloat(step) : 0.5;
$input.val( val + step );
});
jQuery(document).on('click', '.minus', // replace '.quantity' with document (without single quote)
function(e) {
$input = jQuery(this).siblings('.quantity .qty');
var val = parseFloat($input.val());
var step = $input.attr('step');
step = 'undefined' !== typeof(step) ? parseFloat(step) : 0.5;
if (val > 0) {
$input.val( val - step ).change();
};
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="quantity">
<input class="minus" value="-" type="button">
<input class="input-text qty text" step="0.5" min="0.5" max="10" name="quantity" value="0" title="Qty" size="4" pattern="[0-9]*" inputmode="numeric" type="number">
<input class="plus" value="+" type="button">
<span>press - or + to add/remove meterage per half meter</span>
</div>
And here the same with less code and with always one decimal place see comments.
var round = function (value, precision) {
var multiplier = Math.pow(10, precision || 0);
return Math.round(value * multiplier) / multiplier;
}
jQuery(document).on('click', '.plus, .minus', function(e) {
var $input = jQuery(this).siblings('.quantity .qty');
var val = parseFloat($input.val());
var step = parseFloat($input.attr('step'));
step = step == null ? 0.5 : step;
var sum = val + step;
if($(this).hasClass('minus')) {
sum = val - step;
}
$input.val(sum.toFixed(1));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="quantity">
<input class="minus" value="-" type="button">
<input class="input-text qty text" step="0.5" min="0.5" max="10" name="quantity" value="0" title="Qty" size="4" pattern="[0-9]*" inputmode="numeric" type="number">
<input class="plus" value="+" type="button">
<span>press - or + to add/remove meterage per half meter</span>
</div>

auto-fill age field when date is selected from datepicker

I want the age field to be automatically filled as soon as you select the date from the datepicker.Right now the datepicker is not working in the snippet dont know why.I do also have the working age calculation code in the php section.Only need help in filling the age field as soon as you select the date.I read that you need ajax for that but I couldnt really work that out. Please do help as I am a beginner working on this part.
< script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js" > < /script>
<script src="https:/ / maxcdn.bootstrapcdn.com / bootstrap / 4.0.0 - alpha.2 / js / bootstrap.min.js " integrity="
sha384 - vZ2WRJMwsjRMW / 8U7i6PWi6AlO1L79snBrmgiDpgIWJ82z8eA5lenwvxbMV1PAh7 " crossorigin="
anonymous "></script>
<script src="
http: //cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>
< script type = "text/javascript" >
$(document).ready(function() {
$('#datetimepicker8').datepicker({
icons: {
time: "fa fa-clock-o",
date: "fa fa-calendar",
up: "fa fa-arrow-up",
down: "fa fa-arrow-down"
}
});
});
< /script>
<?php
$dob=$_POST['birthdate'];
$dob=explode("/",
$dob);
$age=(date("md",
date("U",
mktime(0,
0,
0,
$dob[0],
$dob[1],
$dob[2]))) > date("md") ? ((date("Y") - $dob[2]) - 1):(date("Y") - $dob[2]));
echo"age is" $age;
?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" integrity="sha384-y3tfxAZXuh4HwSYylfB+J125MxIs6mR5FOHamPBG064zB+AFeWH94NdvaCBm8qnd" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" />
<link href="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker.css" rel="stylesheet" type="text/css" />
<form method="post" id="signUpForm">
<div class="container" id="signupContainer">
<div class="form-group row">
<label class="col-sm-2 form-control-label">Birthdate</label>
<div class="col-sm-5">
<div class='input-group date' id='datetimepicker8'>
<input type='text' id="birthdate" class="form-control" placeholder="D.O.B" name="birthdate" />
<span class="input-group-addon">
<i class="fa fa-calendar" aria-hidden="true"></i>
</span>
</div>
</div>
<label class="col-sm-1 form-control-label">Age:</label>
<div class="col-sm-4">
<input type="text" id="age" class="form-control" name="age" placeholder="Age" />
</div>
</div>
</div>
</form>
Try this code, php code is not required. Date of birth is calculated in datepicker onchange itself.
$('#datetimepicker8').datepicker({
icons: {
time: "fa fa-clock-o",
date: "fa fa-calendar",
up: "fa fa-arrow-up",
down: "fa fa-arrow-down"
},
dateFormat: "dd/mm/yy"
}).on('change', function (ev) {
var selectDate = $('#datetimepicker8').val().split("/");
var todayDate = new Date();
var selectedDate = new Date(selectDate[2], selectDate[1], selectDate[0]);
var firstDate = new Date(todayDate.getFullYear(), todayDate.getMonth() + 1, todayDate.getDate());
var diff = Math.floor(firstDate.getTime() - selectedDate.getTime());
var day = 1000 * 60 * 60 * 24;
var days = Math.floor(diff / day);
var months = Math.floor(days / 31);
var years = Math.floor(months / 12);
if (years > 0) {
$('#age').val(years);
} else {
$('#age').val('');
}
if (days <= -1) {
alert("Please select valid date of birth.")
$('#datetimepicker8').val("");
}
});

jQuery live calc multirow input

This is my code:
<?php
for($i=1;$i<10;$i++){
echo '<input type="text" class="count value'. $i .'">';
echo '<input type="text" class="count '. $i .'value">';
echo '<input type="text" disabled="disabled" id="result'. $i .'"><p>';
}
echo '<input type="text" disabled="disabled" id="total"><p>';
?>
and jQuery:
$(document).ready(function(){
$(".count").keyup(function(){
for (var i = 0; i < 10; i++) {
var val1 = +$(".value"+ i).val();
var val2 = +$("."+ i +"value").val();
$("#result" + i).val(val1*val2);
}
});
});
$(document).ready(function(){
$(".count").keyup(function(){
for (var i = 0; i < 10; i++) {
var vala = 0;
vala += +$("#result"+ i).val();
}
$("#total").val(vala);
});
});
First part of code works great.
Return multiplication two inputs to id=result$i.
I have a problem with last one id=total.
It should return sum of all result X inputs
but now only return the last multiplication.
Do You have any idea what's wrong?
You can simplify your code by grouping the related input elements together in a containing div, using DOM traversal to retrieve the needed values, and joining the two for loops together. Try this:
<div class="group">
<input type="text" class="count valueA" />
<input type="text" class="count valueB" />
<input type="text" class="result" disabled="disabled" />
</div>
<!-- repeat the above as needed. Note that the incremental id is no longer needed -->
<p>
<input type="text" disabled="disabled" id="total" />
</p>
$(document).ready(function(){
$(".count").keyup(function() {
var total = 0;
$('.group').each(function() {
var $group = $(this);
var valA = +$group.find('.valueA').val() || 0;
var valB = +$group.find('.valueB').val() || 0;
var result = valA + valB;
total += result;
$group.find('.result').val(result);
});
$("#total").val(total);
});
});
Example fiddle
That is because you have defined variable vala to 0 in for loop. which should be outside for loop:
$(".count").keyup(function(){
var vala= 0;
for (var i = 0; i < 10; i++) {
vala += $("#result"+ i).val();
}
$("#total").val(vala);
});

Setting value with Javascript

I have this code:
<html>
<head>
<script type="text/javascript">
/***********************************************
* Drop Down Date select script- by JavaScriptKit.com
* This notice MUST stay intact for use
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and more
***********************************************/
var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
function populatedropdown(dayfield, monthfield, yearfield){
var today=new Date()
var dayfield=document.getElementById(dayfield)
var monthfield=document.getElementById(monthfield)
var yearfield=document.getElementById(yearfield)
for (var i=0; i<31; i++)
dayfield.options[i]=new Option(i, i+1)
dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true) //select today's day
for (var m=0; m<12; m++)
monthfield.options[m]=new Option(monthtext[m], monthtext[m])
monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true) //select today's month
var thisyear=1920; // fixed start year of 1900
var nowyear = 1994;
var diff = nowyear - thisyear +1; // number of years from 1900
for (var y=0; y<diff; y++){
yearfield.options[y]=new Option(thisyear, thisyear)
thisyear+=1
}
}
</script>
<script>
function getcombined(){
var year = document.getElementbyId("yeardropdown").value;
var month = document.getElementById("monthdropdown").value;
var day = document.getElementById("daydropdown").value;
var combineddob = year + "-" + "month" + "-" + day;
document.getElementById("hidden1").value=combineddob
}
</script>
</head>
<body>
<form action="" name="someform">
<select id="daydropdown" name='day' value="daydropdown">
</select>
<select id="monthdropdown" name='month' value="monthdropdown">
</select>
<select id="yeardropdown" name='year' value="yeardropdown">
</select>
<input type='hidden' id='hidden1' name='dob' value="" />
<input type='submit' />
</form>
<script type="text/javascript">
window.onload=function(){
populatedropdown("daydropdown", "monthdropdown", "yeardropdown")
}
</script>
</body>
</html>
I need the <input type='hidden' id='hidden1' name='dob' value='' /> to update to var combineddob when the form is submitted.
I have tried several methods, but I do not know much about javascript so I am not good with these kinds of fixes.
I may be over looking something, but I have not yet figured out what is wrong here.
If you want something to happen before a user submits the form, you should set the onsubmit property of the form. In this case:
<script>
<!--
function getcombined(){
var year = document.getElementById("yeardropdown").value;
var month = document.getElementById("monthdropdown").value;
var day = document.getElementById("daydropdown").value;
var combineddob = year + "-" + month + "-" + day;
document.getElementById("hidden1").value=combineddob;
return true;
}
-->
</script>
</head>
<body>
<form action="" name="someform" onsubmit='return getcombined();'>
I've added "return true" to the function to indicate that the form should actually submit afterwards. Also I fixed two typos, namely a lower case b in getElementbyId and a missing semicolon.
change your html
<input type='submit' />
to this
<input type='submit' id="submitButton" />
and in your script
window.onload = function(){
populatedropdown("daydropdown", "monthdropdown", "yeardropdown");
var submitBtn = document.getElementById("submitButton");
submitBtn.onsubmit = function(){getcombined();};
};
This will allow you to run the function in onsubmit when the submit button is clicked. It will in turn call the function getcombined(); and then submit the form.
EDIT
Perhaps you should change these two lines:
var combineddob = year + "-" + "month" + "-" + day;
document.getElementById("hidden1").value=combineddob
to
var combineddob = year + "-" + month + "-" + day;
document.getElementById("hidden1").value=combineddob;
and
var year = document.getElementbyId("yeardropdown").value;
to (note that getElementbyId is not a function)
var year = document.getElementById("yeardropdown").value;
First of all:
var year = document.getElementbyId("yeardropdown").value;
to:
var year = document.getElementById("yeardropdown").value;
Add onclick in submit button:
<input type='submit' onclick="getcombined();alert(hidden1.value);" />

Categories