Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing on my Javascript Variables to PHP Variables using AJAX - problem.

    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!

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    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
    Code:
    parameter1=value1&parameter2=value2&parameter3=value3&....
    Maybe something like that:
    Code:
    request.send("requestMinData="+encodeURIComponent(requestMinData)+"&requestMaxData="+encodeURIComponent(requestMaxData));
    Those values will then be available in PHP as $_POST['requestMinData'] and $_POST['requestMaxData']

  • #3
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you for the response

    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

    Thank you in advance!

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    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

  • #5
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    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/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 Code:
    <?php $v $_POST['requestMinData'];
                      echo 
    $v;
    ?>


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •