...

View Full Version : Passing on my Javascript Variables to PHP Variables using AJAX - problem.



freedomgw
02-01-2011, 02:10 PM
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
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);
}
}
}

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....

<?php $v = $_REQUEST['savemin'];
echo $v; ?>

Please take a look, thank you in advance again!

devnull69
02-01-2011, 09:06 PM
Two major problems ... fix them and then test again

1. onreadystatechange is supposed to get assigned to a function reference but you assign it to the result of a function call. Please omit the parentheses ().
2. You can not send two values using two .send() commands. Instead you will have to build a URI compliant parameter string of the form


parameter1=value1&parameter2=value2&parameter3=value3&....

Maybe something like that:


request.send("requestMinData="+encodeURIComponent(requestMinData)+"&requestMaxData="+encodeURIComponent(requestMaxData));

Those values will then be available in PHP as $_POST['requestMinData'] and $_POST['requestMaxData']

freedomgw
02-01-2011, 09:36 PM
Hello, thank you for the response; I have done what you have suggested and I have received the following errors:

Notice: Undefined index: requestMinData in /Applications/MAMP/htdocs/trunk/application/modules/cne/admin/views/scripts/index/index.phtml on line 113

Notice: Undefined index: requestMaxData in /Applications/MAMP/htdocs/trunk/application/modules/cne/admin/views/scripts/index/index.phtml on line 114

I am starting to think that my URL might be one of my other problems, both my php and javascript are on the same page... is it okay if I refer to the same page for the url?

This is the place that I am referring to..
var url = "/Applications/MAMP/htdocs/trunk/application/modules/cne/admin/view/scripts/index/index.phtml";

request.open("POST", url, true);

Still looking for other problems that might have displayed this error :eek:

Thank you in advance! :thumbsup:

devnull69
02-01-2011, 10:50 PM
this URL is definitely not correct. You should either provide a full URL (e.g. "http://www.yourdomain.com/path/to/blabla.php") or a path relative to the javascript (e.g. "../php/blabla.php")

The errors are not Javascript errors. They seem to be PHP errors ... how do you output the Javascript from PHP? It seems to me you're not correctly separating PHP from Javascript here

freedomgw
02-03-2011, 08:02 PM
nono, my mistake on my wording. my javascript and php are all sharing in the same phtml file which is /Applications/MAMP/htdocs/trunk/application/modules/cne/admin/views/scripts/index/index.phtml

I am pretty sure that my javascript and php are both at their right places.. hm.... thank you for the help so far!

I am still trying to get it work though! ^_^ any more help will be appreciated

Here is my code so far with the changes!

Javascript


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/views/scripts/index/index.phtml", // perhaps the URL is wrong.
savemin = pconvert(pslider.minVal),
savemax = pconvert(pslider.maxVal),
requestMinData = "savemin=" + savemin,
requestMaxData = "savemax=" + savemax;

// 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);
}
}
}


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="+encodeURIComponent(requestMinData)+"&requestMaxData="+encodeURIComponent(requestMaxData));
}

pslider.subscribe('slideEnd', psavestate);




<?php $v = $_POST['requestMinData'];
echo $v;
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum