jQuery Ajax使用PHP函数返回HTML代码 [英] jQuery Ajax using PHP function returns HTML code
问题描述
我正在使用Joomla,我打算通过代码在Joomla用户表的数据库中进行检查,以执行注册.
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.
好吧,三天前,我正在尝试使用一个调用php函数的ajax函数. 发生的是,当我尝试从服务器读取响应时(php函数)只能获取HTML页面.不知道发生了什么.我看到了几篇文章和问题,但仍然不知道发生了什么.
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
我不确定我是否可以正确访问我的validatePartner.php
I am unsure if I am correctly access to my validatePartner.php
这是我的脚本:
<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>
这是我的validatePartner.php:
Here is my validatePartner.php:
<?php
function myfunction() {
$myvar = $_POST['q']." how are you?";
$myvar2 = $_POST['z'];
echo $myvar."\n".$myvar2;
}
myfunction();
?>
我在同一目录中有这个teo文件.
I have this teo files in the same directory.
感谢您的帮助!
解决方案:
使用@jonasfh提示,我得到了我所需要的! 我用他告诉我的两个文件创建了一个组件,并在我使用的ajax函数中创建了该组件:
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>
相反:
<script language="javascript" type="text/javascript">
. . .
$.ajax({
url: '?option=com_validatepartner&tmpl=json',
. . .
</script>
感谢您的帮助!
推荐答案
您可能应该制作一个组件来返回@MrCode概述的ajax调用.该组件可能非常简单,但至少需要2个文件(可能还有更多文件?):一个xml定义文件和一个入口点,如下所示:
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和 /components/com_validatepartner/validatepartner.php
/administrator/components/com_validatepartner/validatepartner.xml and /components/com_validatepartner/validatepartner.php
此处.文件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);
现在还有一个技巧:在您的模板文件夹中添加以下文件: json.php
Now one more trick: In your template folder add the following file: json.php
包含:
defined('_JEXEC') or die;
<jdoc:include type="component" />
现在,您可以通过以下方式测试您的组件:
Now you can test your component by going to:
index.php?option = com_validatepartner#结果在正常的tmpl文件中
index.php?option=com_validatepartner #result inside the normal tmpl-file
和
index.php?option = com_validatepartner& tmpl = json#使用您的json.php tmpl文件
index.php?option=com_validatepartner&tmpl=json # to use your json.php tmpl-file
最后调用与@MrCode建议的文件类似的文件,但有少量更改
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);
}
});
还记得通过转到extensions-> extension-manager然后单击Discovery在joomla后端中注册组件.
Also remember to register the component in the joomla backend, by going to extensions->extension-manager and then Discovery.
向乔纳斯致敬
在以上代码和示例中,布局已更改为tmpl.还更改了一些文件布局等.
Changed layout to tmpl in the above code and examples. Also changed some of the file layout etc.
这篇关于jQuery Ajax使用PHP函数返回HTML代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!