How to change the value of a php variable using Javascript or Ajax?

Asked

Viewed 671 times

1

Contextualization When registering a project by default (default) Status of Documentation is pending as a result of this configuration project tabs are not visible. inserir a descrição da imagem aqui If the user changes the Status of Documentation for Authorized or Accordingly and press the button Authorize Documentation as project tabs will be visible. inserir a descrição da imagem aqui

Doubt: How to do this event in Javascript, Ajax or Jquery?

Possible Solution: I declared the php variable $btnAutorizarDocument as in kind boolean. But I’m having trouble making the programming logic. Because when this variable is "Authorized" or "Accordingly" it must be true, otherwise it must be false.

Follows the codes: The project registration is done in the editing mode. Method of edit class Projectocontroller.php:

 //Método que  redireciona para a página de editar o  Projeto
 public function edita($id, Request $request)
 {
    $btnAutorizarDocumento = false;
    $setores = Setor::all();
    $proponentes = Proponente::all(); 
    $tipoProjetos = TipoProjeto::all(); 
    $modalidadeApoios = ModalidadeApoio::all();
    $localidades = Localidade::all(); 
    $permissoesAprovacao = verificarPermissao('Aprovação');
    $permissoesJuridico = verificarPermissao('Juridico');
    $permissoesGestaoContrato = verificarPermissao('Gestão Contratos e Convênios');
    $permissoesFinanceiro = verificarPermissao('Financeiro');
    $permissoesOcorrencia = verificarPermissao('Ocorrência'); 
    $permissoesAbas= verificarPermissao('Abas do Projeto'); 
    $permissoesDadosGeraisProj= verificarPermissao('Dados Gerais do Projeto'); 
    $usuarioResponsavel = Auth::user()->name;

    return view('admin.projeto.edita', ['projeto' => Projeto::find($id), 'permissoesAprovacao' =>  $permissoesAprovacao, 'permissoesJuridico' => $permissoesJuridico,
            'permissoesGestaoContrato' =>  $permissoesGestaoContrato , 'permissoesFinanceiro' => $permissoesFinanceiro,
            'permissoesOcorrencia'=> $permissoesOcorrencia, 'permissoesDadosGeraisProj' => $permissoesDadosGeraisProj,
            'permissoesAbas' =>  $permissoesAbas, 'setores' => $setores, 'proponentes' => $proponentes, 'tipoProjetos' => $tipoProjetos,
            'localidades' => $localidades,'modalidadeApoios' => $modalidadeApoios, 'usuarioResponsavel'=>$usuarioResponsavel,'btnAutorizarDocumento' =>$btnAutorizarDocumento]);
 }

Excerpt from the project edit page code edita.blade.php. Contains the selection combo and the button Authorize Documentation.

  <div class="col-md-6 form-group">
                <label class="control-label">Status da Documentação</label>
                <select multiple id="statusDoc" class="form-control" type="text" class="form-control" name="statusDoc" >
                    <option value="A" {{old('statusDoc') == 'A' ? 'selected' : ''}}>Autorizado</option>
                    <option value="P" {{old('statusDoc') == 'P' ? 'selected' : ''}}>Pendente</option>
                    <option value="C" {{old('statusDoc') == 'C' ? 'selected' : ''}}>Em Conformidade</option>
                </select>                
                </div>
 <button  id="btnAutorizarDocumento" type="button" class="btn btn-primary" data-toggle="button" aria-pressed="false" autocomplete="off" 
                        align="center" style="width: 180px; height: 40px" onclick="click();">
                        Autorizar Documentação
                        </button> 

Excerpt from the Conditional that validates the visibility of the project tabs. Explanation: if the user has permission to view the tabs and the document is authorized the tabs will be visible.

@if($permissionsAbas->create == 1 && $btnAutorizarDocument)

  <!--Inicio da abas-->
    @if($permissoesAbas->create == 1 && $btnAutorizarDocumento)

    <div class="card">
      <!-- Nav tabs -->

         <ul class="nav nav-tabs" id="nav-tab" role="tablist" >
            <li  class="nav-item nav-link active"  aria-controls="aprovacao" data-toggle="tab" href="#aprovacao" role="presentation" aria-selected="true"><i class="fa fa-check" aria-hidden="true"></i> Aprovação</a></li> 
            <li  role="presentation"><a href="#juridico" aria-controls="juridico"  data-toggle="tab"><i class="fa fa-gavel" aria-hidden="true"></i> Jurídico</a></li>
            <li  role="presentation"><a href="#gestaoContrato" aria-controls="gestaoContrato"  data-toggle="tab"><i class="fa fa-book" aria-hidden="true"></i> Gestão de Contratos e Convênios</a></li>
            <li  role="presentation"><a href="#financeiro" aria-controls="financeiro"  data-toggle="tab"><i class="fa fa-usd" aria-hidden="true"></i> Financeiro</a></li>
            <li  role="presentation"><a href="#ocorrencia" aria-controls="ocorrencia"  data-toggle="tab"><i class="fa fa-tasks" aria-hidden="true"></i> Ocorrências</a></li>
          </ul>

Excerpt in Javascript that will make this event (file edita.blade.php) Note: it is incomplete

<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script type="text/javascript">
    $(document).ready(function() {      
        $( "#btnAutorizarDocumento" ).bind( "click", function() {
            $( "{$btnAutorizarDocumento}" ).val(true);
        });     
    });
</script>
  • Remove the onclick="click(); button... the event is already captured in the bind, otherwise the button will be clicked 2x.

  • Thanks, I made the modification. But my main question is about the programming logic that will probably be done in Javascript.

  • From what I understand you want to change a PHP variable, right? And you want to use Ajax for this.

  • Yes. That’s it, but I don’t know how to make this modification. I have little knowledge in Javascript and its technologies.

  • the problem is that there is no way to change a PHP variable other than Session

  • I’d like you to post an example, so I have an idea how to solve this.

  • You think sending something "something" to a PHP page via Ajax could change something?

  • A basic example of Ajax: var valor = "o que quero enviar para o PHP";&#xA;$.ajax({&#xA; url: "pagina.php",&#xA; type: 'POST',&#xA; data: {valor:valor},&#xA; success: function(data) {&#xA; // data é o que retorna do servidor, se quiser que algo retorne&#xA; // caso não deseje resposta, pode apagar este success: function(data){....}&#xA; }&#xA;});

  • Thank you. I’ll run the tests. :)

  • In this case, using session. The php variable must be of type Session?

  • The variable can take the session value: $variavel = $_SESSION['nome_da_session'];

  • But like I said, you can’t change PHP variable via Ajax... you can change Session. For example, if $_SESSION['nome_da_session'] = "1";, with Ajax you can change the value to $_SESSION['nome_da_session'] = "2";, for example.

  • Good afternoon, @dvd. I posted another question regarding this problem. I tried to solve it with ajax; I would like you to help me. Follow the link: https://answall.com/questions/290916/como-synchronizr-o-ajax-com-o-php-para-manipular-os-dados

Show 8 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.