4

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.

4
  • You are not sending the data with the ajax request. Commented May 26, 2015 at 7:25
  • @Szenis i tried data: {myData, "urlRouteToController"} too, but in controller i couldn't be able to get posted value using $_POST['myData'] too. That one gave me a null value too. Commented May 26, 2015 at 7:28
  • You should use the Request class use Illuminate\Http\Request; place this on top right below the namespace of your controller then in the function public function myController(Request $request) Then u can try to var dump the request variable to check if you are reciving any data. for more info check laravel.com/docs/5.0/requests Commented May 26, 2015 at 7:34
  • @Szenis i am already checking laravel documents and API, btw using laravel 4.2. There's nothing wrong with including namespaces. But thank you for the help. Commented May 26, 2015 at 7:50

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Just because of using serialize(), my function wasn't working. Tried .val() and it works like a charm.
Why can't user serialize for this ? why not explain?
2

you need to pass data in $.ajax function.

function customModalSubmitFunction(modalID){
    $.ajax({
        type: "POST",
        url: "urlRouteToController",
        data: $('form-id').serialize(),
    });
}

3 Comments

Tried this one too already. my laravel debugger shows me this query result : insert into myTableName (myField1) values ('') so it still returns a null value.
in js --> data: {'myData': data}, controller --> Input:get('myData') sould be work
debugging says that "Cannot read property 'serialize' of null" which means the value hasn't been reached to the javascript function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.