Reputation: 1762
Issue is i am not able to get bootstrap modal textarea input value using Input::get(), it returns null.
Modal's submit and text input(my view):
<button type="button" class="btn btn-danger" onclick="customModalSubmitFunction('modalID')">Submit</button>
JS function:
function customModalSubmitFunction(modalID){
$.ajax({
type: "POST",
url: "urlRouteToController"
});
}
Route.php route
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.
Thanks in advance.
Upvotes: 4
Views: 8329
Reputation: 4170
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.
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.
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.
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
Upvotes: 3
Reputation: 56
you need to pass data in $.ajax function.
function customModalSubmitFunction(modalID){
$.ajax({
type: "POST",
url: "urlRouteToController",
data: $('form-id').serialize(),
});
}
Upvotes: 2