10010010001011101111
10010010001011101111

Reputation: 59

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

Upvotes: 1

Views: 770

Answers (4)

thordarson
thordarson

Reputation: 6251

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!";
}

Upvotes: 1

Techie
Techie

Reputation: 45124

Yes parameters should not be passed as you supposed because as the name intends method load data from the server and place the returned HTML into the matched element.

.load( url [, data ] [, complete(responseText, textStatus, XMLHttpRequest) ] )

EG : Same as above, but will POST the additional parameters to the server and a callback that is executed when the server is finished responding.

$(".ac-content").load("photos.php", {user_id:user_id}, function(){
  alert("have been loaded");
});

Try the above syntax.

  1. Try $_REQUEST instead $_GET
  2. It should photos.php or some .php not photos/
  3. Make sure id is not null.

Upvotes: 1

Mike
Mike

Reputation: 1768

How about wrapping your variable in quotes?

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

Upvotes: 1

DACrosby
DACrosby

Reputation: 11450

Looks like id was never defined. Try this:

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

Upvotes: 0

Related Questions