调用PHP函数与AJAX,但重定向不能正常工作不能正常、函数、重定向、工作

2023-09-10 22:07:27 作者:﹏丢了幸福の猪゛

我有一个是把我的头一个问题。我会说明情况......我正在与opencart一个项目,我创建了一个按钮的工作原理是这样的:当你点击它,它应该发送给网站的所有者一切都购物车中,并这些产品一起去客户数据,并且只可以发送电子邮件,如果用户登录,那么这个问题开始。

I have a problem that is turning my head. I will explain the situation... I'm working on a project with opencart, and I created a button that works like this: when you click on it, it should send to the website owner that everything is within the shopping cart, and these products go together customer data, and only can send the email if the user is logged in, then the problem starts.

这是该按钮,其客户会点击:

This is the button where the customer will click:

<div class="checkout-button"><a id="button-quotation" class="button">click me</a></div> 

再通过AJAX将调用PHP函数来检查这一点:

Then through the AJAX will call the PHP function to check for this:

$(document).ready(function(){
$('#button-quotation').click(function(){
    var clickBtnValue = $(this).val();
    var ajaxurl = 'index.php?route=checkout/cart/quotation',
    data =  {'action': clickBtnValue};
    $.post(ajaxurl, data, function (response) {
        alert("works!!");
    });
});});

在这里,有一个重定向功能,将发送给客户端的登录页面,如果没有登录:

And here there is a redirect function that will send the client to the login page, if it is not logged:

public function quotation(){
    if (!$this->customer->isLogged()) {
        $this->session->data['redirect'] = $this->url->link('checkout/cart', '', 'SSL');

        $this->redirect($this->url->link('account/login', '', 'SSL')); 
    }
}

我可以使邮件工作,但我不能运行重定向如果没有登录到该网站的客户端。有人能帮助我吗?

I can make the emails work, but I can not run the redirect if the client is not logged into the site. Someone can help me please?

推荐答案

如果你在你的PHP函数重定向只有AJAX请求将会被重定向。

If you redirect in your PHP function only the AJAX request will be redirected.

你应该做的:让函数返回的数据,指示JavaScript的,它应该发出一个重定向到登录页面

What you should do: let the function return data that indicates the JavaScript that it should issue a redirect to your login page.

也看到这一问题:How管理一个jQuery的Ajax调用后重定向请求

See also this question: How to manage a redirect request after a jQuery Ajax call