I am developing a page that sum a summary of report. By default it only shows today. I am using this:
include $koneksi
$date ='current_date';
$query1 = mysqli_query($koneksi,"SELECT * from daftar where tanggal=subdate($date, 1)");
$result=mysqli_num_rows($query1);
And here is my HTML
<div class="count blue"><?php echo $result." people(s)"?></div>
And it works, the result of query is what I expected.
Now I need to change the value of $date variable with datepicker.
<div class='input-group date' id='myDatepicker'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
I want it so that every time I click on datepicker the variable $date will change and the page will reload (same page but different query result ) by itself.
If you want the data to displayed in the same page, better you can use Angular. Here, just to show the flow I have used angularJs but the support for angularJs has been stopped so use Angular or reactJs as per your need (search more about it)
Use html to construct a form and using angularJs http.post(), post your form data to php
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>
<script>
$(document).ready(function(){
var date_input=$('input[name="inpDate"]'); //our date input has the name "date"
var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
var options={
format: 'yyyy-mm-dd',
container: container,
todayHighlight: true,
autoclose: true,
};
date_input.datepicker(options);
});
</script>
<form ng-submit="submit()">
<label>Date</label>
<input class="form-control" ng-model="inpDate" id="date" name="inpDate" placeholder="YYYY-MM-DD" type="text" required="required"/>
</form>
And write a separate angularJs file as below. On submit this will send date to the php file and get the response.
$scope.submit = function(){
var date1=$scope.inpDate;
$http.get('http://localhost/report/urfile.php?inpdate='+date1).success(function(shiftdata) {
$scope.dbValue = shiftdata;
console.log(JSON.stringify(shiftdata) );
});
}
after this modify your php will to receive the data and send a response accordingly.
Now the data is URL you can get it in ph like below.
urfile.php
$date = $_GET['inpdate'];
Related
I am using a Codeigniter script purchased in Codecanyon. I need add the input datepicker but in Controller cannot get from UI picked date.
UI:
<input type="date" class="form-control datetimepicker" value="<?php echo date('Y-m-d'); ?>" name="deadline" id="deadline">
Controller :
$deadline = date("Y-m-d", strtotime($this->input->post('deadline')));
I have test to show before update database but it's display current time:
var_dump($deadline);
return;
The modal you're dealing with is outside the form. I made it that - using jquery - when the date field is changed, it updates the value of its hidden field which is in the form. So when the modal triggers form submission, the value of its date field is also submitted to the controller.
HTML:
<input type="hidden" name="deadline" id="deadlinehidden" value="" />
JS:
$("#deadline").on("change paste keyup", function() {
$("#pos-sale-form input[name='deadline']").val($(this).val());
});
you can use this:
<input name="deadline" type="text" value="2017-07-08" class="form-control datepicker" id="datepicker">
<script>
$( function() {
$( "#datepicker" ).datepicker();
} );
</script>
$date=$this->input->post('deadline');
I am trying to understand ajax and want to figure out how I can refresh a div that displays a rowcount from a mysql database live as the data is entered into that database.
I have a simple form on the same page and as the data is submitted from the form to the database how can I make the div update 'live' as well?
The code I've posted here posts a name from a form which is inserted into a mysql database. Then the number of rows in the database is counted and returned as a json object. It all works fine but only refreshes the rowcount when I reload the page and I want it to refresh instantly.
Many thanks.
The form
<form class="form-inline" action="" id="myform" form="" method="post">
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="name"></label>
<div class="col-md-8">
<input id="name" name="name" type="text" placeholder="name" class="form-control input-lg" required>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit1"></label>
<div class="col-md-4">
<button id="submitButtonId" name="submit1" class="btn btn-primary btn-xl">Submit</button>
</div>
</div>
</form>
<!---------Display rowcount from database--------->
The jquery
<script>
$(document).ready(function(){
$("#submitButtonId").on("click",function(e){
e.preventDefault();
var formdata = $(this.form).serialize();
$.post('data.php', formdata,
function(data){
//Reset Form
$('#myform')[0].reset();
});
return false;
});
});
</script>
<script>
$(document).ready(function() {
$.ajax({
url: 'data.php',
dataType: "json",
success: function (data) {
$("#count").append(data.count);
}
});
return false;
});
</script>
data.php
<?php
//include db configuration file
include_once("db_conx.php");
$name= mysqli_real_escape_string($db_conx,$_POST['name']);
//Update Database
$stmt = $db_conx->prepare('INSERT INTO my_table set name?');
$stmt->bind_param('s',$name);
$stmt->execute();
//Count Rows
$sql="SELECT name FROM utility";
$query = mysqli_query($db_conx, $sql);
// Return the number of rows in result set
$rowcount=mysqli_num_rows($query);
// sending JSON output
$my_data=array(count=>"$rowcount");
echo json_encode($my_data,true);
?>
If you want the server to push events to the client, you can use Websockets. There are services like Pusher that can help, it has a free plan (100 connections, 200K messages per day) and a good documentation to integrate with PHP and some popular frameworks.
If you don't want to use websockets, you can use a more traditionnal polling : every X seconds, you make a GET request to the server asking for the count, if it changes you update it, if not you do nothing and wait for the next call. This can be setup easily with setTimeout() in Javascript.
With PHP + ajax you should query to the database every X time with a timeout (setTimeOut()).
You could use websockets or take a look to firebase.
Also I suggest you to change .append(data.count); to .html(data.count); in order to 'clean' the div, if not, you may have multiple 'data.count' on it.
Here a post with a lot of answers for this: What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?
I'm working on a planner using bootstrap, php and jquery and i'd like to use a datetimepicker (http://www.eyecon.ro/bootstrap-datepicker/) to go to a different date on the planner and view the tasks of that day.
I want to use a normal button to trigger the datepicker. Then, whenever the user picked a date, use GET to create a link like index.php?page=planner&date=2013-08-03. I'm trying not to use an input field, but whenever a date is clicked to create a GET link immediately if possible..
Example image:
How to create a button working like this?
I'm using the following HTML:
<a id="diff-date" class="btn btn-large btn-primary" href="#">Date <i class="icon-calendar icon-white"></i></a>
An the following JQuery:
$(function() {
$('#diff-date').datetimepicker({
pickTime: false
});
});
Without succes so far.. I hope someone can help!
EDIT
Normally I use this HTML:
<label>Datum:</label>
<div id="datum" class="input-append">
<input data-format="dd-MM-yyyy" type="text" name="datum" placeholder="Kies een datum..." /><span class="add-on"><i data-date-icon="icon-calendar"></i></span>
</div>
And this JQuery:
$(function() {
$('#datum').datetimepicker({
pickTime: false
});
});
To achieve this:
You're confusing:
http://tarruda.github.io/bootstrap-datetimepicker/
With this:
http://www.eyecon.ro/bootstrap-datepicker/
They're not the same.
Try this :
$(document).ready(function() {
$('#diff_date').datepicker().on('changeDate', function(ev){
var choosenDate = ev.date;
//Add a specific treatment for the date here...
$('#diff-date').datepicker('hide');
});
$('#diff_date').click(function() {
$('#diff-date').datepicker('show');
});
}
And change the html of your link like :
<a id="diff-date" class="btn btn-large btn-primary" href="#" data-date-format="yyyy-mm-dd" data-date="2012-02-20">Date <i class="icon-calendar icon-white"></i></a>
Just for information, this is the code used in the site to make a link with a calendar :
var startDate = new Date(2012,1,20);
var endDate = new Date(2012,1,25);
$('#dp4').datepicker()
.on('changeDate', function(ev){
if (ev.date.valueOf() > endDate.valueOf()){
$('#alert').show().find('strong').text('The start date can not be greater then the end date');
} else {
$('#alert').hide();
startDate = new Date(ev.date);
$('#startDate').text($('#dp4').data('date'));
}
$('#dp4').datepicker('hide');
});
And the Html for that
Start dateChange
I have a input text in the form for date and i use JQuery datepicker to select date.
<input type="text" id="datepicker" name="datepicker" value="Date"/>
when i use form post to post the values into another php for calculation. i don't get the values from this input text alone. i get an empty value instead of selected date.
$date=$_POST['datepicker'];
i'm not sure of how to pass the datepicker value in form POST. Can someone help me out?
jQuery's datepicker has nothing to do with posting your form. If your form is set to "post" then whatever is left in that input field after the user has selected a date will be sent with the form.
<form action="process.php" method="post">
<input type="text" id="datepicker" name="datepicker" value="Date"/>
</form>
Gives you:
$date = $_POST['datepicker'];
However if your form is being sent as a "get" request you'll need to access it as such.
<form action="process.php" method="get">
<input type="text" id="datepicker" name="datepicker" value="Date"/>
</form>
Gives you:
$date = $_GET['datepicker'];
$( "#currentDate" ).datepicker("setDate", new Date("<?php echo $_POST['myCurrentDate'] ?>"));
works for me, read at least 10 very complicated ways of doing it and then realised this works. You are instantiating a new value each time. you store the date somewhere in your php scripts, using form input or some other technique and then recycle it back into the above statement. So reloading doesn't affect the result.
If myCurrentDate is unset it will set it by default. Getting it to be set to today's date requires a php if statement to set it the first time (or reload the page) to new Date() and the else statement can be used to include the above code.
That's a simple form with a datepicker
<form id="myform" name="myform">
<input type="text" id="datepicker" name="datepicker" value="Date"/>
<input type="button" id="submitMe">
</form>
Now your jquery,we are going to use some AJAX stuff:
//init datepicker
$(function() {
$("#datepicker").datepicker({
showButtonPanel: true
});
});
//pass deatepicker to php
$("#submitMe").click(function() {
$.ajax({
type: 'POST',
url: "pass.php",
dataType: "json",
data: $('#myform').serialize(),
success: function(data) {
console.log("Done");
}
});
return false;
});
And finally your pass.php file
<?php
//note that date may have slashes or dots so we url decode it
$date = urldecode ($_GET['datepicker']);
echo "chosen date is: ".$date;
?>
I need to assign javascript client Date (examp. - 2012/02/03 16:00:00) to php variable. Any idea how? I was trying to use this lines and changing them in million different ways. But I just cant get it.
Today = new Date();
var date = ????
var date = "<?= $date ?>";
I solved it this way:
<input id="date" type="hidden" name="date">
<script type="text/javascript">
document.getElementById('date').value = Date();
</script>
But thank you very much all.
Add an input in your form
<input type="hidden" name="clientDate">
if you are using jquery add this to set the client date input when the user submits the form
$(YOUR_FORM_SELECTOR).on("submit", function() {
$("[name=clientDate]").val(new Date());
});
If you want to go with vanilla javascript follow this answer
you can't simply assign a javascript variable to a php variable. php runs on the serverside and is executed before javascript.
you can however submit an ajax call with the value of your javascript variable to a php script.
you might wanna have a look at jquery's post function.
$.post("test.php", { yourDate: date } );
in your PHP script you'll be able to access the date with $_POST['yourDate']
you can also use a form and a hidden field as you say in your comment.
in this case you can use (assuming you're using jQuery)
$('#id_of_input').val(date);
This will convert js variable to php variable and php variable to js variable
<script>
function jstophp(){
var javavar=document.getElementById("text").value;
document.getElementById("rslt").innerHTML="<?php
$phpvar='"+javavar+"';
echo $phpvar;?>";
}
function phptojs(){
var javavar2 = "<?php
$phpvar2="I am php variable value";
echo $phpvar2;
?>";
alert(javavar2);
}
</script>
<body>
<div id="rslt">
</div>
<input type="text" id="text" />
<button onClick="jstophp()" >Convert js to php</button>
<button onClick="phptojs()">Convert php to js</button>
PHP variable will appear here:
<div id="rslt2">
</div>
</body>
Demo: http://ibence.com/new.php