How to load php page using javascript (with GET method)?

0

Good afternoon!

I have the following problem: I have a php page (productos.php), where the body of the page can vary according to 2 files (products-filters.php and products-wfiltros.php) that are loaded via javascript using load The two files in question depend on the GET method to work, however, because they are loaded via javascript they do not find the variable passed via url (whereas the products.php page finds the get normally).

Thanks in advance for your help!

<?php 

if(isset($_GET['categoria'])){
    $cat = $_GET['categoria'];
}

?>
<!-- CABEÇALHO -->

<?php require_once 'includes/header.php';?>

<!-- FIM CABEÇALHO -->

<div class="container-fluid" style="margin-top:90px">
 
  <label class="switch" style="float:right;">
  <input type="checkbox" id="testeste" onchange="getval(this);" checked >
  <span class="slider round"></span>
  </label>

</div>


<script type='text/javascript'>

 $(document).ready( function(){
 $('#produtos').load('includes/produtos/produtos-filtros.php');
 refresh();
 });

 function getval(sel){ 
 if ($('#testeste').is(':checked')){   
 $('#produtos').load('includes/produtos/produtos-filtros.php');
 } else { 
 $('#produtos').load('includes/produtos/produtos-wfiltros.php');}
 }
</script>




<div id="content " style="margin-top:130px;">
     <div class="container" id="produtos">

     </div>
</div>



<script  src="componentes/js/index.js"></script>
</body>

</html>





PÁGINA PRODUTOS-FILTROS (Exemplo):

<?php 

if(isset($_GET['categoria'])){
    $cat = $_GET['categoria'];
}


/*EFETUAR BUSCA POR PRODUTOS DENTRO DESSA CATEGORIA NO BD*/

print_r("

<div class=\"col-md-4 col-sm-6 borda-product\" onclick=\"location.href = 'produto-detalhes.php';\">
 <div class=\"product\">
  <div class=\"product_image\" >
     <img  src=\"imagens/img-produtos/$pro_nome.jpg\" alt=\"First slide\">    
  </div>
  <div class=\"product_info\">
      <h6 class=\"product_name\"><a href=\"produto-detalhes.php\">$pro_nome</a></h6>
      <div class=\"product_price\">$pro_preco</div>
  </div>
 </div>
</div>

    ");


?>    
    
asked by anonymous 05.03.2018 / 18:45

1 answer

0

You need to pass the variable from php $ category to javascript and then the parameters by load ().

It would look something like this:

<script type='text/javascript'>
var cat = <?php echo $cat; ?>

$("#produtos").load("includes/produtos/produtos-filtros.php", {categoria:cat})
</script>
    
05.03.2018 / 22:01