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

    I have a page (id 1) and a list of items which are displayed on it (id 2, 3, 4...).
    I'd like to create one button for each item that creates a new line in the table called "test" in my database. I just created this table in PhpMyAdmin.

    The first column of the new line should contain a unique id. I can handle it in Phpmyadmin.
    The second one should contain the result i get from my snippet using $username = $modx->getLoginUserName()
    The third one should contain the id of the item i just clicked.

    Can anyone please tell me what is the bit of code that will allow me to do this ?
    Thx a lot [ed. note: youfff last edited this post 14 years, 3 months ago.]
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I presume this is Evo (1.0.x).

      Use eForm. Submitting a form gives you the $_POST to work with. You can either get the username on submission, or load it into a hidden field when generating the form.
        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
        • 40469
        • 33 Posts
        Thanks for answering, Susan. This is Evo, indeed (1.0.6).

        Basically, using eForm is what I planned to do first.
        But the problem is that I have several items on the same page (=> several eForms), and I couldn't make it work because of that.

        Fatal error: Cannot redeclare sendtodb() (previously declared in ... [ed. note: youfff last edited this post 14 years, 3 months ago.]
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          You need to wrap that function in a conditional so it won't redefine it if it's already defined.

          if (!function_exists('sendtodb')) {
              // function code here
          }
          [ed. note: sottwell last edited this post 14 years, 3 months ago.]
            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
            • 36404
            • 307 Posts
            hi,

            or put your function in a distant file and use include_once in your snippet smiley

            have swing
              réfléchir avant d'agir
              • 40469
              • 33 Posts
              So close !
              Thanks a lot. I kept Susan's solution, which actually works, but...

              I now have a trouble with my chunk "saveItem"
              <p class="error">[+validationmessage+]</p>
              <form method="post" action="[~[*id*]~]" id="saveItem[+id+]" name="saveItem">
              
                  <fieldset>
                      <label for="iditem"></label>
                      <p><input name="iditem" id="iditem" class="text" type="text" value="[+id+]"/></p>
                   
                      <p><input type="submit" name="svItem" id="cfContact" class="button" value="Save item"/></p>
                  </fieldset>
              
              </form>


              I managed to save the username in the database (yay !!) - however I can't get the id of the item in the chunk above.
              I also tried an alternative in the snippet ( $dbTable['iditem'] = $modx->documentIdentifier; ) but then it gets the id of the page - not the id of the item. [ed. note: youfff last edited this post 14 years, 3 months ago.]
                • 3749
                • 24,544 Posts
                It should be available as

                $_POST['iditem']
                  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
                  • 40469
                  • 33 Posts
                  Hi Bob,
                  Nothing's available since I didn't manage to create several forms with their own unique id. sad I need one form per item.
                  What's the trick ?
                    • 40469
                    • 33 Posts
                    After a whole day trying to do this, I'm afraid it's impossible...

                    Does anyone has another idea of how to create a button that gets the id of an item in the database ?
                      • 3749
                      • 24,544 Posts
                      How are you putting the forms on the page? Ditto? A snippet with getChunk()?

                      If it's a snippet, post it here.


                      ------------------------------------------------------------------------------------------
                      PLEASE, PLEASE specify the version of MODX you are using.
                      MODX info for everyone: http://bobsguides.com/modx.html
                        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