How to echo / test a jQuery variable in php? - php

As the title says, how can i test/show my jQuery variable in php? I have created a datepicker and on the click of a specific date, that date needs to be stored into a variable so i can use it later on (like for example to show your date of birth).
The code of the datepicker and the Onselect is:
$(document).ready(function(){
$("#datepicker").datepicker({
onSelect: function(dateText, inst) {
alert(dateText);
document.getElementById('thedate').value=dateText;
},
showOn: "button",
buttonImage: "/debasis/hoofdstuk03/img/calendar.png",
buttonText: "Open de kalender",
buttonImageOnly: true,
inline: true,
showOtherMonths: true,
dayNamesMin: ['Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za','Zo'],
monthNames: ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni',
'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December'],
});
});
The HTML to show the datepicker is:
<div type="text" id="datepicker"></div>
<form method=post>
<input type='text' id='thedate' name='thedate' />
</form>
If im right, but correct me if i am wrong, when i click a date on the calendar, that date is being POST and the Jquery variable 'thedate' will be set in $_POST['thedate'] is this correct?
However to be sure i got the chosen date in my $_POST (so i can use this variable to store it in my databse) i wanted to test it with an echo. So in the same file i created this to test it:
<?php print_r($_POST); ?>
Though this little line doesnt return anything? Anyone knows how i can test/show 'thedate' variable (the chosen date)? So i know the chosen date is stored in a variable. I would like to get it returned in a PHP variable so i can store it in my databse.. Though if there are other ways to do it i am open for those solutions as well. :)

AJAX post it to an external php file. $_SESSION the var in that file and use it wherever else.

nope; it doesn't work like that. It's not enough to just change input value. You need to actually submit the form - which means comunicating with php which is BACK-END, to be able to show it using PHP.
the easiest way would be to submit the form in onSelect method:
HTML:
JS:
$("#datepicker").datepicker({
onSelect: function(dateText, inst) {
alert(dateText);
document.getElementById('thedate').value=dateText;
document.getElementById("myForm").submit();
},
...
and then print it out usign PHP:
<?php print_r($_POST); ?>

Related

php form with two dates validation (before submit)

I'm trying to create a validation for an "", but I can't understand how to.
The form has two dates:
DateStart
Datestop (must be after Datestart).
These variables are stored as associative array (routine['col'] =>'val').
I created this:
<form ..>
...
<tr>
<td>Star date: </td>
<td><input type="date" name="routine[Datestart]"></td>
</tr><?php $routine['Datestart']; ?>
<tr>
<td>Stop Date: </td>
<td><input type="date" name="routine[Datestop]"
min="routine[Datestart]"></td>
</tr>
...
</form>
Obviously, the min option doesn't work.
I saw that some solution are provided by Javascripts, but I never used Java, so if I could do it with a simple php variable it would be better, but I'm looking for the smartest solution, so I ask to you simply how to do that.
Validitions on the web are done in two places:
On the server (PHP). You always need to check user input on your server before inserting data from the wild webs into your database. The simplest possible validation to check that a field is not blank would be:
<?php
$errors = array();
if ( empty($POST["foo"]) || count($POST["foo"]) < 1) {
$errors["foo"] = "Foo can't be blank";
}
if ( empty($errors) ) {
// all good here
} else {
// display errors to user.
}
This is a pretty trivial example and great volumes have been written on different ways to validate user input.
To compare two dates in PHP you need to parse the input string into a two DateTime objects which you then can compare:
$start = DateTime::createFromFormat(DateTime::RFC3339, $routine['Datestart']);
$end = DateTime::createFromFormat(DateTime::RFC3339, $routine['Dateend']);
if ($start > $end) {
// its not valid.
}
In the browser. Since server side validions require the user to press send before getting feedback which is pretty slow at times they are often complemented with client side validations which give instant feedback while the user types. The are written in Javascript [not to be confused with Java] since it is the universal language of the browser.
However they are never a replacement for client side validations as a script error or the user not having javascript enabled would disable the validations.
you should use jquery like this.
jquery code
<script type="text/javascript">
$(function () {
$("#DateFrom").datepicker(
{
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true,
yearRange: '-50:+50',
maxDate: 'today',
onSelect: function (selectedDate) {
$("#DateTo").datepicker("option", "minDate", selectedDate);
}
}
);
$("#DateTo").datepicker(
{
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true,
yearRange: '-50:+50',
maxDate: 'today',
onSelect: function (selectedDate) {
$("#DateFrom").datepicker("option", "maxDate", selectedDate);
}
}
);
});
</script>
HTHML CODE OR JUST INCLUDE IT IN UR FORM
<center>
<input type="text" id="DateFrom"> To
<input type="text" id="DateTo">
</center>
you have to include jquery datepicker js and css. files.
try this one it will works.

How to get month in number from date in "JAN-2014" format?

Actually, i am using jquery datepicker and getting month in "MONTH-YEAR" format?
how i can get month number form this format in PHP or JAVASCRIPT?
js code:
$("#date1_live").datepicker({
changeMonth: true,
changeYear: true,
showAnim:'slideDown',
dateFormat:'M yy'
});
html text field:
<input type="text" name="reportMonth" id="date1_live" required Placeholder="Select Start Date" >
using php try like this
$month = 'JAN-2014';
echo date('m', strtotime($month));
Please take a look at the following documentation.
jQuery UI Datepicker
The format for parsed and displayed dates. For a full list of the
possible formats see the formatDate function.
Code examples:
Initialize the datepicker with the dateFormat option specified:
$( ".selector" ).datepicker({ dateFormat: "yy-mm-dd" });
try this,
alert($("#datepicker").datepicker({ dateFormat: "mm-yy" }).val());
or Demo
{
changeMonth: true,
changeYear: true,
showAnim:'slideDown',
dateFormat:"mm-yy"
}

jquery autocomplete bring value and key

first at all thanks for reading.
I was reading a lot of similar post but really, i dont understand how to get it working.
Here's the thing, i have a jquery autocomplete that brings an array from a php that i used json_encode. Example of the array
array('23' => 'Robert','3' => 'alaric'... and so on.
The input that is completed by the autocomplete obviously bring me the Value of that array.
But now what i need to do for other script is, in the input show the name (just the way its right now) but when i click at a button right next to that input (called it '+', "submit" buttono or doesnt matter the type) to complete a select multiple empty that is at the right, i will need to put as value the key of the array and the text of the option the Value of the array...
so here's my code of autocomplete and my input, hope you can help me
<input type="text" name="letter" placeholder="Nombre Medico..." id="search_medico"/>
$(function() {
$("#search_medico").autocomplete({
minLength: 3,
source: '<?php echo $general->full_path?>view/general/search_medico.php'
});
});
and i need to complete a select like
her ei need to add each one option
I don't get exactly what you want to do but to access value and label of a selected item in a jquery ui autocomplete, you can use the select event callback.
$(function() {
$("#search_medico").autocomplete({
minLength: 3,
source: '<?php echo $general->full_path?>view/general/search_medico.php',
select : function(event, ui) {
// ui.item.label is the displayed value
// ui.item.value is the key
// you can here put these values somewhere else in your page
}
});
});
EDIT :
Your source must be in this format :
[{value : 1977, label : "CARBALLO JACINTO"}, {value : 4527, label : "CARBALLO GAIS NATALIA"}, ... ]

Datepicker get date according to server date/time

How to make work datepicker according to server time , how to set the server time to datepicker Jquery Ui, any help will be appreciated
Yes via clinet side we cannot get the server datetime, is there any procedure to get the server date/time and apply to date picker in PHP?
<?php $_date=date("d-m-Y"); ?>
<input type="text" class="datepicker" name="_date" value="<?=$_date;?>"/>
hope this will help.
//In my case i have used folloing script. Hope it will help you!
$('#selectedPicker').datepick
({
yearRange: '1980:2010',
alignment: 'bottomRight',
onSelect: updateSelected,
altField: '#set_Date',
showTrigger: '#calImg'
}).datepick('setDate', '<?php echo $server_date; ?>');
Use this. It will work and set maxDate if possible so the greater date is not selected
$(function() {
$( "#ppcertdate" ).datepicker({ maxDate: "<?=date("d/m/Y");?>", dateFormat: 'dd/mm/yy' });
$("#ppcertdate").datepicker("option","defaultDate", "<?=date("d/m/Y");?>");
});

Get jQuery UI Datepicker selected date to a PHP Session variable?

How to get jQuery UI Datepicker selected date to a PHP Session variable when a date selected by user?
Make an AJAX call when the onSelect is trigged.. Just dummy code you can play with:
$('.selector').datepicker({
onSelect: function(dateText, inst) {
$.ajax({
url: "myPHPscript.php?selecteddate=" + dateText,
success: function(){
$(this).addClass("done");
}
});
}
});
Description
You can do this using Ajax. After the Datepicker is closed you can set the Session Variable using a Ajax call.
You can use the DatePicker event onClose if you want to set the variable after the dialog is closed or onSelect after a date is selected. I would prefer the onClose event.
Sample
jQuery
$('.selector').datepicker({
onClose: function(dateText, inst) {
$.post("/backend.php", {"VariableName": dateText});
}
});
PHP (backend.php)
<?php
// set the variable
$_SESSION['VariableName'] = $_POST['VariableName'];
?>
More Information
jQuery.ajax

Categories