Reputation: 11
The following code is in my view/tmpl/edit.php:
<script type="text/javascript">
jQuery("#button").click(function(){
jQuery.ajax({
url:'somefile.php',
type:'POST',
data: {action:"1"},
success: function(response) {alert(response);}
});
});
</script>
and in the same folder i have the somefile.php with the following code:
if (isset($_POST['action'])) {
SomeFunction();
}
function SomeFunction()
{
...do something
}
My problem is that when I hit the button it never access the somefile.php although the script is executed (checked with alerts). What am I doing wrong here?
Please show me some directions on that with some relevant sample/example code.
Thanks.
Upvotes: 0
Views: 1036
Reputation: 9330
If you're calling 'somefile.php' as your URL that will equate to the current URL you're browsing in Joomla + the file name. It will not map to the path of your file.
e.g. if you're on the front page of the site i.e. http://example.com
the AJAX request will go tohttp://example.com/somefile.php
while on a page at http://example.com/blog/mypost01
it will map to http://example.com/blog/somefile.php
.
Both of these will be wrong as your file is actually somewhere like:
http://example.com/components/com_mycomponent/view/tmpl/somefile.php
First up I would use a proxy like Charles to monitor the full request
and response
, cycle this should give you a clear idea of whats going on and the entire request/redirect/response cycle.
Make sure you have Error Reporting
turned all the way up to Development
and Joomla DEBUG mode enabled, that way you may see errors rather than a 200
result being returned.
Some Joomla 3 htaccess
configurations and security extensions (AdminTools) will prevent direct access to php
files. If this is the problem, it's probably a sign that you're doing it in an unsafe/incorrect way.
Since you're using Joomla 3.3 read about Using Joomla Ajax Interface on the Joomla Doc's site.
Finally, as you appear to be building your own component, you could/should be passing the AJAX call via your component and to a method in a suitable controller rather than accessing a PHP file directly.
Upvotes: 1