0

First of all, i have read and tried what's on this post : Laravel change input value

My goal is to change some inputs' values on page load, but i am doing wrong somewhere.

i.e. I have some inputs filled by user lately, now i want them to edit these fields. So i need to pull some data from database and insert these into my inputs.

navTabs.js

$(document).ready(function(){
   $('#myTab a').on(function (e) {
        e.preventDefault();
        $(this).tab('show');
    },myOnLoadFunction());
}

function myOnLoadFunction(){
   $.post('myRoute');
}

Routes.php :

Route::post('myRoute', array(
    'uses' => 'MyController@myMethod',
    'as' => 'myRoute'
));

MyController.php

public function myMethod(){
   Input::merge(array('myInputName' => 'theValueFromDatabase'));
}

So, i am making a mistake somewhere, but i couldn't find it out.
Everything works fine on test until myMethod i can get responses.
Am I using merge method wrong ?

Thanks in advance.

UPDATE

MyController.php

public function myMethod(){
   Input::merge(array('myInputName' => 'theValueFromDatabase'));
   //i have nearly 15 inputs here, assigning each of them one by one.
   $allVariablesWithNewInput = \Input::all();
   return Response::json($allVariablesWithNewInput);
}

Now this value is being returned to navTabs.js

function myOnLoadFunction(){
   $.post('myRoute', function(data){
      document.getElementById('myFieldID').value = data['myInputName'];
      //and assign 15 more inputs here to see values on html elements.
   });
}

First of all, thank you for your patience. We figured out to find a solution to my problem with this way. But i am not satisfied with this solution. I'm going to have to write a lot of code here. Is there any way to reduce repetition ? Do you offer an algorithm or something else ?

Thank you.

1
  • @AbishekRSrikaanth do you mean returning Input::get('inputname') ? I'll try this one. Commented Dec 15, 2014 at 15:25

2 Answers 2

2

After calling \Input::merge() have you tried calling \Input::get() doesn't it give you the merged array?

Because \Input::merge() only adds the new set of input parameters that you pass and not return anything.

Let me know if this works:

public function myMethod(){
   Input::merge(array('myInputName' => 'theValueFromDatabase'));
   $newInputVariables = \Input::get('myInputName');
   $allVariablesWithNewInput = \Input::all();
}
Sign up to request clarification or add additional context in comments.

1 Comment

this one works. Also returning values with return Response::json(Input::all()); I've updated the post.
1

I see some errors which you should fix and maybe it will help.

Javascript

$(document).ready(function(){
    $('#myTab a').on('this needs an event', function (e) {  // 'click' maybe?
        e.preventDefault();
        $(this).tab('show');
    },myOnLoadFunction);  // Shouldn't put paranethesis
}

function myOnLoadFunction(){
      $.post('myRoute');
}

You likely aren't getting a response however because you aren't actually returning anything from your method. If you are wanting to get json, try return Response::json(Input::all());

1 Comment

still working on it. by the way .on('load', function e) works. but myOnLoadFunction without () does not work.

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.