Php Prestashop获取显示为null的ajax响应
在prestashop,我正在做一个小模块。在smarty的模块中,我有一个表单。我想使用ajax提交这些值。因此,我将ajax设置为$.post,如下所示Php Prestashop获取显示为null的ajax响应,php,ajax,prestashop,prestashop-1.6,Php,Ajax,Prestashop,Prestashop 1.6,在prestashop,我正在做一个小模块。在smarty的模块中,我有一个表单。我想使用ajax提交这些值。因此,我将ajax设置为$.post,如下所示 jQuery(document).ready(function($) { $('.module-form').submit(function(e) { e.preventDefault(); msg = ''; $form = $(this); $.post( baseDir +
jQuery(document).ready(function($) {
$('.module-form').submit(function(e) {
e.preventDefault();
msg = '';
$form = $(this);
$.post(
baseDir + "modules/mymodule/mymodule.php",
{ name: 'myname', action: 'form_subscribe' },
function(data) {
var response = jQuery.parseJSON(data);
console.log(response);
}
);
return false;
});
});
class MyModule extends Module {
public function __construct() {
-----
----
--
}
function form_subscribe() {
$name = $_POST['name'];
echo json_encode($name);
exit;
}
}
在模块文件(mymodule.php)中,我的代码如下
jQuery(document).ready(function($) {
$('.module-form').submit(function(e) {
e.preventDefault();
msg = '';
$form = $(this);
$.post(
baseDir + "modules/mymodule/mymodule.php",
{ name: 'myname', action: 'form_subscribe' },
function(data) {
var response = jQuery.parseJSON(data);
console.log(response);
}
);
return false;
});
});
class MyModule extends Module {
public function __construct() {
-----
----
--
}
function form_subscribe() {
$name = $_POST['name'];
echo json_encode($name);
exit;
}
}
但是当我提交表单时,它将响应显示为null。有人能告诉我如何解决这个问题吗?任何帮助和建议都是非常值得的。谢谢。您不能像尝试那样直接访问类或方法。 要执行操作,必须首先实例化类,然后调用函数。 特别是在prestashop环境中,请执行以下步骤: 在模块文件夹中创建另一个php页面,让我们调用页面mymodule_ajax.php。 然后在其内部检索配置、prestashop上下文的初始化,然后实例化模块并执行调用管理 mymodule_ajax.php:
include(dirname(__FILE__). '/../../config/config.inc.php');
include(dirname(__FILE__). '/../../init.php');
/* will include module file */
include(dirname(__FILE__). '/mymodule.php');
/* will instantiate the class MyModule so that you can access the method */
$my_mod = new MyModule;
/* using Prestashop Tools Class we ensure that the call is made from the form with action "form_subscribe */
if(Tools::isSubmit('action') && Tools::getValue('action') == 'form_subscribe')
$my_mod->form_subscribe(); //call the method
然后将ajax代码更改为指向该url,以便:
$.post(
baseDir + "modules/mymodule/mymodule_ajax.php", //new url
{ name: 'myname', action: 'form_subscribe' }
现在,您应该在console.log中检索json值。您只是向一个类发出请求,它不会回答任何问题,这就是为什么响应为空。那么您能告诉我如何获取类内的值并使用它吗?还有更好的方法吗?你的ajax请求必须有一个指向PHP脚本的链接,在这里你可以使用我想知道的MyModule函数。任何参考代码或示例?用
test.php
替换modules/mymodule/mymodule.php
,并在此脚本中生成