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

Asked

Viewed 762 times

0

Good afternoon!

I have the following problem: I have a php page (products.php), where the page body 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 (While the.php products page finds the get normally).

Thanks 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>

    ");


?>    

  • What do you want the get for? Would it be useful to store its value in a js variable? show the code of the two other pages

  • Good afternoon, Guilherme! The variable passed via GET will serve to select the category or gender of the product in the bank. The other pages are not yet complete, because we still have to be able to pass the variable to them. I’ll add a basic example of how other pages work. Thanks for the response!

1 answer

0


You need to pass the php $category variable 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>
  • Good morning, Juliano! It worked perfectly! Thanks, because in the javascript category I am not as advanced as in php. The only addendum I put is that the javascript load() method uses the GET request if the parameter is not passed, as in this case we pass the parameter, in php you should use $_REQUEST instead of $_GET. Once again, thank you for your reply!

Browser other questions tagged

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