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

    I have been working off a wonderful tutorial to create forms from Bob's Guides found here: http://bobsguides.com/custom-db-tables.html. I've used it as a basis to build a simple form but have hit a roadblock. I am trying to add

    First the html:
    <form method="post" action="[[~[[*id]]]]">
    <fieldset class="loginLoginFieldset">
    <input type="hidden" name="quoteId" value="[[+id]]"/>
    
    <label for="eFirstName">First Name</label>
    <input type="text" name="eFirstName" id="eFirstName" value="[[+eFirstName]]"/>
    <label for="eMI">Middle Initial</label>
    <input type="text" name="eMI" id="eMI" value="[[+eMI]]"/>
    <label for="eLastName">Last Name</label>
    <input type="text" name="eLastName" id="eLastName" value="[[+eLastName]]"/>
    <label for="eCity">City</label>
    <input type="text" name="eCity" id="eCity" value="[[+eCity]]"/>
    
    <label for="bowlgames">Please check which items you are interested in?</label>
    <input type="checkbox" name="item_chk[]" value="value1" />Value 1<br />
    <input type="checkbox" name="item_chk[]" value="value2" />Value 2<br />
    <input type="checkbox" name="item_chk[]" value="value3" />Value 3<br />
    
    <input type="submit" name="submit" id="submit" value="submit"/>
    </fieldset>
    </form>


    Now my PHP:
    <?php
    /* CreateQuote Snippet */
    
    if (isset($_POST['submit']) &&
        ($_POST['submit'] == 'submit')) {
        /* Form has been submitted */
     
        /* You might want to check for empty
         * required fields here */
     
        /* We need to load our class so newObject() will work */
        $path = MODX_CORE_PATH . 'components/online_app/';
        $result = $modx->addPackage('online_app',$path .
            'model/','xxx_');
        if (! $result) {
            return 'Failed to add package';
        }
    
        $fields = array(
            'eFirstName' => $_POST['eFirstName'],
            'eMI' => $_POST['eMI'],
            'eLastName' => $_POST['eLastName'],
    	'eCity' => $_POST['eCity'],
    );
    
    $online_app = $modx->newObject('application', $fields);
        if ( $onlineapp->save()) {
            $output = "Fields Inputted";
        } else {
            $output = "Error";
        }
     
    } else {
        /* Not a repost, just display the form */
        $output = $modx->getChunk('online_appTpl');
    }
    return $output;



    What do I place in the PHP that will collect the selections for the "item_chk". I have come across many variations online but the best that ever happens, when I check any of the items on my form is the word "array" is posted in that field in the database. I'm at a complete loss for something fairly simple.

    Thank you in advance for any help.
      • 3749
      • 24,544 Posts
      $_POST['item_chk'] will always be an array. You can store a string or boolean PHP value directly in the DB, but not an array -- you have to convert it first. How you do that will depend on how you want to store it and the DB type of that field.

      Here are a couple of options:

      $val = implode(',', $_POST['item_chk']);
      


      or

      $val = $modx->toJSON(',', $_POST['item_chk']);


      Then this:

      $fields['item_chk'] = $val;


      You also have the option to give each checkbox a different name and create the stored version yourself in the code.

      When you get the data from the DB, you may also have to convert it back to a PHP array with
      explode(',', $value);
      or
      $modx->fromJSON($value);


        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
        • 47711
        • 3 Posts
        Fantastic! That worked perfectly. I appreciate your time and help.
          • 3749
          • 24,544 Posts
          I'm glad I could help. smiley
            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
            • 51242
            • 3 Posts
            Can anyone tell me how to store values from multiple checkboxes in a separate rows in db?

            Example:
            <form>
            <input type='hidden' name='user_id' value='1'/>
            <input type='checkbox' name='user_choice[]' value='10'/>
            <input type='checkbox' name='user_choice[]' value='20'/>
            <input type='checkbox' name='user_choice[]' value='30'/>
            </form>

            so that I have 3 rows (in case each checkbox is checked) in my database ('1','10'), ('1', '20'), ('1','30') instead of a single row with string inside 'user_choice' column.