PHP Query and jQuery load not giving any results

I use the following jQuery function and PHP query to display a listing of menu items:


[B]jQuery:[/B]

      $(".nav").on("click", "a", function(e) {
        e.preventDefault();
        $("#kaart").load(this.href); 	
      });


[B]Nav url:[/B]

<a href='modules/site/menu_items.php?menu_id=1' class='' title=''>Soup & Salads</a>


[B]PHP:[/B]

<?php 
  $menu_id       = filter_input(INPUT_GET, 'menu_id', FILTER_SANITIZE_NUMBER_INT);
  if (isset($menu_id)){
    $menukaart = $pdo->query("SELECT menu_kaart_id, menu_kaart_type_nl, menu_kaart_type_en FROM menu_kaart WHERE menu_kaart_id = 

$menu_id"); 								
    $menukaart->setFetchMode(PDO::FETCH_ASSOC);
	  
    while($row = $menukaart->fetch()) {
	  $menu_kaart_type = $row["menu_kaart_type_$lang"];
	    echo "<h1>";
        echo $menu_kaart_type;
		echo "</h1>";
    }	  
  }else{
      $content = $pdo->query("SELECT title, content FROM content WHERE page_id = $page AND language_abbr = '$lang'");
  
      $content->setFetchMode(PDO::FETCH_ASSOC);
  
      while($row = $content->fetch()) {
          echo "<h1>{$row['title']}</h1>
		            {$row['content']}" ;
      }
   }  	  
?>

By default the second query $content is executed, which is working fine. This content should be replaced with data from the query $menukaart when one of the links is clicked but it is not it is just giving me the empty div kaart. Does anyone see what is wrong?

Thank you!

The databse table is there and has data in it as you will see in this example page, because the navigation on the left is coming from the same table. But I don’t get any results. I even tried to set the varable $menu_id manually at the top of my PHP but still no result

One of the possible causes of ending up with blank data when the page is displayed in your browser is making sure the field you are specifying in the $row[‘field’] is included from your query. As it turns out, what you specify in $row IS case sensitive. I think the following line in your code may be causing the issue:

$menu_kaart_type = $row[“menu_kaart_type_$lang”];

Thank you for the reply Bribread22. For the purpose of testing your presumption i have changed:


while($row = $menukaart->fetch()) {
  $menu_kaart_type = $row["menu_kaart_type_$lang"];
  echo "<h1>"; echo $menu_kaart_type;	echo "</h1>";

into


while($row = $menulisting->fetch()) {
   echo "<h1>{$row['menu_kaart_type_nl']}</h1>" ;
    }

where menu_kaart_type_nl is coming from the actual table. But I still don’t have any result

When I hardcode the menu id in the navigatie bar like this http://fanskitchen.sothenwhat.com/fanskitchen-menukaart.php?menu_id=2 it is working. What am I doing wrong?

Edit:
I think it has to do with the way I am using PDO! Because for some other websites with a similar functionality I used mysqli and they are all working fine

Hi donboe,

Was the PHP code in your first post from the file modules/site/menu_items.php? If so, is that the entire file, because you’re not creating a DB connection anywhere?

Hi fretburner. Yes it is. The DB connection is included in the main page:


<?php
  try
  {
      $pdo = NEW PDO ('mysql:host=**<host>**;dbname=**<db name>**', '**<user name>**', '**<password>**');
  }
      catch (PDOException $e)
  {
      echo 'Unable to connect to the database server.';
      exit();
  }
?>

like this. Should I include it in the file modules/site/menu_items.php itself?

Your JS is making a new request to menu_items.php, which means that the script in that file doesn’t have access to any variables or data from the current request. So yes, you would need to include your DB connection file within menu_items.php.

If you’re also including menu_items.php from fanskitchen-menukaart.php, make sure you use [fphp]include_once[/fphp] when including the DB connection script to avoid PHP trying to include the file twice.

Hi fretburner
. That was indeed the case :). Thanks a lot. Is there now ay to include the db connection global?

A common way of dealing with this issue is to use the Front Controller pattern. This is where you set up your server to direct all page requests through a single script (often index.php) which loads all common resources (e.g. a DB connection) and then loads and displays the correct template based on the URL.

Most PHP frameworks use this pattern, and there are several good, light-weight frameworks out there that you can start with to ‘dip your toe in the water’ if you’ve not used a framework before, such as Slim.

A (very basic) example of using Slim might look like this:


$app = new \\Slim\\Slim;
$app->db = new PDO ('mysql:host=localhost;dbname=mydb', 'myuser', 'mypass');
 
// www.mydomain.com
$app->get('/', function(){
    echo "Home Page";
}); 

// www.mydomain.com/testPage
$app->get('/testPage', function() use ($app) {
    $conn = $app->db; // Get DB connection
    $app->render('testpage.php');
});
 
$app->run();

Notice that the PDO connection is set up and available to all routes (pages) on the site.

So when I go and use this script I just need to include it in index.php?

You’d include the framework files into your index.php file, and then set up your routes like in the example above.

If you downloaded the zip file from the website, it includes a basic example ready to go. It’s probably easier to look through that, and have a read of the documentation on the site and see if you think you want to go down that route. You can always start another thread if you have questions, and I or someone else can help you out.

It can be a bit of a learning curve if you’ve not used a framework before, but for anything above the level of simple scripts it really does save you time and make things easier.

Thanks a lot fretburner. This is all very helpful!!

Hi fretburner. Apparently I have the same problem with:


$menu_kaart_type = $row["menu_kaart_type_$lang"];

where variable $lang represent the language i want the listing to been shown. Unlike the the database connection I can not delare that one in the file (modules/site/menu_items.php) itself since I want to use just one file to serve several languages.

How are you keeping track of the chosen language within the site, are you using a session variable?

Hi fretburner. No I am not using sessions. The mail language, in ths case ducth is in the root and the other languages have their own directory e.a. en, el, es etc. But I already found a sollution I sticked the variable $lang in the url

<a href='../modules/site/menu_items.php?menu_id=1&lang=$lang' class='' title=''>

and in /modules/site/menu_items.php I added:

$lang = filter_input(INPUT_GET, 'lang', FILTER_SANITIZE_STRING);

and it is working fine! Thanks for your input.