Search code examples
phpjqueryjquery-loadurl-parameters

jQuery parameters not passed to PHP


I am not sure why but jQuery is not passing parameters to PHP in order to complete the load() function with specific data based on the parameters ID.

In short term, when a user clicks a link there is onclick which fires up photo_tab('.$row['id'].');

account.php HTML element

<li><a href="#!/photos" onclick="photo_tab('.$row['id'].');"><i class="icon-camera"></i> Photos</a></li>

In a sence the ID should be passed to the photo_tab (the script is included into page not on the page it self)

general.js

function photo_tab(user_id) {
    $('.ac-content').load('ajax/photos.php',{user_id:id});
} 

And photos/ page should be loaded which is a PHP page and based on the user_id value load photos from database associated with that user_id.

<?php
   if ($_GET['user_id'] == 2) {
      echo "WORKED!";
   } else {
      echo "FAILED!";
   } 

***this is the photos/ file located at ajax/photos.php with mod_rewrite to point
otherwise to just photos/ or photos

EDIT: 1 - Using full path instead of rewrite photos > ajax/photos.php does not work 2 - The ID is being passed and echoed onto page via $row['ID']; 3 - ajax/photos.php is using $_REQUEST


Solution

  • Try passing the data as a query string, like so:

    function photo_tab(user_id) {
        $('.ac-content').load('photos/','user_id='+id);
    } 
    

    EDIT: Giving this a second look, is that URL correct? It seems weird to have a relative URL.

    You should also make sure the generated markup is correct, is the row ID printing out in the source?

    EDIT 2: I set this up locally, this is working code, mimicking your file structure.

    //localhost/account.php

    <html>
    <head>
        <script src="http://code.jquery.com/jquery.min.js"></script>
        <script>
            function photo_tab(id) {
                $('.ac-content').load(
                    'ajax/photos.php',
                    { 'user_id': id }
                );
            }
        </script>
    </head>
    <body>
        <div class="ac-content"></div>
        <a href="#!/photos" onclick="photo_tab(3);"><i class="icon-camera"></i> Photos</a>
    </body>
    

    //localhost/ajax/photos.php

    <?php
    if (isset($_GET['user_id']) && $_GET['user_id'] == 2)
    {
        echo "WORKED!";
    }
    else
    {
        echo "FAILED!";
    }