We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Hello, I have a site that has 10 different forms (yes 10!) and I would like to try and use one validation script/data capture for them all. I am trying to put everything in to an array and send that array to the DB but I don’t know how to do that.

    I was thinking something a long the lines of

    foreach($_POST as $key=>$value) //$$key=$value;
    {
    $details[] = array(
    $key => $value,
    );
    }


    but then how do I insert this in to the DB? I have the fields matching the input names.

    $insert = $modx->db->insert( $details,'capture');

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

      BLUcart - MODX Revolution E-Commerce & Shopping Cart
      • 10449
      • 956 Posts
      • Thanks ganeshXL,

        will this allow me to store `name` in the `name field , `email` in the `email` field etc? Or does it just stick all the info in one field?

        I have to keep each field separate in the DB for future use with CSVs etc.
          Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
          AugmentBLU - MODX Partner

          BLUcart - MODX Revolution E-Commerce & Shopping Cart
        • Serializing an array (either with the serialize function or converting the array to JSON) will allow you to store the whole array in one database field. As you say, you may need separate database fields depending on what you’re doing. If you ever need to query, search or sort the data, then you need separate fields. If all you ever intend to do is to export the data to a csv, then maybe storing everything in one field is ok. You can unserialize the data in php, and generate your csv rows pretty easily. This also gives you the flexibility to change your data model without changing the database.

          If you need separate db fields, and you’re up to learning xPDO and modeling each of your forms, then your form capture code can become really simple.
          $myForm = $xpdo->newObject('myForm');
          $myForm->fromArray($_POST);
          $myForm->save();
          

          http://rtfm.modx.com/display/xPDO20/Home
          (Yes, you can include xPDO and use it in Evo snippets.)

          If xPDO’s too much to learn for your immediate needs, then I’m not sure what to recommend. You need some sort of system to map your arrays to database queries. You could roll one yourself, or look for a php script out in the wild, or use xPDO.

            Mike Schell
            Lead Developer, MODX Cloud
            Email: [email protected]
            GitHub: https://github.com/netProphET/
            Twitter: @mkschell
          • Evo or Revo? In either case, you’ll need to build an INSERT INTO query out of the array. Of course, validate, sanitize and escape the $_POST values before they get anywhere near your database! There will be a few POST values that are not matched to your database table’s fields, so you would get database errors if you try to just suck up the whole $_POST like that. Not to mention leaving your database wide-open to sql injection. Evo’s would look like this:
            if(isset($_POST['submit-value'])) {
                $fields = array(
                    'field1' => $modx->db->escape($_POST['value1']),
                    'field2' => $modx->db->escape($_POST['value2']),
                    'field3' => $modx->db->escape($_POST['value3'])
                );
            
                $rs = $modx->db->insert($fields,'table-name');
            }
            
            
            
            
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
            • Nice, easy "roll your own" Susan! grin
                Mike Schell
                Lead Developer, MODX Cloud
                Email: [email protected]
                GitHub: https://github.com/netProphET/
                Twitter: @mkschell
              • Thanks for both of your replies... I’m using Evo for this particular site. I’ll have a read over your ideas and see what suits, Deadline is looming so I need to get the best solution soon! Ahhhh... grin

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

                  BLUcart - MODX Revolution E-Commerce & Shopping Cart
                • Having a look at your idea Susan, the field array is still manually set. Is there a way to make this array generate with what is actually posted?

                  So if `name`,`email` and `enquiry` is sent for 1 forum and another has `name`, `email` and `telephone` that it knows to make the array match that? I would make the DB tables match the forms so that all inputs had a field in the table.
                    Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
                    AugmentBLU - MODX Partner

                    BLUcart - MODX Revolution E-Commerce & Shopping Cart
                  • I tried this but it’s creating a multidimensional array

                    foreach (array_keys($_POST) as $key) {
                    $$key = $_POST[$key];
                    if($key != 'antispam'){
                    $fields[] = array(
                    $key =>  $modx->db->escape($_POST[$key]),
                    );
                    }
                    }


                    It won’t insert this in to the DB as it’s nothing but arrays. If I don’t use the [] to add to the array only the last field is sent to the DB.
                      Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
                      AugmentBLU - MODX Partner

                      BLUcart - MODX Revolution E-Commerce & Shopping Cart
                      • 4172
                      • 5,888 Posts
                      perhaps you can use something like this:

                      <?php
                      
                      $allowedfields='field1,field2,field3';
                      $allowedfields=explode(',',$allowedfields);
                      if(isset($_POST['submit-value'])) {
                          foreach ($allowedfields as $field){
                              if (isset($_POST[$field])){
                                  $fields[$field]=$modx->db->escape($_POST[$field]);
                              }
                          }
                          $rs = $modx->db->insert($fields,'table-name');
                      }
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!