dimst
dimst

Reputation: 11

Joomla 3.3 AJAX call

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

Answers (1)

Craig
Craig

Reputation: 9330

  1. 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 to
    http://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

  2. 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.

  3. 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.

  4. 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.

  5. Since you're using Joomla 3.3 read about Using Joomla Ajax Interface on the Joomla Doc's site.

  6. 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

Related Questions