如何在octobercms中使用Ajax或javascript动态更改表单的方向 [英] 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,
这意味着我使用了一个JavaScript函数,该函数接受用户的选择并返回一个字符串
it means that I use a javascript function that takes the user's choice and return a string
ajax处理程序 data-request-redirect 仅接受字符串,而不执行javascript代码:
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>
那么 data-request-redirect
so what to do in the data-request-redirect
我真的被封锁
请帮助
推荐答案
可能很简单的方法是使用Redirect
门面.
May be simple thing is to use Redirect
facade.
使用
October Ajax framework
时,可以从服务器发送重定向.
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-查看#重定向
现在,如果未指定任何内容,它将自动将网页重定向到/home
Now it will automatically redirect webpage to /home
when nothing specified
如果将some-choise
指定为my-profile
,则会将页面重定向到/user/profile
if you specify some-choise
as my-profile
it will redirect page to /user/profile
因此,您甚至可以通过
url in post data
并使用它,您不需要做任何花哨的事情[因此您可以省略data-request-redirect
]
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 omitdata-request-redirect
]
data-request-redirect是字符串,因此它的静态数据可能是您可以更新的,但以上方法对于动态重定向url更好. [redirect specified as string
=> https://octobercms.com/docs/ajax/javascript-api#javascript-api ]
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.
这篇关于如何在octobercms中使用Ajax或javascript动态更改表单的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!