Ad

How To Change The Direction Of A Form Dynamicly With Ajax Or Javascript In Octobercms

I have a form, and I would like it when I submit it, that it redirects me in another page,

knowing that the redirection path must be dynamic,

it means that I use a javascript function that takes the user's choice and return a string

the ajax handler data-request-redirect only accept a string and execute not javascript code:


title = "test"
url = "/test"
layout = "default"
is_hidden = 0
==
<?php
function onStart()
{
    $this['example'] = Session::get('example-key');
}

function onTest()
{
    Session::put('example-key', input('my-input'));
}

?>
==

<form   method="POST"  data-request="onTest" data-request-redirect="......" name="formu" accept-charset="UTF8"  enctype="multipart/form-data">


    <input type="text" name="my-input">
    <button type="submit">Submit</button>
</form>

{% if example %}
    <strong>Example: {{ example }}</strong>
{% endif %}


<script type = "text/javascript">

           $(function() {

               var $buttonBien = $('.bien');

                $buttonBien.on('click',function (event) {
                    event.preventDefault();

                });


           });


function type_bien(x){

    switch( x) {
    case 0:
       return "formulaire_villa";

        break;
    case 1:

                    return "formulaire_villa";
        break;
    case 2:
                   return "formulaie_riad";
        break;

    case 3:


        document.getElementById(3).checked="true";
       /* document.forms["formu"].action="formulaire_appartement";*/

            return "formulaire_appartement";
        break;

    default:
         alert('local_commerce est selected');
}

}


</script>

so what to do in the data-request-redirect

I am really blocked

please help

Ad

Answer

May be simple thing is to use Redirect facade.

when you use October Ajax framework you can send redirect from server.

so from your function / code section.

use Redirect;

function onTest()
{
    $defaultUrl = '/home';

    // you get data from post
    // from hidden or select etc ...
    $someChoise = post('some-choise');

    if($someChoise == 'my-profile') {
        $defaultUrl = '/user/profile';
    }

    Session::put('example-key', input('my-input'));
    return Redirect::to($defaultUrl);
}

more info on Redirect facadehttps://octobercms.com/docs/services/response-view#redirects

Now it will automatically redirect webpage to /home when nothing specified

if you specify some-choise as my-profile it will redirect page to /user/profile

so in this way you can even pass url in post data and use it, you don't need to do anything fancy [ so you can omit data-request-redirect]

data-request-redirect is string so its static data may be you can update it but above way is better for dynamic redirect url I guess. [ redirect specified as string =>https://octobercms.com/docs/ajax/javascript-api#javascript-api ]

if any doubt please comment.

Ad
source: stackoverflow.com
Ad