如何在 Bootstrap 模式中添加函数 - Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17004595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add functions in Bootstrap modal - Laravel
提问by emen
I'm currently developing a system with Laravel framework. Though, I'm not very familiar with MVC architecture yet.
我目前正在使用 Laravel 框架开发一个系统。不过,我对 MVC 架构还不是很熟悉。
When I'm developing the system, I come across a problem which relates with this bootstrap modal that has a form in it. I'd like to put functions in it so that it could be submitted to the database.
在开发系统时,我遇到了一个问题,该问题与这个包含表单的引导模式有关。我想把函数放在里面,以便它可以提交到数据库。
I have followed several tutorials, but didn't come up with a solution because they're all using pagesinstead of modals.
我遵循了几个教程,但没有提出解决方案,因为它们都使用pages而不是modals。
So, here is the HTML of the modal:
所以,这是模态的 HTML:
<div class="modal hide fade" id="linkSettings">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3>Add New Link</h3>
</div>
<div class="modal-body">
<div class="control-group">
<label class="control-label" for="focusedInput">Add A Link:</label>
<div class="controls">
<textarea class="autogrow"></textarea>
</div>
</div>
</div>
<div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal">Close</a>
<a href="#" class="btn btn-primary">Save changes</a>
</div>
</div>
Here is the Javascript of the above modal:
这是上述模式的Javascript:
$('.btn-settingLink').click(function(e){
e.preventDefault();
$('#linkSettings').modal('show');
});
Now, how do I put the function in the Controller so that whenever a user presses Save Changesbutton, it will store the value in <textarea>
into the database? Is there anything to do with the Javascript code? and Do I need to use {{Form::open('main/addLink','POST')}}
at the form to pass its values to the controller and model?
现在,如何将函数放入控制器中,以便每当用户按下“保存更改”按钮时,它都会将值存储<textarea>
到数据库中?与Javascript代码有什么关系吗?以及我是否需要{{Form::open('main/addLink','POST')}}
在表单中使用将其值传递给控制器和模型?
Basically, all I ever wanted was to have the CRUD functions by using modal.
基本上,我想要的只是通过使用模态来获得 CRUD 功能。
回答by Kylie
First you need to create a form with a route....
首先,您需要创建一个带有路由的表单....
<form method="post" action="{{URL::to('Link/addlink')}}>
Or like you showed above, with your Blade {{Form}} ...either way
或者像你上面展示的那样,用你的 Blade {{Form}} ...无论哪种方式
Then your textarea needs an id and name
然后你的 textarea 需要一个 id 和 name
<textarea id="mytext" name="mytext"></textarea>
So your HTML becomes...
所以你的 HTML 变成了......
<form method="post" action="{{URL::to('Link/addlink')}}>
<div class="modal-body">
<div class="control-group">
<label class="control-label" for="focusedInput">Add A Link:</label>
<div class="controls">
<textarea id="mytext" name="mytext" class="autogrow"></textarea>
</div>
</div>
</div>
<div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal">Close</a>
<input type="submit" id="submit" name="submit" class="btn btn-primary">Save changes</input>
</div>
</form>
Then in your Link controller...or wherever you're actually sending this form..no idea what your controllers are named :)
然后在您的 Link 控制器中……或者您实际发送此表单的任何地方……不知道您的控制器的名称是什么 :)
public function addlink(){
$input = Input::all();
//whatever validation you wanna do, with error handling etc...not gonna provide that, thats up to you
$yourmodel = New Model;
$yourmodel->text = $input['mytext'];
$yourmodel->save();
Return Redirect::back();
}
EDIT (for Ajax)
编辑(对于 Ajax)
Add this script at the bottom of your page...
在页面底部添加此脚本...
<script>
$(document).ready(function(){
$('input#submit').click(function(e){
e.preventDefault();
var text = $('#mytext').val();
var url = 'your url' //we were using Link/addlink in the other example
$.ajax({
type: "POST",
url: url,
data: { mytext: text },
success: function(){
//Probably add the code to close your modal box here if successful
$('#linkSettings').hide();
},
});
});
});
</script>
PS Completely untested....you will need some tweaking, plus the fact that your controller has the Redirect will probably make it not work since this will just be expecting an echoed Json string
PS 完全未经测试......你需要一些调整,加上你的控制器有重定向的事实可能会使它不起作用,因为这只会期待一个回显的 Json 字符串
But these are the nuts and bolts, and the foundation is there for you to continue on your own I would think...
但这些都是细节,基础是你继续自己的我想......