We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25551 ☆ A M B ☆
    • 1,231 Posts
    Hey folks, I want to create a very very simple user registration snippet that will allow a user to sign up to a test site. All I need is a username, email and password to be passed to the user database. I would like to understand how this is done but in a simplified way. Rather than use weblogin(pe) I would like to find out how I can do this in the easiest and simplest way possible.

    Thanks!
      Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
      AugmentBLU - MODX Partner

      BLUcart - MODX Revolution E-Commerce & Shopping Cart
      • 4310
      • 2,310 Posts
      Ross, Have a look at the plain vanilla Weblogin snippet and slim it down.
        • 25551 ☆ A M B ☆
        • 1,231 Posts
        I have returned to this as I have only a few weeks to put together a simple login and management system... I am using modx as the CMF but writing my own login and management snippet to cover the required asks of the project.

        I have to create everything from scratch and so far things are going well... I just started last night and already started writing my own inline PHP code in external files and calling on them through snippets.

        I have hit a very small error with displaying a user name.... I need a little advice to solve it.

        <?php
        $output ="";
        if(isset($_SESSION['webValidated']) && isset($_SESSION['webShortname'])){
        
        echo " welcome back!" .$output = print_r($_SESSION['webShortname']);
        
        }
        else{
        	echo " show this";
        }
        ?>



        This code shows
        rossco welcome back!1

        I don’t understand why it’s displaying the name before the welcome back! text, also why is it including 1 in there? I know the one is coming from the array of the webuser but why is it being included when I only asked for the webShortname?

        Any help would be great.
          Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
          AugmentBLU - MODX Partner

          BLUcart - MODX Revolution E-Commerce & Shopping Cart
          • 26903
          • 1,336 Posts
          The print_r is giving you information about the variable, not just its value, hence the , from the book :-
          print_r() displays information about a variable in a way that’s readable by humans. If given a string, integer or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements

          Ok, can you not just use ’echo " welcome back ". $_SESSION[’webShortname’]" or ’echo $_SESSION[’webShortname’]. " welcome back!" depending on which way round you want it?

            Use MODx, or the cat gets it!
            • 25551 ☆ A M B ☆
            • 1,231 Posts
            Thanks so much! That has worked nicely.

            This is for my final year web development project so I am still learning lol. I have to create a casino with a custom login, registration and management user system. I’m using modx as I said to help me deploy the site but the signup etc is going to be written by myself as I can’t use weblogin etc.

            Thanks again, I’ll no doubt be back when I hit more snags!

            Once completed I will show you all what I’ve managed to do.
              Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
              AugmentBLU - MODX Partner

              BLUcart - MODX Revolution E-Commerce & Shopping Cart
              • 25551 ☆ A M B ☆
              • 1,231 Posts
              I have hit another snag, I need a little help.

              I have searched and read the wiki but I can’t seem to get information from an external database that is still on the same server a/c

              I have tried

              <?php
              $mydb= new DBAPI();
              $mydb->connect($host='localhost',$dbase='test', $uid='test',$pwd='test');
              $output = '';
              $result = $mydb->db->query( 'SELECT id, username, joined FROM `users` GROUP BY `id` ORDER BY id ASC' );
               
              while( $row = $mydb->db->getRow( $result ) ) {
              	$output .= '<br /> ID: ' . $row['id'] . '<br /> Name: ' . $row['username'] . '<br /> Joined: ' . $row['joined']
              	. '<br />---------<br />';
              }
              echo $output;
              ?>


              The error

              Fatal error: Call to a member function query() on a non-object in /home/xxx/public_html/manager/includes/document.parser.class.inc.php(769) : eval()’d code on line 4

              As I am learning still, could someone tell me where I am going wrong? 

              Thanks!
                Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
                AugmentBLU - MODX Partner

                BLUcart - MODX Revolution E-Commerce & Shopping Cart
                • 26903
                • 1,336 Posts
                Try $mydb->query, $mydb->getRow etc. You’re not using the $modx object so you don’t have a nested DB object named db, your just using the DBAPI class.
                  Use MODx, or the cat gets it!
                  • 25551 ☆ A M B ☆
                  • 1,231 Posts
                  Nice on buddy! That’s displaying info from an external DB.

                  Now it’s time for me to create a form to write to the DB! I’ll be back no doubt lol.

                  Thanks
                    Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
                    AugmentBLU - MODX Partner

                    BLUcart - MODX Revolution E-Commerce & Shopping Cart
                    • 25551 ☆ A M B ☆
                    • 1,231 Posts
                    OK I have spent most of the day trying to figure out how to use a simple form to send data to an external database and it’s just not happening. Completely new to this so I just need a little more help.

                    My register.php file on my server is as follows...

                    <?php
                    $conn = mysql_connect("localhost", "mydb", "pass")
                    or die("no connection" .mysql_error());
                    $db = mysql_select_db("phpwork")
                    or die("connected" .mysql_error());
                    ?>
                    <?php
                    $output ="";
                    
                    	echo "
                    <html>
                    <head>
                    <title>Form to DB</title>
                    </head>
                    <body>
                    
                    <?php
                    $id = $_POST['id'];
                    $username = $_POST['username'];
                    $password = $_POST['password'];
                    $query = "INSERT INTO table (id, username, password) VALUES ('$id', '$username', '$password')";
                    ?>
                    <p>
                    
                    <form action=\"<?php $PHP_SELF ?>\" method=\"post\">
                    Username: <input type=\"text\" name=\"username\"
                    value=\"\"><br>
                    Password: <input type=\"password\" name=\"password\"
                    value=\"\"><br>
                    id: <input type=\"text\" name=\"id\"
                    value=\"\"><br>
                    
                    <input type=\"submit\" name=\"submit\" value=\"Register\">  
                    <input type=\"reset\" value=\"Clear\">
                    </form>
                    </p>
                    
                    </body>
                    </html>
                    ";
                    
                    ?>


                    My snippet [!register!] is as follows

                    <?php
                    define(WORK_PATH, "assets/snippets/phpwork/");
                    
                    $output = "";
                    include($modx->config['base_path'].WORK_PATH.'register.php');
                    return $output;
                    ?>



                    I call it in a page using [!register!]

                    I’m trying to create a simple form in php files on my webserver in assets/snippets/phpwork

                    I want to use the files, call them in modx in a document and when you fill the form in, it’s sent to an external DB. I’m attempting to learn php and modx and I am learning but I’m hitting another snag! undecided
                      Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
                      AugmentBLU - MODX Partner

                      BLUcart - MODX Revolution E-Commerce & Shopping Cart
                      • 4041
                      • 788 Posts
                      Try this and see if it helps:

                      * Register.php *
                      <?php
                      
                      $output ="";
                      $output .="
                      <html>
                      <head>
                      <title>Form to DB</title>
                      </head>
                      <body>";
                      
                      // check if $_POST['id'] is set, if so do the db stuff
                      if(isset($_POST['id'])){
                      $conn = mysql_connect("localhost", "mydb", "pass")
                      or die("no connection" .mysql_error());
                      $db = mysql_select_db("phpwork")
                      or die("connected" .mysql_error());
                      
                      $id = $_POST['id'];
                      $username = $_POST['username'];
                      $password = $_POST['password'];
                      $query = "INSERT INTO table (id, username, password) VALUES ('$id', '$username', '$password')";
                         if($query){
                           $output .="<p>Congratulations ".$username.", the form was filled out</p>";
                         }else{
                           $output .="<p>An error occured while saving the information</p>";
                         }
                         
                      }else{
                      // show the form
                      $output .="<p>
                      <form action=\"$PHP_SELF\" method=\"post\">
                      Username: <input type=\"text\" name=\"username\"
                      value=\"\"><br>
                      Password: <input type=\"password\" name=\"password\"
                      value=\"\"><br>
                      id: <input type=\"text\" name=\"id\"
                      value=\"\"><br>
                      
                      <input type=\"submit\" name=\"submit\" value=\"Register\">  
                      <input type=\"reset\" value=\"Clear\">
                      </form>
                      </p>";
                      }
                      
                      $output .="</body>
                      </html>
                      ";
                      
                      echo $output;
                      ?>



                      [!register!] snippet:
                      <?php
                      define(WORK_PATH, "assets/snippets/phpwork/");
                      
                      include($modx->config['base_path'].WORK_PATH.'register.php');
                      
                      ?>


                      note: untested smiley
                        xforum
                        http://frsbuilders.net (under construction) forum for evolution