Sending HTML Calendar Selected Dates as JSON using JavaScript - javascript

I have an HTML page, and I have 2 calenders in it. A "Start Date" and an "End Date"
<label for="startdatepicker" class="col-sm-2 control-label">Start Date</label>
<div class="container">
<div class="row">
<div class='col-sm-6'>
<div class="form-group">
<div class='input-group date' value="2018-01-01" name="StartDate" id='startdatepicker'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calender"></span>
</span>
</div>
</div>
</div>
</div>
</div>
<label for="enddatepicker" class="col-sm-2 control-label">End Date</label>
<div class="container">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<div class="input-group date" id="enddatepicker">
<input type="text" class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calender"></span>
</span>
</div>
</div>
</div>
</div>
</div>
Here is a JavaScript that has converted some other data successfully into JSON. (From a drop down selection list)
What I'm trying to do is on the 'Click' of a button, I am converting the following data into JSON. Everything besides the Calendar select is working.
Instead of outputting the dates, it outputs an empty "".
$("#requestdata").click(function () {
var sourceserver = $("#sourceserver").val();
var orgnumber = $("#orgnumber").val();
var startdate = $("#startdatepicker").val();
var enddate = $("#enddatepicker").val();
var jsonrequest = {
SourceServer: sourceserver,
OrgNumber: orgnumber,
StartDate: startdate,
EndDate: enddate
};
var jsonString = JSON.stringify(jsonrequest);
console.log(jsonString);
});
My current output is:
{"SourceServer":"CHS-HISTORIAN","OrgNumber":"5679 - Stn001ChsWinLagBlwr","StartDate":"","EndDate":""}
The 'button' HTML if it matters
<input type="button" class="btn btn-primary col-sm-offset-2" id="requestdata" value="Request Data" />
Basically I'm having lots of trouble converting the selected Calendar Dates into a JSON.
Thanks!
EDIT 1:
In the JavaScript code, I changed
var startdate = $("#startdatepicker").val();
to
var startdate = $("#startdatepicker").data("DateTimePicker");
And now the console is returning an empty object
{}
instead of just quotations
""
The object is still empty for some reason though..

Related

jQuery only run function for element that gets clicked

I got a small problem that i have no clue how to solve. This HTML/PHP code bellow gets different values from a database and outputs them into the different input fields.
The HTML/PHP bellow is one element, and multiple of them are made with different values from the database. Then i got a small javascript that calulates some different values from the values that are inputted. The problem is that i got lets say 5 elements, and only wants to calculate for one of them, but if i press the "btn-oppdater" button it calculates for all the different elements.
How do i make it only calculate for the element where the button is?
Script
$('.btn-oppdater').click(function(){
$(".kval_spill").each(function(){
var fieldShow = $(this).next('.kval_spill_inner');
var b_value_kval_1 = fieldShow.find('.b_value_kval_1')[0].value;
var b_odds_kval_1 = fieldShow.find('.b_odds_kval_1')[0].value;
var e_odds_kval_1 = fieldShow.find('.e_odds_kval_1')[0].value;
var gebyr_kval = '0.02'
var q_value = ((b_odds_kval_1 / (e_odds_kval_1 - gebyr_kval)) * b_value_kval_1);
var q_tap = (b_odds_kval_1 - 1) * b_value_kval_1 - (e_odds_kval_1 - 1) * q_value;
var q_value_fixed = q_value.toFixed(2);
var q_tap_fixed = q_tap.toFixed(2);
fieldShow.find('.q_value_1')[0].value = q_value_fixed;
fieldShow.find('.q_tap_1')[0].value = q_tap_fixed;
});
});
HTML/PHP
<?php while ($row = mysqli_fetch_assoc($result2)) { echo '
<form style="margin-top: 10px;" action="" method="post" class="">
<input type="hidden" class="kval_spill">
<div class="kval_spill_inner">
<input class="" type="hidden" name="id" value="'.$row['id'].'">
<div class="form-row">
<div class="form-group col-md-4">
<input type="text" class="form-control kval_kamp_1" name="kval_kamp_1" value ="'.$row['kval_kamp_1'].'" placeholder="Kamp">
</div>
<div class="form-group col-md-3">
<div class="input-group">
<input type="text"class="form-control b_value_kval_1" name="b_value_kval_1" value ="'.$row['b_value_kval_1'].'" placeholder="Spill verdi">
<div class="input-group-append">
<span class="input-group-text">Kr</span>
</div>
</div>
</div>
<div class="form-group col-md-2">
<input type="text" class="form-control b_odds_kval_1" name="b_odds_kval_1" value ="'.$row['b_odds_kval_1'].'" placeholder="Odds">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<input type="text" class="form-control kval_marked_1" name="kval_marked_1" value ="'.$row['kval_marked_1'].'" placeholder="Type marked">
</div>
<div class="form-group col-md-3">
<div class="input-group">
<input type="text"class="form-control text-info q_value_1" name="q_value_1" value ="'.$row['q_value_1'].'" placeholder="Lay verdi">
<div class="input-group-append">
<span class="input-group-text">Kr</span>
</div>
</div>
</div>
<div class="form-group col-md-2">
<input type="text" class="form-control e_odds_kval_1" name="e_odds_kval_1" value ="'.$row['e_odds_kval_1'].'" placeholder="Odds">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-2">
<div class="input-group">
<div class="input-group-append">
<span class="input-group-text">Tap</span>
</div>
<input type="text" class="form-control text-danger q_tap_1" name="q_tap_1" value ="'.$row['q_tap_1'].'" placeholder
="0.00" readonly>
<div class="input-group-append">
<span class="input-group-text">Kr</span>
</div>
</div>
</div>
<div class="col-auto">
<button type="button" class="btn btn-outline-secondary btn-oppdater">Regn ut</button>
</div>
</div>
</div>
</form>
<br>
'; }?>
Replace $(".kval_spill") with $(this).closest("form").find(".kval_spill").
But it looks like there's only one kval_spill and kvall_spill_inner in each form, so there's no need to use .each(). You can get rid of the .each() loop and just use:
var fieldShow = $(this).closest("form").find('.kval_spill_inner');
And instead of
fieldShow.find('.q_value_1')[0].value = q_value_fixed;
fieldShow.find('.q_tap_1')[0].value = q_tap_fixed;
you can write:
fieldShow.find('.q_value_1').val(q_value_fixed);
fieldShow.find('.q_tap_1').val(q_tap_fixed);

disable past date after first date is selected datepicker

i'm working to make a simple booking form that redirect to an staah booking engine using GET (they dont have any apis or embedded widget).
the booking engine only accept "dd M yyyy" date format (eg 19 Apr 2018)
i have search SO but dont find any working solution, recently i found working fiddle and i have edit that fiddle for my need (you would know what i want to achieve in the fiddle). you can see it here : https://jsfiddle.net/k5zookLt/1929/
but the problem is they have a different approach on the HTML code and i have tried to modifying the js code for my html but it doesn't work. i dont have any skill in javascript. i dont know why this fiddle doesn't work in my html code
my code
<div class="col-md-2 col-sm-3">
<div class="input-group date" >
<input type="text" class="jaraksmall sm-form-control form-control from " id="from" name="from" required>
</div>
</div>
<div class="col-md-2 col-sm-3">
<div class="input-group date">
<input type="text" class="jaraksmall sm-form-control form-control to" id="to" name="to" required>
</div>
fiddle html code :
<div class="line col-sm-4">
<div class="form-group">
<label>First check in:</label>
<input type="text" class="form-control form-control-1 input-sm"placeholder="CheckIn" >
</div>
<div class="form-group">
<label>First check out:</label>
<input type="text" class="form-control form-control-2 input-sm" placeholder="CheckOut">
</div>
</div><!--line-->
how to make the js code from fiddle working well in my html code?
sorry for my bad english
That's because selector for .each and selector to find input element is not finding the required element. You need to :
1) to remove each statement.
2) modify datepicker input element selectors to use classes that you have added(to and from).
var startDate = new Date();
var fechaFin = new Date();
var FromEndDate = new Date();
var ToEndDate = new Date();
$(".from").datepicker({
autoclose: true,
startDate: "+0d",
format: 'd M yyyy'
}).on('changeDate', function(selected){
startDate = new Date(selected.date.valueOf());
startDate.setDate(startDate.getDate(new Date(selected.date.valueOf()))+1);
$('.to').datepicker('setStartDate', startDate);
});
$('.to').datepicker({
autoclose: true,
format: 'd M yyyy'
}).on('changeDate', function(selected){
FromEndDate = new Date(selected.date.valueOf());
FromEndDate.setDate(FromEndDate.getDate(new Date(selected.date.valueOf())));
$('.from').datepicker('setEndDate', FromEndDate);
});
Working Demo
You can also replace the html code as below
<div class="line" style="width:100%">
<div class="col-md-2 col-sm-3" style="display: inline-block">
<div class="input-group date" >
<input type="text" class="jaraksmall sm-form-control form-control-1 form-control from " id="from" name="from" required>
</div>
</div>
<div class="col-md-2 col-sm-3" style="display: inline-block">
<div class="input-group date">
<input type="text" class="jaraksmall sm-form-control form-control-2 form-control to" id="to" name="to" required>
</div>
</div>
</div>
jsfiddledemo

Why date is not displaying for jquery date picker?

Iam using date picker jquery and it is not displaying inside input field. My link is https://jsfiddle.net/sbk2hd4c/10/ .
My code:
<div class="container">
<div class="row">
<div class='col-sm-6'>
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>
My link is: https://jsfiddle.net/sbk2hd4c/10/
Thankyou in advance!!!
Check this
https://jsfiddle.net/sbk2hd4c/11/
You used your Id in div which should be in your input tag
<div class="container">
<div class="row">
<div class='col-sm-6'>
<div class="form-group">
<div class='input-group date' >
<input type='text' class="form-control" id='datetimepicker1' />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>
<input type='text' class="form-control" id='datetimepicker1' />
Change the id for input as above. Div should have different id.
You can bind the changeDate to the datepicker and can set the date in textBox
$('#datetimepicker1').datetimepicker().on('changeDate', dateChangeListener);
function dateChangeListener(event) {
var formattedDate = event.format('dd.mm.yy');
$('text_box_id').val(formattedDate);
}

How to display the start date one day ahead with bootstrap-datepicker

The select date seem to be empty when I added startDate: '+1d' into the javascript. Otherwise, it is just displaying today's date. I need it to display one day ahead and in some situations, two days ahead as well.
Javascript below:
$("#datepicker").datepicker({
autoclose: true,
todayHighlight: false,
startDate:'+1d',
dateFormat: 'dd-mm-yy'
}).datepicker('update', new Date());
HTML Code below:
<div class="form-group">
<div class="col-sm-2"></div>
<label for="inputName" class="col-sm-3 control-label">Select Date Range</label>
<div class="col-sm-4">
<div class='col-sm-6'>
<div class="form-group">
<div id="datepicker" class="input-group date" data-date-format="dd-mm-yyyy">
<input class="form-control" type="text" readonly id="start"/>
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
</div>
</div>
</div>
<div class='col-sm-6'>
<div class="form-group">
<div id="datepicker2" class="input-group date" data-date-format="dd-mm-yyyy">
<input class="form-control" type="text" readonly id="end"/>
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
</div>
</div>
</div>
</div>
</div>
you can use moment.js for this type of date/time manipulation
var tomorrow = moment().add(1, 'day').format('DD/M/YYYY');
outputs
03/11/2016
see jsbin:
here is the full working jsfiddle: https://jsbin.com/haxamaxaqe/edit?html,output
$('#txtDate').datepicker('setDate', "+1d");

Fetch event details (title, start and end) from database for fullcalendar

I have really searched a lot about this, but I still haven't gotten through with my problem. I am creating a calendar (using the plugin fullcalendar and using codeigniter as the backend) that would display a tour and it's specific time span. So, in this case the title of the event would be the tour name, and for the duration of the tour, the start and end dates is to be fetched.
I have already succeeded with the insertion of the data to the database, it's just that the events are not appearing on their respective dates.
These are what I have coded so far:
Controller: Calendar.php
function view_tours(){
$this->load->model('Calendar_model');
$tours = $this->Calendar_model->getTours();
echo json_encode($tours);
}
function add_tour(){
$sdate = $this->input->post('start_date'); //start date
$edate = $this->input->post('end_date'); //end date
if($this->form_validation->run() == TRUE){ //column name in db=>name in form
$tour = array('tour_name' => $this->input->post('tour_name'),
'start_date' => date('Y-m-d', strtotime($sdate)),
'end_date' => date('Y-m-d', strtotime($edate)),
'slots' => $this->input->post('slots'),
'rate' => $this->input->post('rate'),);
$this->Calendar_model->insert_tour($tour);
echo 'success';
}
else {
redirect(base_url().'Calendar');
echo "error";
}
}
Model: Calendar_model.php
public function getTours(){
$query = $this->db->get('tours');
return $query -> result_array();
}
function insert_tour($tour){
$this->db->insert('tours', $tour);
return $this->db->insert_id();
}
View: home.php (This is the html file)
<div id="AddModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span> <span class="sr-only">close</span></button>
<h4 id="modalTitle" class="modal-title"> Add a Tour </h4>
</div>
<div id="modalBody" class="modal-body">
<?php
echo validation_errors();
?>
<form class="form-horizontal" id="crud-form" method="POST" action="<?php echo base_url();?>Calendar/add_tour">
<div class="form-group">
<label class="col-md-4 control-label" for="tour_name">Tour Name</label>
<div class="col-md-4">
<input type="text" class="form-control" id="tour_name" name="tour_name"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="start_date">Start Date</label>
<div class="col-md-4">
<input type="date" class="form-control" id="start_date" name="start_date"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="end_date">End Date</label>
<div class="col-md-4">
<input type="date" class="form-control" id="end_date" name="end_date"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="slots">Slots</label>
<div class="col-md-4">
<input type="text" class="form-control" id="slots" name="slots"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="rate">Rate</label>
<div class="col-md-4">
<input type="text" class="form-control" id="rate" name="rate"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="color">Color</label>
<div class="col-md-4">
<input id="color" name="color" type="text" class="form-control input-md" readonly="readonly" />
<span class="help-block">Click to pick a color</span>
</div>
</div>
<button type="submit" class="btn btn-primary"> Add Tour</button>
</form>
</div> <!--end of modal body-->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
I have recently edited this but this does not work.
JS File: main.js I have tried fetching data using the $.ajax options but when i do this, the calendar itself does not appear. What is wrong with the code? I am just a beginner with ajax.
events: {
url: base_url+'Calendar/view_tours',
type: 'POST',
data{
title: tour_name,
start: start_date,
end: end_date
},error: function(){
alert('error in fetching from database!');
}
}, //end of events
I just pasted a snippet of the js. I know that the calendar is "somehow" retrieving data from the database because i can see the events, BUT, it only appears on the current date in real time and shows the current time, for all events added. And i have added a tool tip to see if the tour name (title) is being read, and it is.
Here's what the actual results are: click to see image
Summary of problem: Events are not in their respective dates and the time added is on realtime.
So it seems like your problem is that the JSON object returned by your URL has unrecognizable data for fullCalendar. So you'll have to fetch events like this:
events: base_url+'Calendar/view_tours',
And change the columns of your data base from tour_id to id, tour_name to title, start_date to start and end_date to end. This will create the correct JSON object.

Categories