We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25226
    • 29 Posts
    Ok, completely new at php and modx and cannot figure out how to call a php snippet to post form data to mysql. The way the code was originally developed, it called an external .php file to do the post. But in modx, it wants me to call a snippet. Thats where its breaking, the [[snippet]] is not taking the place of the url path to the old php file very well. Any help on this mediocre task would be great. Or if there is an easier way of posting a form field to mysql that would be even better. Thanks in advance! If possible to post sample code, it will help me wrap my head around this.

    Here is my Resource Page
    <head>
    [[$drop]]
    <style>
    
    
    h1 {
    	padding: .2em;
    	margin: 0;
    }
    textarea {
    	overflow: hidden;
    	height: auto;
    	width: 90%;
    }
    </style>
    </head>
    <body>
    <form method="post" action=[b]"normally put php file name here[/b]">
       
       <div id="article">
    		<label for="article"></label>
    		<textarea rows="5" cols="35" name="article" id="article"></textarea>
    		<input type="submit" name="submit" id="submit" value="Submit" />
    	</div>
    </form>
    </body>
    


    Here is my Chunk called [[drop]]
    <script src="assets/js/controls/jquery-1.4.4.js"></script>
    
     <script type="text/javascript">
          $(function() {
          $('#submit').click(function() {
          $('#article').append('<img src="img/loading.gif" alt="Currently Loading" id="loading" />');
          
            var article = $('#article').val();
            
            $.ajax({
              url: '[b]normally put php file path here[/b]',
              type: 'POST',
              data: 'article=' + article,
              
              success: function(result) {
                $('#response').remove();
                $('#article').append('<p id="response">' + result + '</p>');
                $('#loading').fadeOut(500, function() {
                  $(this).remove();
                });
                
              }
            });
            
            return false;
          });
          
          });
        </script>
    
    
    [[$[[+customScriptsChunk]]]]
    [[javascript_includes]]
    [[customJQuery]]
    
    


    and
    here is my Snippet [[submit_article]]
    <?php
    $conn = new mysqli('server', 'user', 'password', 'database');
    $query = "INSERT into table_name(article) VALUES (?)";
    
    $stmt = $conn->stmt_init();
    if($stmt->prepare($query)) {
      $stmt->bind_param('s', $_POST['article']);
      $stmt->execute();
    }
    
    if($stmt) {
      echo "Thank you. We'll be in touch with you shortly!";
    } else {
      echo "There was a problem. Please  try again later.";