Reputation: 10137
I have the following functions. One is supposed to receive an id, which is then supposed to be transmitted via AJAX, by way of a POST request.
Yet, while I know the "initiateSubCategories" function is working, for whatever reason the ajax function refuses to work. It's supposed to send the data to the very same file it's called in, and I have a feeling that this may be the issue.
Is there anything I'm doing wrong here? I've tried validating my JavaScript via jslint, but all that has given me is some nitty gritty complaints about my code format (which is not the issue here, but will be after I've fixed this).
Here's my code:
function sendAjaxDataViaPost(pathToFile, ajaxData) {
$.post(pathToFile, function(ajaxData) {
alert("Data loaded: " + ajaxData);
$('#pageOne').append('<pre>' + ajaxData '</pre>');
});
}
function initiateSubCategories(parent) {
//$("#pageOne").append("<pre>" + parent + "</pre>");
sendAjaxDataViaPost('form.php', parent);
<?php
$categories = $this->formData->getCategories($_POST['value']);
?>
printSubCategories(<?=$categories;?>);
}
So, what is it that I'm doing wrong? I've tried this in both Firefox and Chrome, and neither have been able to produce anything.
Update
I changed the function to use the following format:
$.post(pathToFile, { id: ajaxData }, function(ajaxData) {
alert("Data loaded: " + ajaxData);
$('#pageOne').append('<pre>' + ajaxData '</pre>');
});
yet, when I try to land a $_POST['id']
, it tells me that it's not referenced.
???
Update 2
I just found in FireBug's console that my "initiateSubCategories" function is undefined. What I'd like to know is how this is possible...
I'll post my entire code - a second pair of eyes may really help on this one.
<?php
$target = JPATH_BASE.DS."index.php?action=com_adsmanager&task=save&Itemid=".$this->itemId;
$saveTarget = JPATH_BASE.DS."index.php?action=com_adsmanager&task=Itemid=".$this->itemId;
function populateCategories($parent, FormData $formData) {
$categories = $formData->getCategories($parent);
foreach($categories as $section => $row){
?>
<option value=<?=$row['id'];
?>>
<?php
echo $row['name'];
?>
</option>
<?php
}
}
?>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js">
</script>
<script type="text/javascript">
function sendAjaxDataViaPost(pathToFile, ajaxData) {
$.post(pathToFile, {'id' : ajaxData}, function(ajaxData) {
alert("Data loaded: " + ajaxData);
$('#pageOne').append('<pre>' + ajaxData '</pre>');
});
}
function initiateSubCategories(parent) {
sendAjaxDataViaPost('form.php', parent);
$("#pageOne").apppend('<pre>' + <?=$_POST;?> + '</pre>'));
}
function printSubCategories(categories) {
for (var i = 0; i < categories.length; i++) {
var opt = document.createElement("option");
opt.text = categories['name'];
opt.value = categories['id'];
$("#subcategories").append(opt);
}
}
</script>
<div id="pageOne">
<form action=<?=$saveTarget;?> method="post" enctype="multipart/form-data">
<select name="category" id="categories" class="ads_categories" onclick="initiateSubCategories(this.value);">
<option value="0">--Please choose a category--</option>
<?php populateCategories(0, $this->formData); ?>
</select>
<select name="category" id="subcategories" class="ads_subcategories">
<option value="0">--Please choose a subcategory--</option>
</select>
</form>
</div>
Upvotes: 0
Views: 2406
Reputation: 719
Try changing your post call to
$.post(pathToFile, { 'id': ajaxData }, function(ajaxData) {
alert("Data loaded: " + ajaxData);
$('#pageOne').append('<pre>' + ajaxData '</pre>');
});
I am assuming that you are sending some value through ajaxData before you call
function sendAjaxDataViaPost(pathToFile, ajaxData) {
Upvotes: 1