We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14138
    • 13 Posts
    Hiya guys, this is my first post and first MODx contribution so be nice!

    Snippet: db2form
    Author: Andy Callaghan
    Website: http://www.microplop.com


    Description:
    The snippet that I have made will take a named MySQL table and automatically produce an XHTML standards compliant form to view or insert data in that table.


    Parameters:

    Required parameters:

    &table -required
    set the full table name that you wish to use in the html form

    &type -required -defaults to ’select’
    options:
    &type=’select’ -- view and edit form (SELECT query)
    &type=’insert’ -- create new record form (INSERT query)
    set the type of form which is diplayed

    &redirect -required -defaults to current page
    Accepts the document ID of the page you wish to redirect to.
    After an insert query has been successful, redirect to this page.



    Optional parameters:

    &title -defaults to modx current page title
    display h1 text above your html form

    &error_warn -defaults to ’warn’
    the css class for a html p tag displaying validation errors

    &success_msg -defaults to ’A new record has been added to $table’
    on a successful insert, this message will display at the top of the form


    Usage:
    [!db2form? &table=`modx_manager_users`!]
    [!db2form? &table=`modx_manager_users` &type=`select` &title=`Users in MODx`!]
    [!db2form? &table=`modx_manager_users` &type=`insert` &title=`Add users to MODx`!]
    [!db2form? &table=`invoices` &title=`Make an invoice` &redirect=`1` &error_warn=`invalid` &success_msg=`An invoice has been added`!]
    


    Benefits of my snippet:

    • Completely user input entry safe -- no MySQL attacks
    • No need to code form after form of repeating code
    • Any changes to the core code for security exploits etc... needs to be done in one file only
    • Simple validation checks.(If MySQL states that field is NOT NULL, the field cannot be left blank)


    To do:

    • Extensively test script for bugs and errors
    • Add DELETE and UPDATE form functions
    • Add pagination to SELECT form
    • Add WHERE function to filter SELECT queries
    • Change &success_msg into a chunk
    • Integrate with eForm

    I need your help to do all of these above, so I’d be really grateful if you could download my script and test it on your installation!

    Thanks alot!
      • 7231
      • 4,205 Posts
      This looks like a great snippet idea, nice contribution. I will give it a go as soon as I get some time.
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don't know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 10487 MODX Staff
        • 1,535 Posts
        Garry Nutting Reply #3, 19 years ago
        Nice first post smiley I’ll second dev_cw’s comments ... looks really promising. Keep up the good work!
          Garry Nutting
          Senior Developer
          MODX, LLC

          Email: [email protected]
          Twitter: @garryn
          Web: modx.com
          • 16085
          • 203 Posts
          knightknight Reply #4, 19 years ago
          nice work andy! I know you’re be developing more for this excellent project in the future!! smiley

            • 1535
            • 84 Posts
            Nice snippet!

            This snippet requires PHP 5, however.

            Fatal error: Call to undefined function: stripos() in /Library/WebServer/Documents/modx/assets/snippets/db2form/db2form_instantform.php on line 56


            If you have PHP 4, you can insert this working substitute near the top of db2form_instantform.php

            function stripos($haystack, $needle){
                return strpos($haystack, stristr( $haystack, $needle ));
            }


            (found it on php.net)

            Cheers,

            Andreas
              • 14138
              • 13 Posts
              Thanks everyone for the kind comments.

              AndreasT: The code is now in the 0.2 version of the snippet, has been tested and works without PHP5.

              Code inserted:
              if (PHP_VERSION < 5) {
                function stripos($haystack, $needle){
                    return strpos($haystack, stristr( $haystack, $needle ));
                }
              }
              


              I am also getting more MySQL field types to be recognised by my script. For instance:

              For the ’enum’ type, my script sets up a <select></select> field and uses the php explode function to use the enum values as option values.
              if (stripos($row["Type"],"enum") !== false) {
                  $this_row["Type"] = "enum";
                  $enum_remove = array('enum','(',')','\'');
                  $this_row["Options"] = explode(',',str_replace($enum_remove,"",$row["Type"]));
                } 


              and then when printing the html...

                  case "enum":
                    $output .=  "<p class=\"$error_class\"><select name=\"{$table}[{$field["Field"]}]\">";
              	foreach ($field["Options"] as $value) {
              	$output .= "<option>" . $value ."</option>";
              	}
                    $output .= "</select> $error_text</p>\n";
                    break;


              I am also currently half way through implementing a WHERE clause function to the SELECT forms, using template variables.

              Thanks, Andy.
                • 1535
                • 84 Posts
                I like this more and more, and will surely find it very useful.

                This does not display, in fact it times out after 30 secs.

                [!db2form? &table=`modx_site_snippets` &type=`select` &title=`Site Snippets`!]

                Cheers,

                Andreas
                  • 37550 ☆ A M B ☆
                  • 711 Posts
                  onesmarthost Reply #8, 19 years ago
                  Looks great I will try it out soon.

                  :D
                    http://www.onesmarthost.co.uk
                    UK MODX Hosting with love.
                    • 14138
                    • 13 Posts
                    AndreasT:

                    With the snippet call:
                    [!db2form? &table=`modx_site_snippets` &type=`select` &title=`Site Snippets` &per_page=`5`!]


                    The &per_page variable is a parameter that I am working on in version 0.2, for pagination.

                    I get a successfully loaded page:
                    <!-- 9 MySQL queries took 0.0313 seconds. Document parsing from the database took 0.5200 seconds, totalling 0.5513 seconds in total -->


                    It could be something I have changed in the latest version, or a problem with an MySQL query taking too long.

                    I’ll look into though, thanks for the feedback!

                    Has anyone tried it yet?
                      • 1535
                      • 84 Posts
                      Have you submitted the 0.2 version anywhere or are you still working on it?

                      Cheers,

                      Andreas