0
Hello, I need to call a script inside a page triggered by a button using ajax method.
My script in Javascript:
<script type="text/javascript">
function attachProjetoFuncionario()
{
var id_projeto = document.getElementById("ajaxValueProjeto").value;
var id_funcionario = document.getElementById("ajaxValueFuncionario").value;
$.get('projeto/'+id_projeto+'/'+id_funcionario+'/attachProjetoFuncionario', function(data){
console.log(data);
var idProjeto = data.id_projeto;
var idFuncionario = data.id_funcionario;
})
}
</script>
The HTML area that prints the information in the form of cards is as follows.
<div>
<h5 style="margin-top: 12px; margin-bottom: 6px; text-align: left;"><strong>Funcionários Encarregados: </strong></h5>
<?php
$funcionarios = $projeto->Funcionarios;
?>
<div id="ajaxCardsFuncionarios" style="margin-top: 20px; display: table">
<div class="mb-6" style="display: table-row; ">
@foreach($funcionarios as $fun)
@if($fun->removido != 1)
<div class="card text-white bg-primary" style="min-width: 10rem; display: table-cell; float:left;">
<div class="card-header"><a href="http://127.0.0.1:8000/funcionario/{{ $fun->id }}/detailedFuncionario/" style="font-size: 25px">{{$fun->name}}</a></strong></div>
<div class="card-body">
<button style="margin-top: 2px; padding: .150rem .75rem" type="button" class="btn btn-primary" onclick=" window.location.href = 'http://127.0.0.1:8000/funcionario/{{$fun->id}}/atualizarFuncionario';" >Editar</button>
<!-- Botão de Remover -->
<button style="margin-top: 2px; padding: .150rem .75rem;" type="button" data-toggle="modal" data-target="#md{{$fun->id}}" class="btn btn-primary">Remover</button>
<!-- Fim dos Botões de Editar e Remover -->
</div>
</div>
@endif
@endforeach
</div>
</div>
</div>
The area of the page on which you can click to add or remove employees (within an extra information attach method) is as follows.
<!-- Div Escolher Funcionários -->
<center><div>
<?php
$fun = new App\Funcionario;
$funcionarios = App\Funcionario::all();
?>
{{ csrf_field() }}
<div style="margin-top: 20px; float: left;">
<h4 style="margin-top: 18px">Atualizar Funcionários</h4>
</br>
@foreach($funcionarios as $fun)
@if($fun->removido != 1)
<center><div style="display: table-row;">
<p style="padding-top: 10px; margin-top: 10px; float: left;">{{$fun->name}}</p>
<input id="ajaxValueFuncionario+{{$fun->id}}" value="{{$fun->id}}" type="hidden" name="">
<div style="float: left;">
<button onclick="attachProjetoFuncionario()" id="ajaxAdicionarFuncionario"
class="btn btn-primary" style="margin-left: 10px; padding: 6px; padding-right: 12px; padding-left: 12px; padding-bottom: 10px; ">+</button>
<button type="button"
class="btn btn-primary" style="padding: 6px; padding-right: 16px; padding-left: 16px; padding-bottom: 10px; ">-</button>
</div>
</div></center>
@endif
@endforeach
</div>
</div>
<!-- Fim da Div Escolher Funcionários -->
The framework I’m using for desktop is Laravel, with MVC structure. The routes and controllers that relate all this is working completely, I did some tests to know this before.
My specific question is how can I correctly call my variable, which in this case is the "ajaxValueFunctioning" for something like "ajaxValueFunctioning" concatenated with variable "$fun->id", the latter being a php variable that varies according to the new one printed on the employee page of the @foreach loop.
That correct statement I’ve made in:
(...)
<input id="ajaxValueFuncionario+{{$fun->id}}" value="{{$fun->id}}" type="hidden" name="">
(...)
Now I need to know how to do something similar within the script.
Thank you for your attention.
Because it does not pass from id directly to function <button onclick="attachProjectsFunctioning('ajaxValueFunctioning'+{{$fun->id}})" and in its function it takes as parameter Function attachProjectsFunctioning(idfunc) for example
– Jorge Costa
Thanks! I used something based on your idea and it worked yes!
– Braian