We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28764
    • 3 Posts
    Hi there,

    For your information... I’m using MODx 2.0.7 traditional and jQuery.

    I ran into the following problem: I have a form on my page and when the page unloads, I want to save the values of the form fields in the session. On other websites using another CMS I did this: On unload I call jQuery’s ajax-function to send the values of the form fields to a PHP-file using the POST-method. the PHP-file saves the values in the $_SESSION-array and echos the success or failure back to the jQuery part. Everything works fine there. But now on my MODx-site i can’t get this to work right. I don’t get any error message nor is the success part of the ajax-call triggered. Just nothing happens. When I replace the $.ajax(...) with alert(’something’), I get the alert so the error has to be in the $.ajax-part. I hope I made my problem clear so everyone can understand it. Here are the bits of code I use...

    The jQuery-AJAX-call:
    $(window).unload(function() {   
        $.ajax({
            type: "POST",
            url: "http://www.example.com/modx/saveToSession.php",
            data: {
    	    formfieldvalue1: $('[name=formfield1]').val(), 
    	},
    	success: function(msg){
                alert('bla');
    	}
        });
    });
    


    The called PHP file:
    session_start();
    if($_POST) {
        $_SESSION['formvalues'] = '';	
        $formvalues = array($_POST['formfieldvalue1']);
        $_SESSION['formvalues'] = $formvalues;
        $data['success'] = true;
    } else {
        $data['success'] = false;
    }
    echo json_encode($data);
    


    The strange thing is, that even when I replace the content of the PHP-file with something like "echo ’success’;", the success-part in the AJAX-call is still not triggered. I’m very confused and I hope somebody out there is able to help me solve this, cause this has been driving me crazy for days now.

    Thanks a lot...
    Joachim
      • 4172
      • 5,888 Posts
      I would try to put your php-code into a snippet without session_start().

      then put a snippet-call of that snippet to a new resource with blank template.

      call the url of that resource in your ajax-request.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 26903
        • 1,336 Posts
        Put an error function in your AJAX handler as well as the success one, see if this is triggered, jQuery API refers e.g
        error(jqXHR, textStatus, errorThrown)Function
        A function to be called if the request fails......
          Use MODx, or the cat gets it!
          • 28764
          • 3 Posts
          Hi,

          I tried what y’all suggested, but I’m not any closer to getting this to work.

          I removed every JavaScript from my page and changed the AJAX-part to the following:
          $(document).ready(function() {
              $(window).unload(function() {   
                  $.ajax({
                      url: "[[++site_url]][[~26]]",
                      statusCode: {
                          404: function() {
                              alert('page not found');
                          },
                          200: function() {
                              alert('page found');
                          }
                      },
                      type: "POST",
                      data: {
          	        formfielvalue1: $('[name=formfield1]').val() 
          	    },
                      success: function(){
          	        alert('success');
          	    },
                      error: function(){
                          alert('error');
                      }
                  });
              });
          });
          


          I put PHP-code into a snippet and made a document (ID:26) calling this snippet like that:
          [[!saveToSession]]
          
          
          In order to minimize potential sources of errors I just have the following in the snippet:
          echo 'thisIsTheReturnvalueOfTheSnippet';


          But anyways... I don’t get any status alert, success or error message. It just doesn’t do anything. Does anybody know what I am missing? I just don’t get the mistake, as this works on other websites with other CMS.
            • 4172
            • 5,888 Posts
            try this one and you will see, when you are waiting long enough, there comes an answer. (testet!)

            $(document).ready(function() {
                $(window).unload(function() {   
                    $.ajax({
                        url: "[[++site_url]][[~26]]",
                        statusCode: {
                            404: function() {
                                alert('page not found');
                            },
                            200: function() {
                                alert('page found');
                            }
                        },
                        type: "POST",
                        data: {
            	        formfielvalue1: $('[name=formfield1]').val() 
            	    },
                        success: function(){
            	        alert('success');
            	    },
                        error: function(){
                            alert('error');
                        }
                    });
                    alert('Ich warte erstmal noch auf Antwort von [[++site_url]][[~26]]');
                });
            });


              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 28764
              • 3 Posts
              D’oh! That works... which means that the response takes too long and the browser leaves the page before the ajax-call is completed.

              And for those of you experiencing the same problem: I solved now by adding the parameter
              async: false
              

              to the $.ajax-function. Like that the page is not left until the ajax-call is finished and the success-branch is executed.

              Thanks a lot Bruno, for showing me the right direction!