how to change the direction of a form dynamicly with ajax or javascript in octobercms

混江龙づ霸主 提交于 2019-12-24 10:25:04

问题


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


回答1:


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 facade https://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.



来源:https://stackoverflow.com/questions/53340658/how-to-change-the-direction-of-a-form-dynamicly-with-ajax-or-javascript-in-octob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!