jQuery Ajax using PHP function returns HTML code

2019-08-17 06:46发布

I'm using Joomla, and I intend to be checked in a database of Joomla user table is right there through a code in order to perform the registration.

Well, 3 days ago I'm trying to use an ajax function that invokes a php function. What happens is that when I try to read the response from the server (php function) can only get HTML page. Not sure what is happening. I saw several articles and questions and still not know what's going on.

What I'm doing is when the writing is detected in a given field, performs the function to check if the code exists in the database

I am unsure if I am correctly access to my validatePartner.php

Here is my script:

<script language="javascript" type="text/javascript">
jQuery(document).ready(
    function($){
        $('#jform_username').bind('input', function() {
            alert("FINE");

            var data =" hello world";
            var data2=" hello all";
            $.ajax({
                url: 'validatePartner.php',
                data: {'q': data,'z':data2},
                type: "POST",
                success: function(data) {
                    alert("Here: " + data);
                }

            });
        });
    });
</script>

Here is my validatePartner.php:

<?php
 function myfunction() {
    $myvar = $_POST['q']." how are you?";
    $myvar2 = $_POST['z'];
    echo $myvar."\n".$myvar2;

 }

myfunction();
?>

I have this teo files in the same directory.

Thanks for your help!

SOLUTION:

Using @jonasfh tips, I got what I need! I created a component with two files that he tells me and in ajax function I used:

<script language="javascript" type="text/javascript">
. . .
$.ajax({
    url: '?option=com_validatepartner&format=raw',
    . . .
</script>

Instead:

<script language="javascript" type="text/javascript">
. . .
$.ajax({
    url: '?option=com_validatepartner&tmpl=json',
    . . .
</script>

Thanks for the help!

3条回答
【Aperson】
2楼-- · 2019-08-17 06:59

you should probably make a component to return your ajax call as outlined by @MrCode. The component can be super simple, but needs at least 2 files(possibly some more?): an xml definition file, and the entry point, like this:

/administrator/components/com_validatepartner/validatepartner.xml and /components/com_validatepartner/validatepartner.php

Syntax and format of the xml-file is described here. The file validatepartner.php can be as simple as:

defined('_JEXEC') or die;
#get a database object
$db=JFactory::getDbo(); 
$db->setQuery('select * from #__thetable'); 
$list=$db->loadAssocList(); 
# you probably want to return some json-data or something: 
echo json_encode($list);

Now one more trick: In your template folder add the following file: json.php

containing:

defined('_JEXEC') or die;
<jdoc:include type="component" />

Now you can test your component by going to:

index.php?option=com_validatepartner #result inside the normal tmpl-file

and

index.php?option=com_validatepartner&tmpl=json # to use your json.php tmpl-file

Finally call the file similar to what @MrCode suggested, with small changes

$.ajax({
    url: 'index.php?option=com_validatepartner&tmpl=json',
    data: {'q': data,'z':data2},
    type: "POST",
    success: function(data) {
        alert("Here: " + data);
    }

});

Also remember to register the component in the joomla backend, by going to extensions->extension-manager and then Discovery.

regards Jonas

Edit: Changed layout to tmpl in the above code and examples. Also changed some of the file layout etc.

查看更多
仙女界的扛把子
3楼-- · 2019-08-17 07:02

I had the same problem. I was working on dependent drop down lists using jQuery in joomla component. The second drop down list as result of call display empty and loads with html page. At last I found the solution: the requisite file should be placed outside the components directory. As a result, we won't be able to get the entire html page, but at that page we have to include libraries externally.

Here are the libraries:

define( '_JEXEC', 1 );    
define('JPATH_BASE', dirname(__FILE__));   
define( 'DS', DIRECTORY_SEPARATOR );   
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );   
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );  
$mainframe =& JFactory::getApplication('site');  
$mainframe->initialise();   
查看更多
Summer. ? 凉城
4楼-- · 2019-08-17 07:18

Your request to validatePartner.php is probably being rewritten to the Joomla! index.php. In any case, you should create a native Joomla! component that can be called via the Joomla! index.php, instead of creating a separate script file. Your separate PHP script won't have access to the Joomla! database or session etc.

It would then be called like:

$.ajax({
    url: 'index.php?option=com_myComponent&task=validatePartner',
    data: {'q': data,'z':data2},
    type: "POST",
    success: function(data) {
        alert("Here: " + data);
    }

});
查看更多
登录 后发表回答