We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10405
    • 288 Posts
    Hi, I'm setting up a way for users on a site to have a product they like "remembered" when they log in so that it gets added to their wishlist even if they're not logged in to begin with. At the moment if they click the "Add" button on the product page they get redirected to the Login page if they're not logged in (which is correct) but then after they log in they go to the restricted landing page and the product they wanted to add isn't added- so they have to go and add it "again".

    So I have a form attached to the Add button, in a snippet as follows:

    if ($modx->user->hasSessionContext($modx->context->get('key'))) { //If the user is logged in
       $product = $_SERVER['REQUEST_URI'];
       $product2 = trim($product, '/');
       $product3 = rtrim($product2, '.html');
       $dateadded = date('Y-m-d');
       echo '<form action="spec-notepad.html" method="post" name="addproduct">';
       echo '<input name="_Username" class="formfield" type="hidden" value='.$username.' />';
       echo '<input name="_Product" class="formfield" type="hidden" value='.$product3.' />';
       echo '<input name="_DateAdded" class="formfield" type="hidden" value='.$dateadded.' />';
       echo '<input name="addproduct" class="formfield" type="submit" value="NP" />';
       echo '</form>';
    }  
    
    else { //User not logged in, so go to login page
       echo '<form action="login.html" method="post" name="login">';
       echo '<input name="_Product" class="formfield" type="hidden" value='.$product3.' />';
       echo '<input name="_NPImage" class="formfield" type="submit" value="NP" />';
       echo '</form>';
    }
    


    I want that hidden input (which holds the product code) to be carried through to the Login snippet somehow and remembered when the user reached the landing page.

    My Login snippet is:

    [[!Login? &loginTpl=`lgnLoginTpl` &logoutTpl=`lgnLogoutTpl` &errTpl=`lgnErrTpl` &loginResourceId=`342` &logoutResourceId=`343`]]
    


    Any idea what I can do to get this data tagged onto the login process? Thanks.
      • 17403 ☆ A M B ☆
      • 183 Posts
      Hi,

      Do you use any add-on or extra for handling user authentication?

        zaenal.lokamaya
        • 3749
        • 24,544 Posts
        How about setting a $_SESSION variable with the product code? [ed. note: BobRay last edited this post 10 years, 8 months ago.]
          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
          • 10405
          • 288 Posts
          Thanks, but how would I do that? Would I add another parameter to the Login snippet call?

          Also, the only add-on I'm using for user authentication is Login (and Register, elsewhere)
            • 3749
            • 24,544 Posts
            No, I was thinking of something like this (untested):

            if ($modx->user->hasSessionContext($modx->context->get('key'))) { //If the user is logged in
               if (isset($_SESSION['product'])) {
                   $product3 = $_SESSION['product'];
               } else {
                   $product = $_SERVER['REQUEST_URI'];
                   $product2 = trim($product, '/');
                   $product3 = rtrim($product2, '.html');
               }
               $dateadded = date('Y-m-d');
               echo '<form action="spec-notepad.html" method="post" name="addproduct">';
               echo '<input name="_Username" class="formfield" type="hidden" value='.$username.' />';
               echo '<input name="_Product" class="formfield" type="hidden" value='.$product3.' />';
               echo '<input name="_DateAdded" class="formfield" type="hidden" value='.$dateadded.' />';
               echo '<input name="addproduct" class="formfield" type="submit" value="NP" />';
               echo '</form>';
            }  
             
            else { //User not logged in, so go to login page
               echo '<form action="login.html" method="post" name="login">';
               echo '<input name="_Product" class="formfield" type="hidden" value='.$product3.' />';
               $_SESSION['product'] = $product3;
               echo '<input name="_NPImage" class="formfield" type="submit" value="NP" />';
               echo '</form>';
            }
              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
              • 10405
              • 288 Posts
              Hmm, doesn't seem to do anything for me- I'm using a slightly modified version so that the product code gets grabbed from the URL when the user isn't logged in:

              <?php
              if ($modx->user->hasSessionContext($modx->context->get('key'))) { //If the user is logged in
                 if (isset($_SESSION['product'])) {
                     $product3 = $_SESSION['product'];
                 }
              else {
                 $product = $_SERVER['REQUEST_URI'];
                 $product2 = trim($product, '/');
                 $product3 = rtrim($product2, '.html');
              }
                 $dateadded = date('Y-m-d');
                 echo '<form action="spec-notepad.html" method="post" name="addproduct">';
                 echo '<input name="_Username" class="formfield" type="hidden" value='.$username.' />';
                 echo '<input name="_Product" class="formfield" type="hidden" value='.$product3.' />';
                 echo '<input name="_DateAdded" class="formfield" type="hidden" value='.$dateadded.' />';
                 echo '<input name="addproduct" class="formfield" type="submit" value="NP" />';
                 echo '</form>';
              }  
              
              else { //User not logged in, so go to login page
                 $product = $_SERVER['REQUEST_URI'];
                 $product2 = trim($product, '/');
                 $product3 = rtrim($product2, '.html');
                 echo '<form action="login.html" method="post" name="login">';
                 echo '<input name="_Product" class="formfield" type="hidden" value='.$product3.' />';
                 $_SESSION['product'] = $product3;
                 echo '<input name="_NPImage" class="formfield" type="submit" value="NP" />';
                 echo '</form>';
              }
              


              And the snippet after the login, which then inputs into the db. I can see this doesn't work for the user who wasn't logged in, because the addproduct form isn't sent- instead their data comes from the login snippet. But I can't see how to modify this code to account for when the user wasn't already logged in and they come fresh from the login page:

              if ($_POST['addproduct']) {
              
              if($mysqli->connect_errno > 0){
                  die('Unable to connect to database [' . $mysqli->connect_error . ']');
              }
                $username = $mysqli->real_escape_string($_POST['_Username']);
                if ($_POST['_Product']) { $product = $mysqli->real_escape_string($_POST['_Product']); }
                else { $product = $_SESSION['product']; }
                $dateadded = $mysqli->real_escape_string($_POST['_DateAdded']);
                
                $addproduct = $mysqli->query("INSERT INTO specnotepadproducts (Username, Product, DateAdded) VALUES ('$username','$product','$dateadded')"); 
              
              if ($addproduct) {  
              		echo '<p>Product <strong>'.$product.'</strong> added successfully.</p>';
              		}
              }
              

                • 3749
                • 24,544 Posts
                I didn't realize that you were saving the data to the database.

                How about putting a snippet on the Login page that writes the data to the DB if $_SESSION['product'] is set.

                So, instead of this:

                if ($_POST['addproduct']) {


                this:

                if (isset($_SESSION['product'])) {


                That way, you don't have to do anything on the landing page except consult the DB.

                Once it's working, you may want to modify the database-saving code to make sure the product isn't there already.
                  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