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.
Thank everybody!!
Cláudio Baptista / Rio de Janeiro / Brasil.
Something like this might work...
Code:
window.onunload = getInfo;
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;
}
function getInfo(visitorEnter, visitorExit) {
request = createRequest();
if (request == null) {
alert("Unable to create request");
return;
}
var url= "getInfo.php?visitorEnter=" + escape(visitorEnter) + "&visitorExit=" + escape(visitorExit);
request.open("GET", url, true);
request.onreadystatechange = checkRequest;
request.send(null);
}
function checkRequest() {
if (request.readyState == 4) {
if (request.status == 200) {
alert("success! Or is it?");
}
}
}
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;
}
request.onreadystatechange = checkRequest();
request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send(requestMinData);
request.send(requestMaxData);
}
pslider.subscribe('slideEnd', psavestate); // Slider ends, start method psavestate
})
// Browser Support Code
function createRequest(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
return ajaxRequest;
}
function checkRequest() {
if (request.readyState == 4) {
if (request.status == 200) {
var response = request.responseText;
//alert(response);
}
}
}
</code>
As for my php code, I wrote the following to test to see if this worked... and so far it hasn't been working quite well for me....
<code>
<?php $v = $_REQUEST['savemin'];
echo $v; ?>
</code>
Please take a look, thank you in advance again! :)