pass a Java script variable to php

Asked

Viewed 84 times

-4

Hello I have a Od in js that takes id of a field and with this id(value) I need to use in if Else within php to display posts related that id as the user click down the Cod that picks up the id

 <script>  
        jQuery(document).ready(function($){
          $(".linha-produtos").click(function(){
                var categorias = $(this).attr("id");
                //console.log(categorias);
                $("#recebe-indice").attr('src',categorias);       
           });
           //aqui
          $(".nav-item").click(function(){ 
           
            var capacitancia = $(this).attr("id");
           
                        
       });
      });
   
    </script>

My PHP code

<?php
    
    $nome = $_POST['capacitanciaa'];

   if ( $categories1->have_posts()  ) : while ( $categories1->have_posts()  ) : $categories1->the_post();
       
         $linha = get_the_title() ; 
         $slug = get_post_field( 'post_name', get_post() ); ;
        
        
        if($category->name ==  "440V")
         echo'<p id ="capacitancias" class="tabela-linhax"> '.$linha.' </p>';  else{

    };?>                           
        
                        

       
<?php   
        endwhile; 
        endif;   
?>
                           
  • 1

    Your question is very confusing. But with the information on it, you can’t do what you want, AJAX is designed to make asynchronous requests. To do this you want if I understand correctly, you will use the AJAX request for a file .php return a json for you in HTML through AJAX/Javascript be able to show on screen according to the "Id" of the button you click.

  • Oie , after mounting the ajax var capacitancia = jQuery(this). attr("id"); //console.log(capacitance); var page ="https://williarts.ga/tslcompos/products/" var data = capacitance; $. ajax({ type: 'POST', dataType: 'html', url:data, });

  • Your question seems to have some problems and your experience here in Stack Overflow may not be the best because of it. We want you to do well here and get what you want, but for that we need you to do your part. Here are some guidelines that will help you: Stack Overflow Survival Guide in English (short version). If the solution is very simple it is still possible for someone to do so in the comments.

1 answer

-1

Javascript is a language that in this case is executed on the front end or on the user’s machine, whereas PHP is a back end language and will be executed on the server side.

This way you need to control user behavior via Javascript and to pass variable value to PHP you need to run an AJAX, something like:

 $(".linha-produtos").click(function(){
            var categorias = $(this).attr("id");
            //console.log(categorias);
            $.ajax( "example.php" )
            .done(function() {
              //Quando terminar de fazer a requisição e tiver uma resposta do php positiva
             })
             .fail(function() {
                //Quando a requisição falhar por algum erro
              })    
       });

So you control the user screen send and communicate with PHP and take the return and handle the actions on the user screen without having to reload the page.

  • Oie Thank you first for the attention , because then I’m calling in an ajax , so my doubt and how my php will receive the variable because I want to use for comparison in an if

  • $(".Nav-item"). click(Function(){ var capacitancia = jQuery(this). attr("id"); var url_atual = window.location.href; console.log(capacitance ); var page ="https://williarts.ga/tslcomponetes/wp-content/themes/bb-them-codee-child/shorts/productosshortcode.php/"; $.ajax({ type: 'POST', dataType: "html", url: page, date: {'choice' :empowerment }, });

  • Hello Fabio if you do your ajax with the Get method you can use on the php side (pure) $_GET['a_Variable'] if it is post $_POST['a_variable'], if you are using some framework (which I suggest as a good practice) then you would need to check the framework documentation why each one can handle different ways.

Browser other questions tagged

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