Is there a way to pass a PHP object as a JavaScript parameter in HTML?

I am doing a project using Laravel 5. I am showing a table from a view, and I want this data value for all the data for the JavaScript function when I click this link. I try several ways, but I can not do it.

  @foreach ($basl_officers as $basl_officer)
                        <tr>
                            <td><a href="#"   onClick="functionOne($basl_officer)"  >{{ $basl_officer->officerName }} </a></td>        
                            <td align='center'>
                                {!! Form::open(['method' => 'DELETE', 'route'=>['basl_officers_page.destroy',$basl_officer->id]]) !!}
                                <a href="{{route('basl_officers_page.edit',$basl_officer->id)}}" class="btn btn-default btn-sm"> <span class="glyphicon glyphicon-pencil"></span> </a> &nbsp &nbsp
                                <button type="submit" class="btn btn-default btn-sm" onclick="return confirm('Are you sure?')"> <span class="glyphicon glyphicon-trash"></span> </button> 
                                {!! Form::close() !!}
                            </td>
                        </tr>
 @endforeach

JavaScript code

<script type="text/javascript">
function functionOne(x) {
    alert('You clicked the top text' + x);
}
function functionTwo() {
    alert('You clicked the bottom text');
}

+4
source share
2 answers

You can try the following:

onClick="functionOne({{ json_encode($basl_officer) }})"
+4
source

I got this to work using this code inside my .blade file.

<script>
    var object = <?php echo json_encode($object) ?>;
    console.log(object);
</script>

This assumes that the object is provided through your controller.

+1
source

Source: https://habr.com/ru/post/1608274/


All Articles