We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33992 ☆ A M B ☆
    • 455 Posts
    Hi,
    i want to create a html form to submit data with PHP in MODx (by using a snippet),
    i created some codes like below:
    <?php
    print '
    <form action="[~[*id*]~]" method="POST">
    link:<input type="text" name="link" />
    comment"<textarea name="comment"></textarea>
    <input type="submit" name="submit" value="GO" />
    </form>
    ';
    
    $link=$_POST['link'];
    $comment=$_POST['comment'];
    
    mysql_query("INSERT INTO modx_sand (`link`, `comment`) VALUES ('$link','$comment')") or die("Couldn't insert to sandTable:".mysql_error());
    ?>
    

    but i see different behaviors,
    sometimes it works well and create a row in database,
    sometimes it creates a row with empty columns in database,
    and sometimes (now) it goes to link of snippet (http://site-url/manager/index.php?id=18);

    please help me to create this snippet.

    thanks
    AHHP
      God loves me. 【ツ】


      MODX.ir (Persian Support)

      Boplo.ir/modx/ (Persian)
      • 3749
      • 24,544 Posts
      The code at the bottom of your form will execute every time the page is visited (sometimes, before the form has been filled out). You need to test whether the form has been filled and only execute the query when the page is hit again after the form is filled.

      Bob
        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
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        <?php
        print '
        <form action="[~[*id*]~]" method="POST">
        link:<input type="text" name="link" />
        comment"<textarea name="comment"></textarea>
        <input type="submit" name="submit" value="GO" />
        </form>
        ';
        
        if(isset($_POST['submit']) && $_POST['submit'] == 'GO') {
            $link=mysql_real_escape_string($_POST['link']);
            $comment=mysql_real_escape_string($_POST['comment']);
        
            mysql_query("INSERT INTO modx_sand (`link`, `comment`) VALUES ('$link','$comment')") or die("Couldn't insert to sandTable: ".mysql_error());
        }
        ?>

        Always, always do something like mysql_real_escape_string with any incoming values like POST values to prevent SQL injection attacks.
          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
          • 33992 ☆ A M B ☆
          • 455 Posts
          Thank you bobRay and sottwell wink
          my problem solved well grin

          I have new question about using snippet,
          i need to run a snippet(php codes) by clicking on a LINK, something like this:
          http://site-url/go.php?code=12345
          in example, i want a to send 12345 to PHP codes in go.php but i need to use it within MODx.
          how can i do it in MODx space?

          please help me!

          Thanks
          AHHP
            God loves me. 【ツ】


            MODX.ir (Persian Support)

            Boplo.ir/modx/ (Persian)
            • 33992 ☆ A M B ☆
            • 455 Posts
            Thank you,
            in my example go.php is a file which must exist in specified directory and $_GET[’’] just gets data to it.
            but is it possible that i use codes of go.php in a snippet? it means, i wanna run e.g. [[go? &code=``]] instead executing go.php script by clicking on a link.
              God loves me. 【ツ】


              MODX.ir (Persian Support)

              Boplo.ir/modx/ (Persian)
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Of course, you can put just about any php code into a snippet; that’s what they’re for!
                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
                • 33992 ☆ A M B ☆
                • 455 Posts
                I didn’t tell about my problem well, i need a solution in action. i have no complete knowledge about php coding in MODx space. i can create some scripts by php files and connect them via simple links but i can’t do same in MODx space well.

                i want to create simple LINK in my site but when it’s clicked, a php script run and some variables send to it.
                for example i put script in site-url/somdir/page.php file and create a link like this:
                <a href="http://site-url/somdir/page.php?var1=88&va2=77">LINK</a>
                you know well that when link is clicked what happening occurs.
                but i want use script (page.php in here) in MODx, create a link (like above), send variables to it and run the script.
                HOW can i do that?
                  God loves me. 【ツ】


                  MODX.ir (Persian Support)

                  Boplo.ir/modx/ (Persian)
                  • 4310
                  • 2,310 Posts
                  This is what I did.
                  Created a snippet named test with the php code in it:
                  <?php
                  $val = $_GET['value'];
                  echo ''.$val.'';
                  ?>

                  Created a landing page within MODx, docID of 18 and added this in the content area:
                  [!test!]

                  Made a web link on any other MODx page like this:
                  <a href="[~18~]&value=SomethingGood!">Test Variable</a>

                  When you click the link it takes you to page 18, the snippet executes and the content "SomethinGood!" appears.
                  Is that more like what you meant?
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    That would be better as return $val, rather than echo.
                      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
                      • 4310
                      • 2,310 Posts
                      Thanks for the correction Susan.
                      It was a quick "off the top of my head" example.