0

Let me see if I can explain what's happening...

So I've got a PHP page where results from a specific search are displayed. In this page I've got select boxes (this particular one is static) to filter my results, in this case specifically, by region. This region_id value is being passed through Ajax/Jquery.

$("#region").change(function(){
  var region_id = $(this).val();
  console.log(region_id);
  $.ajax({

      url:"results2.php",
      method:"POST",
      data: {region_id:region_id},
      success: function(data){         
        $("#filter_results").html(data);
      }
  });
})

This PHP page "results2" also receives through HTML GET 2 attributes (recipeid and recipename). One is used in the SQL query, the other is purely for dynamic display. Everything runs smoothly but whenever I start applying filters "recipeid" and "recipename" this message appears:

Notice: Undefined index: recipeid in... Notice: Undefined index: recipename in...

However, the filters are being correctly applied, but the results are wrong since those 2 variables are not being taken into account.

Here's the part of my PHP where results are shown (sql query included)...

<?php
$recipeid = $_GET["recipeid"];
$recipename = $_GET["recipename"];
$servername = "localhost";
$username = "root";
$password = "";
$db = "motherwine";


    // Create connection
    $conn = new mysqli($servername, $username, $password, $db);
  $conn->set_charset("utf8");

    // Check connection
    if ($conn->connect_error) {
        die("Erro: " . $conn->connect_error);
    }

  //Display results with region filter ON
    if (isset($_POST["region_id"]))            {

          if($_POST["region_id"] !="")
          { $region_id = implode("", $_POST["region_id"]);         
            $sql="SELECT wineid, wine_name, winetype_name, wine_img, region_name, sponsored, recipe_name, recipeid, grade_name, nota
            FROM (SELECT wine.id AS wineid, wine_name, winetype_name, wine_img, region_name, sponsored, recipe_name, recipe.id AS recipeid, round(avg(grade_id),0) AS nota
            FROM wine, winetype, region, recipe, rating, pairing
            WHERE wine.id = pairing.wine_id AND wine.winetype_id = winetype.id AND wine.region_id = region.id AND
            recipe.id = pairing.recipe_id AND 
            rating.pairing_id = pairing.id and recipe.id LIKE '%$recipeid%' and wine.region_id LIKE '$region_id'
            GROUP BY wineid, wine_name, recipe_name) AS temp, grade
            WHERE grade.id = temp.nota ORDER BY sponsored DESC, nota DESC;";
            $result = $conn->query($sql);


            echo '<div class="container">
      <div class="row justify-content-start text-left mb-2">
        <div class="col-md-9" data-aos="fade">
          <h2 class="font-weight-bold text-black">Para acompanhar "'.$recipename.'", a mãe sugere...</h2>
        </div>      
      </div>

      <div class="row justify-content-end text-right">
            <h6 class="pr-4 text-black">Ordenar por:</h6>
            <div class="single-element-widget">                        
                    <div class="default-select" id="default-select">
                        <select>
                        <option value="1">Avaliação: melhor</option>
                        <option value="1">Avaliação: pior</option>
                        <option value="1">Preço: mais caro</option>
                        <option value="1">Preço: mais barato</option>                            
                        </select>
                    </div>
                </div>
      </div>
      <div class="row" data-aos="fade">';

If there's not enough information please let me know...

Flemingpt
  • 17
  • 6
  • 2
    You will be surprised but `result2.php` __does not__ receive any GET-variables. – u_mulder Jan 07 '20 at 17:56
  • You're **only** passing `region_id` via your ajax form. You're not passing any GET variables at all. Mixing POST/GET variables is usually not a good idea. – aynber Jan 07 '20 at 17:57
  • 1
    Those GET variables are being received through a JS function. I can assure you that they're being passed. – Flemingpt Jan 07 '20 at 17:58
  • 2
    Do you see any parameters in `url:"results2.php",`? Do you? – u_mulder Jan 07 '20 at 17:59
  • and your filter need a div like `
    ` in html to get result
    –  Jan 07 '20 at 18:00
  • u_mulder: those 2 values are not being passed through AJAX/JQUERY. But they are being received because results are shown correctly at first. The problem starts once filters are applied. – Flemingpt Jan 07 '20 at 18:03
  • They may be passed on the initial page load, but they do not get sent back through the ajax query. Those are two separate requests. – aynber Jan 07 '20 at 18:05
  • @dilek: that div is in another php file (results.php). Inside of it there's a include of "results2.php". – Flemingpt Jan 07 '20 at 18:05
  • @aynber: Yep. That's what happening. So how can I pass them through ajax jquery after the 2nd/3rd...etc requests? – Flemingpt Jan 07 '20 at 18:06
  • With an url like `url:"results2.php?param=value"` – u_mulder Jan 07 '20 at 18:06

0 Answers0