简体   繁体   English

使用PHP函数的jQuery Ajax返回HTML代码

[英]jQuery Ajax using PHP function returns HTML code

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. 我正在使用Joomla,我打算通过代码在Joomla用户表的数据库中进行检查,以便执行注册。

Well, 3 days ago I'm trying to use an ajax function that invokes a php function. 好吧,三天前,我正在尝试使用一个调用php函数的ajax函数。 What happens is that when I try to read the response from the server (php function) can only get HTML page. 发生的是,当我尝试从服务器读取响应时(php函数)只能获取HTML页面。 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 我不确定我是否可以正确访问我的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: 这是我的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. 我在同一目录中有此teo文件。

Thanks for your help! 谢谢你的帮助!

SOLUTION: 解:

Using @jonasfh tips, I got what I need! 使用@jonasfh提示,我得到了我所需要的! I created a component with two files that he tells me and in ajax function I used: 我用他告诉我的两个文件创建了一个组件,并在我使用的ajax函数中创建了一个组件:

<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! 谢谢您的帮助!

Your request to validatePartner.php is probably being rewritten to the Joomla! 您对validatePartner.php的请求可能已被重写为Joomla! index.php. index.php。 In any case, you should create a native Joomla! 无论如何,您都应该创建一个本地的Joomla! component that can be called via the Joomla! 可以通过Joomla调用的组件! index.php, instead of creating a separate script file. index.php,而不是创建单独的脚本文件。 Your separate PHP script won't have access to the Joomla! 您单独的PHP脚本将无法访问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);
    }

});

you should probably make a component to return your ajax call as outlined by @MrCode. 您可能应该使一个组件返回@MrCode概述的ajax调用。 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: 该组件可能非常简单,但至少需要2个文件(可能还有更多文件?):一个xml定义文件和一个入口点,如下所示:

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

Syntax and format of the xml-file is described here . 此处描述xml文件的语法和格式。 The file validatepartner.php can be as simple as: 文件validatepartner.php可以很简单:

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 现在再来一招:在模板文件夹中添加以下文件: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 index.php?option = com_validatepartner#普通tmpl文件中的结果

and

index.php?option=com_validatepartner&tmpl=json # to use your json.php tmpl-file index.php?option = com_validatepartner&tmpl = json#使用您的json.php tmpl文件

Finally call the file similar to what @MrCode suggested, with small changes 最后调用文件类似于@MrCode所建议的文件,但有少量更改

$.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. 还要记住,通过转到extensions-> extension-manager然后单击Discovery在joomla后端中注册组件。

regards Jonas 问候乔纳斯

Edit: Changed layout to tmpl in the above code and examples. 编辑:在上面的代码和示例中将布局更改为tmpl。 Also changed some of the file layout etc. 还更改了一些文件布局等。

I had the same problem. 我有同样的问题。 I was working on dependent drop down lists using jQuery in joomla component. 我正在使用joomla组件中的jQuery处理依赖下拉列表。 The second drop down list as result of call display empty and loads with html page. 通话结果的第二个下拉列表显示为空,并加载html页面。 At last I found the solution: the requisite file should be placed outside the components directory. 最后,我找到了解决方案:必需的文件应该放在components目录之外。 As a result, we won't be able to get the entire html page, but at that page we have to include libraries externally. 结果,我们将无法获得整个html页面,但是在该页面上,我们必须在外部包含库。

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();   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM