R
R
richman772020-07-05 00:41:59
JavaScript
richman77, 2020-07-05 00:41:59

Redirect to js, ​​we take data from the server via json. How?

Good afternoon.
Opencart 3

1. We take the url value for the redirect and pass it to the controller via post.
2. From the controller we pass the value of the required url like this:

$json['redirect'] = $this->request->post["redirect"];

3. In the template, using js, you need to launch a redirect to the transmitted url. I'm trying to do something like this, but it doesn't work...
function () {
if (json['redirect']) {
  location.href = json['redirect'];
}
}

I ask for help, how to redirect to the url I need via json?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arseny, 2020-07-05
Matytsyn @ArsenyMatytsyn

Don't get your hopes up that the PHP language will be implemented in JS. If you catch parameters in PHP, then redirect on it and do: To transfer data to the face of PHP, you need to transfer them correctly.
header("Location:[url]");

R
richman77, 2020-07-05
@richman77

The bottom line is that when filling out the form and clicking on the button, the data is transferred to the controller via js code.

$.ajax({
            url: 'index.php?route=extension/module/test',
            type: 'post',
            data: $('#test2 input[name=\'test3\'], #test2 input[name=\'redirect\']'),
            dataType: 'json',

Moreover, if a value was entered for the redirect (url of the page from where we came to the form page), we pass this value back to js
$json['redirect'] = $this->request->post["redirect"];

And then I need to do either a page update in a piece of js code or a
location.reload();
redirect back to the page where they came from. but the knowledge is not enough to implement it, I myself have not yet been able to find a working version of the code
, thanks in advance!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question