We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38357
    • 178 Posts
    Oops I was modifying the post when you responded.
    I removed the return statement and still get the error:
     [2015-06-23 05:55:07] (ERROR @ /index.php) Array
    (
    )

    when I load or refresh the page and no output. [ed. note: bobd72 last edited this post 8 years, 10 months ago.]
      • 3749
      • 24,544 Posts
      Remember that the snippet will run once when the page is first loaded and the $_POST array is empty. Then it will run again when the form is submitted. This code would capture both events:

      <?php
      if( isset($_Post['Submit'] )) {
          $_SESSION['userLogon'] = $_POST['user'];
           $modx->log(modX::LOG_LEVEL_ERROR, 'Submit is set ' . print_r($_POST, true));
      } else{
           $modx->log(modX::LOG_LEVEL_ERROR, 'Submit is not set ' . print_r($_POST, true));
      }

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 38357
        • 178 Posts
        Quote from: BobRay at Jun 23, 2015, 07:16 PM
        Remember that the snippet will run once when the page is first loaded and the $_POST array is empty. Then it will run again when the form is submitted. This code would capture both events:

        Thanks Bob,
        That appears to be a problem. With your code, when the page is loaded I get the error:
         [2015-06-23 22:09:15] (ERROR @ /index.php) Submit is not set Array
        (
        )

        However when the data is entered and the Submit button is activated there is no further error, so it would appear that the code doesn't run again when the Submit button is activated so no session is created?
          • 3749
          • 24,544 Posts
          That doesn't mean the code isn't running. It just means that 'Submit' is not an element in the $_POST array. The code *isn't* running, however, because your form's action is another page. The user is forwarded to that other page so the snippet never runs a second time.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 38357
            • 178 Posts
            Removing 'action' completely from the form and using the code:
            <?php
            if( isset($_Post['Submit'] )) {
                $_SESSION['userLogon'] = $_POST['user'];
                 $modx->log(modX::LOG_LEVEL_ERROR, 'Submit is set ' . print_r($_POST, true));
            } else{
                 $modx->log(modX::LOG_LEVEL_ERROR, 'Submit is not set ' . print_r($_POST, true));
            }


            gives in the log file:

             [2015-06-25 05:14:00] (ERROR @ /index.php) Submit is not set Array
            (
                [action] => password
                [user] => myuser
                [password] => 1234
                [Submit_x] => 141
                [Submit_y] => 20
            )
            


            so 'action' defaults to 'password' if removed?

            So how do I now redirect to the remote site with the login details if I am unable to use the 'action' parameter in the form?

              • 3749
              • 24,544 Posts
              The 'action' defaults to posting to (and reloading) the current page.

              If you post to another page, the code of your snippet will never run.

              One solution is to post to the current page, let your snippet run, then use cURL at the end of your snippet to post to the other page. http://stackoverflow.com/questions/1217824/post-to-another-page-within-a-php-script. I think you could then use $modx->sendRedirect() to go to the other page, but I'm not sure.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 38357
                • 178 Posts
                Thanks again Bob. I had a look at the curl option but it brings in the form from the other site. I am beginning to think that it just isn't possible in the short term and that I will have to put up with two login processes for now and perhaps spending the time finding a solution later.

                Thanks for your help

                  • 3749
                  • 24,544 Posts
                  It is possible with cURL to fill in the form fields on the other site and submit without the user seeing anything, though I've never done it.

                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting