I have a mypage.php and it contains the below code
And i have added the
<link style="text/css" href="http://jqueryui.com/themes/base/jquery.ui.all.css" rel="stylesheet">
<script type="text/javascript" src="/scripts/jquery.js"></script>
<script type="text/javascript" src="/scripts/ui/jquery.ui.core.js"></script>
<script type="text/javascript" src="/scripts/ui/jquery.ui.widget.js"></script>
<script type="text/javascript" src="/scripts/ui/jquery.ui.datepicker.js"></script>
<form name="test_date" method="post">
<input type="text" id="datepicker1" name="date1">
<input type="submit" name="insert" value="Insert and show">
</form>
And In jquery.ui.datepicker.js i have change the date format to dateFormat: 'dd/mm/yy' from dateFormat: 'mm/dd/yy'
and below is the jquery for selecting the date when click on textbox
<script>
$(function() {
$( "#datepicker1" ).datepicker();
});
</script>
And when i click on submit button i have inserted into mysql database database
<?php
if($_POST['insert']) {
echo "Date : ";
$d = $_POST['date1'];
require('con_open.php');
$t = date('Y-m-j H:i:s', strtotime($d));
mysql_query("insert into date_table (date_field1) values ('$t')");
echo "Converted to Date at Runtime :" . $t;
echo "<br>";
$query = mysql_query("select date_field1 from date_table");
while($r = mysql_fetch_row($query))
{
got_date_db = $r[0];
}
echo "Displaying date from Database : " . got_date_db;
require('con_close.php');
}
?>
Output :
Converted to Date at Runtime : 2011-12-28 14:32:16
Displaying date from Database : 0000-00-00 00:00:00
date_field1 (DATETIME) fromat on mysql
Even i have tried out checking with dateformat of mysql and php date function manuals online and over stackoverflow... but i could not get any solution to this one.
Try:
mysql_query("
insert into date_table (date_field1)
values (FROM_UNIXTIME(". (int)strtotime($_POST['date1']) ."))
");
I think its generally a good idea to pass a date as a timestamp to the database. Then you don't have to worry about formatting it the right way.
Related
Refer to this image:
In my company table, I have 3 fields:
- ID (int auto_increment)
- start_date (date)
- end date (date)
Here is my code:
<html>
<head>
<script type="text/javascript">
$(function(){
$("#datepicker1").datepicker({
dateFormat: 'dd-mm-yy',
onSelect: function(selected){
$("#datepicker2").datepicker("option","minDate",selected);
}
});
$("#datepicker2").datepicker({
dateFormat: 'dd-mm-yy',
onSelect: function(selected){
$("#datepikcer1").datepicker("option","maxDate",selected);
}
});
});
</script>
</head>
<body>
<?php
//db connection
$q = "select * from company where ID = '".$_GET['ID']."'";
$r = mysqli_query($dbc, $q);
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<tr>
<td>Start Date : </td>
<td><input type="text" name="exhstartdate" id="datepicker1" value="<?php if($row['start_date'] == NULL) echo ''; else echo date('d-m-Y', strtotime($row['start_date'])); ?>" /></td>
</tr>
<tr>
<td>End Date : </td>
<td><input type="text" name="exhenddate" id="datepicker2" value="<?php if($row['end_date'] == NULL) echo ''; else echo date('d-m-Y', strtotime($row['end_date'])); ?>" /></td>
</tr>
</form>
</body>
</html>
My requirement is end date cannot greater than start date.
How should I disable the end date value when the end date value is greater than the start date value from the database?
<script>
$(document).ready(function(){
setEndDatePicker(); //set end date picker based on start date
});
var startDate = $("#datepicker1").val(); //get start date value and split to year, month and day
var sDate = startDate.split("-");
var day = sDate[0];
var month = parseInt(sDate[1]) - 1;
var year = sDate[2];
$("#datepicker1").datepicker({
dateFormat: 'dd-mm-yy'
});
// set again the minDate for end datepicker when start date changes
$("#datepicker1").change(function(){
startDate = $("#datepicker1").val(); //get new start date value
sDate = startDate.split("-");
day = sDate[0];
month = parseInt(sDate[1]) - 1;
year = sDate[2];
setEndDatePicker(); //reset the minDate for end datepicker
});
function setEndDatePicker(){
$("#datepicker2").datepicker({
minDate: new Date(year, month, day), //use dynamically initialized minDate
dateFormat: 'dd-mm-yy'
});
}
</script>
I have code to display a table, mysql queries fetching data from a database. One of the columns is dates. I would like to use the jquery datepicker to sort the table. I've included the correct files and show the calendar on my site but how do I connect it to my table so I can sort my data?
<form name="myForm" action="/page.php" onsubmit="return validateForm()" method="get">
<input type="text" id="from" name="fromDate" value=""/>
<input type="text" id="to" name="toDate" value=""/>
<input type="submit" name="Find Dates" value="Submit" />
</form>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#from').datepicker({
defaultDate: "+1w",
onClose: function( selectedDate ) {
$( "#to" ).datepicker( "option", "minDate", selectedDate );
}
});
jQuery('#to').datepicker({
defaultDate: "+1w",
onClose: function( selectedDate ) {
$( "#from" ).datepicker( "option", "maxDate", selectedDate );
}
});
});
</script>
<?php
$values = $wpdb->get_results($wpdb->prepare( "SELECT DISTINCT DATE FROM `signup` ORDER BY DATE ASC"));
?>
<table>
<tr>
<th>Date</th><th>B</th><th>D</th>
</tr>
<tr>
<?php
foreach($values as $v_date){
$date = $v_date->DATE;
$count = $wpdb->get_results($wpdb->prepare(
" SELECT DISTINCT `date`,
(select count FROM signup where LIST_NAME = 'B' AND date = '$date') as 'B',
(select count FROM signup where LIST_NAME = '2 - D' AND date = '$date' ) as 'D', FROM wp_email_signup WHERE date ='$date'"));
echo '<tr class="row_main">';
echo '<td>' . $date . '</td>';
foreach($count as $counts){
$val1 = $counts->B;
$val2 = $counts->D;
echo '<td>' . $val1 . '</td>';
echo '<td>' . $val2 . '</td>';
echo '</tr>';
}
?>
</tr>
</table>
You can get value from datepicker like this:
<script>
jQuery('#from').click(function(){
//this is the value of datepicker
var datepickerVal = $(this).val();
//here you you have to pass this value to php
window.document.location = '?from =' + datepickerVal;
})
</script>
After that after page reload you can get this value in php from $_GET array and use it as you want
You could call a different PHP file in jQuery using AJAX. In that PHP file you query anything you want to the database, and echo the results in a JSON syntax. This way, you can parse it from Javascript and show it on the datepicker :)
References:
jQuery AJAX: http://api.jquery.com/jquery.ajax/
Encode a PHP array to JSON: http://php.net/manual/es/function.json-encode.php
Whoa, take it easy. You should not mix all the code into one file. You should separate database code, php code, javascript and css code as much as possible. You should have a code which runs the search with the necessary parameters and retrieves the data. You should post the parameters using ajax when the date value has been changed. Here you can see an example of posting with ajax.
Hello I have a datepicker calendar and what I want is that we I select the two dates then I want to click on a button it displays me all the data which are between those dates.
Here is my code...
<?php
include_once 'header.php';
$con = mysqli_connect('localhost','root','smogi','project');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$view = ($_GET['view']);
$username2 =$_SESSION['username'];
$sql="SELECT typeValue,unit,sensorValue,time FROM sensors WHERE username='$view' AND time BETWEEN $firstdate AND $lastdate ORDER BY time DESC";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr> <b>Type: </b>";
echo stripslashes($row['typeValue']) . "<br/><b>Unit: </b>";
echo stripslashes($row['unit']) . "<br/><b>Value: </b>";
echo stripslashes($row['sensorValue']) . "<br/><b>Date & Time: </b>";
echo stripslashes($row['time']) . "<br/>";
echo "--------------------------------------------------------------------------------------------------------------";
echo "<br/></tr>";
}
echo "</table>";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
var firstdate = $('#firstdatepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val();
//$( "#firstdatepicker" ).datepicker();
});
</script>
<script>
$(function() {
var lastdate = $('#lastdatepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val();
//$( "#lastdatepicker" ).datepicker();
});
</script>
</head>
<body>
<form method="post" action="graph.php">
<p>First Date: <input type="text" id="firstdatepicker"></p>
<p>Last Date: <input type="text" id="lastdatepicker"></p>
<input type="submit" value="Get Data" name="data"/>
</form>
</body>
</html>
But it doesn't work as I want. Can you help me please to make it work ???
Thank you for your time .
PS: in the image below we can see how my database table look like
Firstly, you never define $firstdate $lastdate. You should like this:
$firstdate = $_POST['firstdatepicker'];
$lastdate = $_POST['lastdatepicker'];
If it's as you have it in your "time" column, they need to be quoted.
BETWEEN '$firstdate' AND '$lastdate'
using error checking on your query would have thrown a syntax error
http://php.net/manual/en/mysqli.error.php
I.e.: $result = mysqli_query($con,$sql) or die(mysqli_error($con));
MySQL reads datetime as YYYY-mm-dd
https://dev.mysql.com/doc/refman/5.0/en/datetime.html
You're using dateFormat: 'yy-mm-dd'
Plus, give your inputs name attributes:
<input type="text" id = "firstdatepicker" name = "firstdatepicker">
<input type="text" id = "lastdatepicker" name = "lastdatepicker">
and adding
$firstdate = $_POST['firstdatepicker'];
$lastdate = $_POST['lastdatepicker'];
as stated above.
you forgot to add the name="firstdatepicker" and name="lastdatepicker" in your form and you need to fill your variables from $_POST superglobal array (and I desperately hope you don't use register_globals=on).
Updated HTML part:
<script type="text/javascript">
$(function() {
$('#firstdatepicker, #lastdatepicker').datepicker({ dateFormat: 'yy-mm-dd' });
});
</script>
<form method="post" action="graph.php">
<p>First Date: <input type="text" name="firstdatepicker" id="firstdatepicker"></p>
<p>Last Date: <input type="text" name="lastdatepicker" id="lastdatepicker"></p>
<input type="submit" value="Get Data" name="data"/>
</form>
On the PHP side:
if (isset($_POST['firstdatepicker'])) {
$firstDate= $_POST['firstdatepicker'];
$lastDate= $_POST['lastdatepicker'];
// forgot the single quotes around the dates ...
$sql="SELECT typeValue,unit,sensorValue,time FROM sensors WHERE username='$username2' AND time BETWEEN '$firstdate' AND '$lastdate' ORDER BY time DESC";
}
I have a problem with the plugin in jQuery it doesn't show me any results up. I dont where I made the mistake?
thats my newJob.php:
<form method="post" action="newJob.php">
<span>Customer Name:<input type="text" id="customerName" name="customerName"></span><br>
<input type="submit" name="submitJob" value="Create Job" >
</form>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/jquery-ui.js"></script>
<script type="text/javascript" src="../js/startDate.js"></script>
<script type="text/javascript">
$(function() {
//autocomplete
$("#customerName").autocomplete({
source: "searchCustomer.php",
minLength: 1
});
});
</script>
My searchCustomer.php:
<?php
include('classes.php');
$obj = new CompanyDatabase;
$obj->connect_db();
$obj->select_db();
if (isset($_POST['customerName'])) {
$query = $_POST['customerName'];
$sql = mysql_query ("SELECT `Customer Name` FROM `job` WHERE `Customer Name` LIKE \"%".$query."%\"");
$array = array();
while ($row = mysql_fetch_assoc($sql)) {
$array[] = $row['customerName'];
}
echo json_encode ($array); //Return the JSON Array
}
?>
Whats wrong with it? Thanks for your help!
I believe MySQL doesn't allow you columns and table names to have spaces beetween names. Are you sure you're using the correct column and table names?
This is a two fold question. How do I update the data (without page reload) on the page when the datepicker is changed?
Also how do i pass the selected date to the dtpickerdate in my sql statement?
Here is my example code
<html>
<head>
<link href="../css/jquery/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="../js/jquery-1.9.1.js"></script>
<script src="../js/jquery-ui.js"></script>
</head>
<body>
<?php include("../navbar.php"); ?>
<p>Date: <input type="text" id="datepicker" value="<?php echo date("m/d/Y"); ?>" /></p>
<script type="text/javascript">
$(function() {
$("#datepicker").datepicker();
});
</script>
<table>
<?php
$sqltext = "SELECT * FROM data where startdate=dtpickerdate";
$result = $mysqli->query($sqltext);
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$key."</td>";
echo "<td>".$row['sun']."</td><td>".$row['mon']."</td><td>".$row['tues']."</td><td>".$row['wed']."</td><td>".
$row['thurs']."</td><td>".$row['fri']."</td><td> ".$row['sat']."</td>";
}}
?>
</table>
</body>
</html>
You need AJAX, first thing create a new php file with your query function that will receive a $_POST parameter with the value of the datepicker:
getData.php
<?php
$dtpickerdate = isset($_POST['dtpickerdate']) ? $_POST['dtpickerdate'] : NULL
$sqltext = "SELECT * FROM data where startdate = $dtpickerdate";
$result = $mysqli->query($sqltext);
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$key."</td>";
echo "<td>".$row['sun']."</td><td>".$row['mon']."</td><td>".$row['tues']."</td> <td>".$row['wed']."</td><td>".$row['thurs']."</td><td>".$row['fri']."</td><td> ".$row['sat']."</td>";
}
?>
Now you listen to the change event on your input and use AJAX to call your PHP, when the call completes you update your table:
$('#datepicker').change(function(){
$.post('getData.php',{dtpickerdate : $(this).val()}, function(response){
$('table').html(response);
});
});