如何使用 Laravel、Bootstrap Modal 和 Ajax 将数据发布和插入数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30452045/
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 Post and insert Data into DB using Laravel, Bootstrap Modal and Ajax
提问by saimcan
Issue is i am not able to get bootstrap modal textarea input value using Input::get(), it returns null.
问题是我无法使用 Input::get() 获取引导模式 textarea 输入值,它返回 null。
Modal's submit and text input(my view):
Modal 的提交和文本输入(我的观点):
<button type="button" class="btn btn-danger" onclick="customModalSubmitFunction('modalID')">Submit</button>
JS function:
JS函数:
function customModalSubmitFunction(modalID){
$.ajax({
type: "POST",
url: "urlRouteToController"
});
}
Route.php route
Route.php 路由
Route::post('urlRouteToController', array(
'uses' => 'myController@insertValueIntoDatabase',
'as' => 'urlRouteToController'
));
Controller
控制器
public function myController(){
$dbModel = new Model;
$dbModel->column1 = Input::get('textbox1');
$dbModel->save();
}
In conclusion, i cannot get posted data using Input::get('textbox1'). But inserting data into database as null values (because i cannot get textbox value) part works.
总之,我无法使用 Input::get('textbox1') 获取发布的数据。但是将数据作为空值插入数据库(因为我无法获得文本框值)部分有效。
Thanks in advance.
提前致谢。
采纳答案by Szenis
The controller assuming you are adding new users (just for the example). First I devine the model users in the construct function so that the function called myController can use it in a nice/clean way. Then I am calling to a function within the Model.
假设您正在添加新用户的控制器(仅作为示例)。首先,我在构造函数中指定模型用户,以便名为 myController 的函数可以以一种漂亮/干净的方式使用它。然后我调用模型中的函数。
Use Illuminate\Http\Request;
Use App\Models\Users;
class className extends baseController
{
private $users;
function __construct(Users $users) {
$this->users = $users;
}
public function myController(Request $request) {
$this->users->createUser($request);
}
}
The model will need to have the exact same name of the table in the database so assuming we have a table called Users our model class will be Users. The variable fillable makes sure only the given fields may changes the is for protecting things like for example a membership status. The function will create a new record in the database and hash the password of the user.
该模型需要与数据库中的表具有完全相同的名称,因此假设我们有一个名为 Users 的表,我们的模型类将是 Users。变量 fillable 确保只有给定的字段可以更改用于保护诸如会员身份之类的内容。该函数将在数据库中创建一个新记录并散列用户的密码。
use Illuminate\Database\Eloquent\Model as Eloquent;
class Users extends Eloquent
{
protected $fillable = ['username', 'password'];
public function createUser($request) {
$this->create([
'username' => $request->username,
'password' => \Hash::make($request->password)
]);
}
}
Then the form needs to have a field with the name username and a field with the name password. Or if you want to do it with a ajax request you could do it like this be aware that I am using the ID of the input field in this example.
然后表单需要有一个名称为 username 的字段和一个名称为 password 的字段。或者,如果您想使用 ajax 请求执行此操作,您可以这样做,请注意我在此示例中使用的是输入字段的 ID。
function customModalSubmitFunction(modalID){
$.ajax({
type: "POST",
url: "urlRouteToController"
data: {'username' : $('#username').val(), 'password': $('#password').val()}
});
}
I am using laravel 5 so I am not sure this will work correctly but I hope it will help you
我正在使用 laravel 5,所以我不确定这是否会正常工作,但我希望它会帮助你
回答by vedat
you need to pass data in $.ajax function.
您需要在 $.ajax 函数中传递数据。
function customModalSubmitFunction(modalID){
$.ajax({
type: "POST",
url: "urlRouteToController",
data: $('form-id').serialize(),
});
}