Need pass JS vars to PHP without using <form> <input> <button> w/ AJAX POST method.
Hello.
I'm a newbie in web aplications development and I've been studing in the W3Schools tutorials ( and other places in web ).
I'm using HTML with JavaScript, PHP and MySQL in a Xampp Lite environment.
I want to record in a MySQL table the date and time the visitor entered and left tha page.
I have the two informations stored in string javascript variables.
But although I've found the AJAX, I just can't make it works.
I don't need a form or input HTML tag.
When the visitor call the page, I get the date and time. And when he leave the page ( onunload event ) I get the other date and time and, at this moment I want to pass this JavaScript variable to the PHP script.
Both codes are ok and have been tested separated. The rows are inserted in the MySQL table correctly when I initialize them inside the PHP code.
And in browser-side, with JavaScript, I can see that they ( the variables ) are Ok by displaying it.
// JavaScript Document
window.onload = initSite;
window.onclose = exitSite;
var visitorEnter = "";
function initSite() {
var date = new Date();
var y = date.getFullYear();
var m = date.getMonth();
var d = date.getDay();
var h = date.getHours();
var i = date.getMinutes();
var s = date.getSeconds();
// make a timestamp
var timestamp = new Date(Date.UTC(y, m, d, h, i, s));
visitorEnter = timestamp.getTime()/1000;
//alert(visitorEnter);
}
function exitSite() {
var url = "php.php";
var requestData = "visitorEnter=" + visitorEnter;
var request = createRequest();
if (request == null) {
alert("Unable to create request");
return;
}
request.onreadystatechange = checkRequest;
request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send(requestData);
}
function checkRequest() {
if (request.readyState == 4) {
if (request.status == 200) {
var response = request.responseText;
//alert(response);
}
}
}
function createRequest() {
try {
request = new XMLHttpRequest();
} catch (tryMS) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (otherMS) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = null;
}
}
}
return request;
}
PHP Code:
//** you must first connect to your database **//
// store timestamp as getdate() array $dArray = getdate($_REQUEST['visitorEnter']);
been trying to move javascript variables to php variables for a VERY LONG TIME =(
Hi, I wish to resurrect this topic because I am encountering some problem even after looking through your examples . I hope someone out there can help me with my coding in AJAX. I am trying to send two javascript variables to PHP variables using AJAX. Please have a look at my code, thank you in advance!
In my javascript, I am using YUI dual slider, so when I end sliding the mouse I want to engage these functions such that....
javascript code
<code>
var psavestate = function(){ // THESE ARE CURRENTLY SAVED VALUES, WE CAN NOW USE THESE VALUES FOR A PHP FUNCTION FOR QUERYING
var url = "/Applications/MAMP/htdocs/trunk/application/modules/cne/admin/view/scripts/index/index.phtml",
savemin = pconvert(pslider.minVal), // pconvert is some function
savemax = pconvert(pslider.maxVal),
requestMinData = "savemin=" + savemin,
requestMaxData = "savemax=" + savemax;
var request = createRequest();
if (request == null) {
alert("Unable to create request, please update your browser to use this feature.");
return;
}
Well, my friend, I'm a newbie in webdevelopping and don't speak english very well, too.
But lets try.
See my PHP code (in blue) in the bottn of this message....
The only difference from your code and the things I do in my code is that I use only one send command. The red part of the code above....See that I put all the variables in a unique string called "parâmetro"...
parâmetro = "dataHoradeAcesso=" + dataHoradeAcesso // Parâmetro a ser passado na abertura da
parâmetro = parâmetro + "&" + "dataHoradeSaída=" + dataHoradeSaída // requisição HTTP (XMLHttpRequest / AJAX)
// Trata a requisição HTTP
requisição = false
limite = 0
while ( !requisição ) //&& limite < 3000 ) // tenta 3.000 vezes antes de interagir com o usuário
{
try
{
requisição = new XMLHttpRequest();
}
catch (tryMS)
{
try {
requisição = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (otherMS)
{
try {
requisição = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (failed)
{
requisição = false;
}
}
}
limite ++
}
if (requisição == null) { // Se não foi capaz de efetuar a requisição HTTP, retorna sem
// fazer nada. É indiferente para o usuário.
// alert("A requisição HTTP falhou!!");
return;
}
Bookmarks