Jump to content

AJAX HELP - Working with multiple results: maybe wrong forum - but desperate!


ineedhelpbigtime

Recommended Posts

Hi i cant get my Ajax to do what i want!

 

I am working on a uni project which is a basic social networking site. I am designing the friends system in AJAX/PHP. The part i am stuck on is accepting friend requests.

 

Here's my code for the displaying of requests awaiting authorisation.

 

// $awaitingresult is the query to return friend requests and is working.

 

if(!($awaitingresult))

{

  echo "<h2>Awaiting Confirmation...</h2><br />You currently have no requests to action.";

}

else

{

  $awaitingtotal = mysql_num_rows($awaitingresult);

  echo "<h2>Awaiting Confirmation...</h2><br />You currently have $awaitingtotal confirmation(s) awaiting your attention!<hr/>" ;

  while($awaitingrow = mysql_fetch_array($awaitingresult))

{

$birthdate = "$awaitingrow[birthday]";

    echo "<br /><img src='images/nia.jpg' align='right' alt='No Image Available' width=\75' height='75'>

Name: $awaitingrow[names]<br/>

    Username: $awaitingrow[username] <br/> 

Age: " . GetAge($birthdate);

 

 

 

echo "<form method='post' name='accept' action='javascript:acceptfriend()'><input type='hidden' id='authid' name='id' value='$awaitingrow[authid]'><div id='insert_response'><input name='image' type='image' onMouseOver=\"this.src='images/accept1.png'\" onMouseOut=\"this.src='images/accept.png'\" src='images/accept.png' align=middle width=80 height=18> <a href='ignore.php'><img src='images/decline1.png' align=middle alt='Add to ignore list!'></a></form></div>" ;

 

// the form above initiates the AJAX. The ajax returns the results to the div insert_response.  !! PROBLEM !! when multiple results are returned by the ajax/php. When i click the accept rollover, it doesn't know what input i want to take the results from.

}

}

 

AJAX:  ** the ajax doesn't know which input id=authid that it should be taking the value from, so always takes the first one regardless of how many results there are. **

var nocache = 0;

function acceptfriend() {

// Optional: Show a waiting message in the layer with ID login_response

document.getElementById('insert_response').innerHTML = "Just a second..."

// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.

var id = encodeURI(document.getElementById('authid').value);

// Set te random number to add to URL request

nocache = Math.random();

// Pass the login variables like URL variable

http.open('get', 'confirm.php?authid='+id+'&nocache = '+nocache);

http.onreadystatechange = acceptfriendReply;

http.send(null);

}

function acceptfriendReply() {

if(http.readyState == 4){

var response = http.responseText;

// else if login is ok show a message: "Site added+ site URL".

document.getElementById('insert_response').innerHTML = ''+response;

}

}

 

PHP: ** the php is working, but when you click on "accept" it updates using the authid value of the first result, and the code is displayed in the div=insert_response of the first result!. **

 

$id = $_GET['authid'];

$awaitingresult = mysql_query ("SELECT `names`, `username` , `birthday` , `useroneid` , `usertwoid`, `authid`

FROM `users` , `auths`

WHERE users.userid = auths.useroneid

AND auths.authid='$id'");

$row = mysql_fetch_array($awaitingresult);

mysql_query ("UPDATE auths SET status = 1 where authid='$id'");

$names = $row['names'];

echo "<img src=\"images/tick.png\" align=\"left\" alt=\"Green Tick\" width=\15\" height=\"15\"/> You have successfully confirmed" . first_name($names);

echo "as your friend!";

 

 

PLEASE TELL ME SOMEONE KNOWS THE ANSWER TO MY PROBS. I know the ajax i am using is to basic to carry out this task, but i am at a loss. I cant find any examples on net of how to fix this (probably because i dont know what im looking for) and im getting desperate now!

 

Forever indebted to anyone that can help. All the best.

Link to comment
Share on other sites

You can have acceptfriend() take an argument.  The argument could be an identifier for the current friend, or it could be a the object itself that you want to manipulate.

 

BTW, "id" is supposed to be unique within a page.  You could use id='insert_response1', id='insert_response2', etc etc.  Then you could pass that unique identifier into acceptfriend(), which could use it to find the appropriate element.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.