so I have this javascript function on my page which computes date. Now, I want to access the result thrown by the function in a php variable so that I can use that variable to insert value in a database. The function should be called on page load. I have been trying to get this done from past 4 hours but upto no solution, any suggestions and solutions are welcome.
My JS Code:-
<script language="JavaScript">
// function to calculate local time
// in a different city
// given the city's UTC offset
function calcTime(city, offset,selDate,selHours,selMinutes) {
//alert(selDate);
//alert(selHours);
//alert(selMinutes);
//alert(offset);
var str = selDate;
var res = str.split("/");
//07,26/2014
//0,1,2
var d = new Date(res[2], res[0], res[1], selHours, selMinutes, 0, 0);
// create Date object for current location
//alert(d);
// var d = new Date();
//alert(d);
// convert to msec
// add local time zone offset
// get UTC time in msec
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// create new Date object for different city
// using supplied offset
nd = new Date(utc + (3600000*offset));
// return time as a string
return nd.toLocaleString();
}
function testTime(){
var selDate = document.getElementById("datepicker").value;
var selHours = document.getElementById("hours").value;
var selMinutes = document.getElementById("minutes").value;
var selTimeZone = document.getElementById("timezone").value;
alert(selDate);
alert(selHours);
alert(selMinutes);
alert(selTimeZone);
}
</script>
3 Answers 3
To allow your JavaScript to communicate with your PHP backend, you'll have to use Ajax. This allows you to send information to a PHP file.
For the sake of keeping it simple, I suggest you use jQuery, it has two useful methods you can use $.ajax and $.get
Your JavaScript could look something like this:
$(document).ready(function(){
var timeToSend = calcTime(city, offset,selDate,selHours,selMinutes);
//we are sending information to saveTimeToDB.php
$.ajax('saveTimeToDB.php', {
data: {time:timeToSend}//this is an object containing all the information you want to send
}).done(function(){
//a function which gets called if the ajax call succeeded
alert('the time has been saved');
}).fail(function(){
//a function which gets called if the ajax call failed
alert('Uh-oh, something went wrong. We did not save the time');
});
});
In the 'saveTimeToDB.php' file you can now access this variable with:
$time = $_GET['time'];
You should then be able to use this variable and insert it in your database.
Make sure to do many checks on this variable, to make sure you're not injecting anything malicious in to your DB.
Comments
Javascript and PHP do't have any interaction due to JS being client side and PHP being server side.
PHP will be executed before the JS.
So, what you can do is use a hidden field:
<input type="hidden" name="hid" id="date">
and the value of javascript variable in that:
document.getElementById('date').value=date; //whatever value you want to store
and then simply fetch the value of this field like you do normally with textboxes.
Or simply use AJAX to send data from client to server as others already said:
$.ajax({
url: something.php // current page
type: 'POST',
data: {
var1: 'date' // of if writing a JS variable remove the quotes.
},
success: function() {
// whatever
}
});
to call a function on page load write: <body onload="foo()">
7 Comments
function calcTime?I'm not sure about the JS, but if the function is correct then try to write this before </script> of your posted code above, and open the file (from browser):
alert calcTime('city', 3,2,12,30);
If what's alerted is what you want, then:
$.get( "ajax/dowork.php?calculatedTime=" + calcTime('city', 3,2,12,30) );
... will call dowork.php (where you put DB insert codes in it, and you'll have $_GET['calculatedTime'] to insert).
3 Comments
$.post( "test.php", { name: "John", time: "2pm" } ); --- the basic idea is, you can't use JS-generated value on a PHP function on the same page (because PHPs are done, before any JS is executed).
$.get( "ajax/dowork.php?selected=" + selDate );