We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4971
    • 964 Posts
    I am trying to use a little AJAX to add a feed to a resource in a
    custom DB table. But it is not working...

    This is the HTML in a resource, just a link to add a feed. A form asking the name and URL of
    the feed will be displayed using AJAX:

    <a class="add-feed" href="javascript:;">Add Feed</a>


    This is the AJAX code. This works fine, it goes to URL, which in this case
    is a MODx Resource with a blank template and MIME = application/x-httpd-php.
    This works OK, the form appears (as you can see in the attached image):

    <script type="text/javascript">
    $(document).ready(function () {
    
    	$("a.add-feed").click(function(){
    		$("#loader").fadeIn();
    		$.ajax({
    			type: "POST",
    			data: "action=show_form",
    			url: "[[!++site_url]][[~23]]",
    			success: function(msg){
    				$("#view-area").html(msg);
    				$("#loader").fadeOut();
    			}
    		});
    	});
    
    });
    </script>
    


    This is the add-form code in resource 23, this displays the form,
    but I think it has a problem with $modx not being defined or something,
    cause it spits out the code after it...
    this is where I need help!

    <?php
    if(!$_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
    	die("No direct access to files is allowed");
    }
    
    $action = trim($_POST["action"]);
    
    if($action == "show_form") {
    ?>
    	<form action='' id='add_form'>
    		<label for='feed_name'>Name:</label>
    		<input type='text' id='feed_name' name='feed_name'/>
    
    		<label for='feed_url'>Feed URL:</label>
    		<input type='text' id='feed_url' name='feed_url'/>
    
    		<input type='submit' value='add' class='submit-button'/>
    	</form>
    <?php
    } elseif($action == "submit") {
            $output = '';
    	$feed_name = filter_var($_POST["feed_name"], FILTER_SANITIZE_STRING);
    	$feed_url = filter_var($_POST["feed_url"], FILTER_SANITIZE_STRING);
    	if(empty($feed_name) || empty($feed_url)) {
    		$output .= 'You have not filled up all the fields!';
    	} else {
                    define('MODX_CORE_PATH', 'http://www.my_domain.com/core/');
                    $path = MODX_CORE_PATH . 'components/reader/';
                    $result = $modx->addPackage('reader',$path . 'model/','myd_');
    		$addfeed = $modx->newObject('reader');  
    		$addfeed->fromArray(array(  
    			'name' => $feed_name,
    			'url' => $feed_url,
    		));  
    		$addfeed->save();
    		$output .= 'Feed is added. Please reload the page.';
    	}
            return $output;
    }
    ?>
    


    TIA


      Website: www.mercologia.com
      MODX Revo Tutorials:  www.modxperience.com

      MODX Professional Partner
      • 4971
      • 964 Posts
      Anyone can help with this?
        Website: www.mercologia.com
        MODX Revo Tutorials:  www.modxperience.com

        MODX Professional Partner
        • 6228
        • 249 Posts
        I believe you need to specify a path to your package, and not a URL.

        Why the PHP parser is terminating is puzzling. It’s possible that you are causing an error by re-defining MODX_CORE_PATH. Try specifying the package path directly in the method call, i.e.

        	...
        	if(empty($feed_name) || empty($feed_url)) {
        		$output .= 'You have not filled up all the fields!';
        	} else {
                        $result = $modx->addPackage('reader', '/absolute/path/to/core/components/reader/','myd_');
        		$addfeed = $modx->newObject('reader');  
        		...
        
          lo9on.com

          MODx Evolution/Revolution | Remote Desktop Training | Development
          • 4971
          • 964 Posts
          Thanks for the response cyclissmo...

          I also was in doubt about the URL, but it works fine, as you can see it
          goes and renders the form perfectly, the problem is the PHP parser stopping.
          I even tried to use a snippet instead of the URL of the add.php but it
          does not work...

          I will try your suggestion of the path... at first I thought that the parser
          was stopping because I had not defined the path, as I think the add.php file could
          be considered to be outside of MODx, but it still breaks.

          Let see what happens with your idea...
            Website: www.mercologia.com
            MODX Revo Tutorials:  www.modxperience.com

            MODX Professional Partner
            • 4971
            • 964 Posts
            Nope, it does not work with the absolute path...

            Any other help out there?
            TIA
              Website: www.mercologia.com
              MODX Revo Tutorials:  www.modxperience.com

              MODX Professional Partner
              • 4172
              • 5,888 Posts
              Hi charliez,

              I’m not sure what you are trying todo.
              Seems to me you have that code directly in your content.
              But you need to put it into a snippet and call that snippet on your page 23.

              Then: it will load that form to your page and if you hit submit it will do a normal submit on your page where you are.

              If you want to send your formdata per ajax you need to apply function, which does that, to your submit-button after loading the form.
              On success remove the form and show a success-message.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 22629
                • 194 Posts
                Hi charliez,

                Like Bruno17 asks - is this code in the resource directly, or in a snippet which is included in the resource?

                Also just a tip - to generate an absolute URL, instead of writing:

                [[!++site_url]][[~23]]


                you can write:

                [[~23? &scheme=`full`]]
                  Andy Shellam | www.networkmail.eu | @Pandy06269 @NetworkMail

                  modx Revolution 2.2.6
                  Windows 2012 | IIS 8 | php 5.4.11 | MySQL 5.5.29

                  Content-Managed Websites Built on MODX
                  • 4971
                  • 964 Posts
                  @Pandy06269: thank you for the tip, even thought both constructs work perfectly. Mine is more Evo style and yours is more Revo style... I will change to yours in the future...


                  @bruno17: thanks for the tip, yes, the code was in the resource and was not going thru the MODx parser I guess, now, it is and the code is almost there, I am just having small form problems passing the data, but it is Ok now... thanks...
                    Website: www.mercologia.com
                    MODX Revo Tutorials:  www.modxperience.com

                    MODX Professional Partner