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