0

im struggling to pass laravel 8 array value in javascript variable with function old(). this is when store the data

"date" => array:4 [▼
        0 => "2022-03-28"
        1 => "2022-03-29"
        2 => "2022-03-30"
        3 => "2022-03-31"
      ]

however it some error with the validation. i need to get back the value throught javascript. but i can't get the value back. here is the code

$(document).ready(function(){   
    var total_row = 4; 

    for (let row = 0; row < total_row; row++) { 
        var getOld_value =  {{ json_encode(old( '"date."+row+' )) }};  

        console.log(getOld_value);
    }

});

all return is null.. how do i able to get the value on javascript.

please help

ferdinand
  • 325
  • 2
  • 14
  • Seems like you are erroneously mixing PHP code with Javascript. E.g. How is Javascript supposed to know what `json_encode()` is? – GetSet Mar 28 '22 at 05:38
  • @GetSet yes. i still learning javascript. so the code is mixing with PHP. im googling it and set like that. so i still confuse – ferdinand Mar 28 '22 at 05:44
  • Well I should have said more with emphasis, that you can't mix PHP and Javascript. This is why PHP code needs its own tags to break from whatever else code, so that it can be rendered in place server-side. E.g. `alert( echo "hello world"; )` doesn't work but `alert( )` does. – GetSet Mar 28 '22 at 06:03
  • What does the `old()` function do? json_encode can be replaced with JS's JSON.parse() but whether you can do this entirely in JS depends on what `old()` is doing. Alternatively. since this JS is executed when your page loads perhaps you can do all this task in server-side code within your template or your controller action. It's unclear what it's all for, since all it does is output a value to the console, which isn't useful by itself (except for debugging). – ADyson Mar 28 '22 at 07:11

0 Answers0