Search
Other Sourcerer questions
Forum

Problem running server side php code from a javascript call

Robin Hair's Avatar Robin Hair
I'm using the following Javascript in Sourcerer, to refresh a DropDown form control, by running some PHP code on the server:

<script type="text/javaScript">
function FetchState(id){
jQuery('#state').html('<option></option>');
jQuery('#city').html('<option>Select City</option>');
jQuery.ajax({
type:'post',
url: 'phpcode2/ajaxdata.php',
data: { country_id : id},
success: function(data){
jQuery('#state').html(data);
}
})
}

My ajaxdata.php code should return 'State' data according to the selected country_id. However, it won't let me execute any database code and continually returns 500 (Internal Server Error). [This isn't a syntax problem as I have tested the code externally and even broken it down to just a single line of code!].

My questions are:
1) Is the above use of javascript linking server side php code a limitation of Sourcerer?
2) Is my javascript URL the correct way to run the .php code?
3) My ajaxdata.php code is just a file on the server, does it need to be 'packaged' within a Joomla Module or PlugIn for the database calls to work?

Thanks
Robin Hair
Peter van Westen's Avatar Peter van Westen ADMIN
There are no limitations in Sourcerer for your javascript code.
Sourcerer simply makes it easy for you to place that javascript into your HTML output.

How and if your javascript code works - also in combination with your template - is completely outside the control and responsibility of Sourcerer.

Check the HTML output (view source) of your page to see if the javascript is there as you want it.
If so, the Sourcerer is doing what you tell it to.
Please post a rating at the Joomla! Extensions Directory
Robin Hair's Avatar Robin Hair
Thanks for your timely reply, and clarification about how Sourcerer works. I know it isn't strictly a Sourcerer question, but would you kindly be able to comment on my question 3:

3) My ajaxdata.php code is just a file on the server, does it need to be 'packaged' within a Joomla Module or PlugIn for the database calls to work?

It almost seems like Joomla is protecting itself from me trying to run privileged code outside of a Module or Plugin?

Thanks again,
Robin
Peter van Westen's Avatar Peter van Westen ADMIN
Also nothing to do with Sourcerer.

That all depends on the custom php code in that PHP file.
If you can run that file without issues directly in the browser, then it should technically also run via an ajax call.
If it doesn't, you will need to do debugging, and see why that happens.

I recommend you ask for further help on the Joomla forum:
forum.joomla.org
Please post a rating at the Joomla! Extensions Directory
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription