Reply
Ajax, MySQL, and PHP help!
Old 06-04-2007, 03:44 PM Ajax, MySQL, and PHP help!
The PHP Professor

Posts: 341
Name: Alex
Location: Behind You
Trades: 0
I know a lot about MySQL and PHP, but not so much Ajax. I am kind of new to the Ajax stuff, but i have seen it work and it looks pretty cool.

I have a script that is able to add and delete friends from a friends list, but i want to add Ajax to it so that i dont have to have seperate pages and it will save me a little bandwidth. I have seen this work on some other websites but i dont really know anything about Ajax so can some people help me here.

I wanted to make this an Ajax script so that you dont actually have to go to the URL's. Also, this is an example script.

PHP Code:
<?php
//If user wants to add friend
if($_GET['add'] == 'true')
{
//get the friends id
$friendid $_GET['fid'];
//get the logged in users id
$userid $_SESSION['userid'];

//Make a MySQL Connection
mysql_connect("localhost""username""password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

//Add the friend to database
mysql_query("INSERT INTO friends_list (userid, friendid) VALUES('$userid', '$friendid' ) ") or die(mysql_error());
//display a success message
echo "Your friend #$friendid, has been added to your friends list!";
exit;}


//If user wants to delete friend
if($_GET['delete'] == 'true')
{
//get the friends id
$friendid $_GET['fid'];
//get the logged in users id
$userid $_SESSION['userid'];

//Make a MySQL Connection
mysql_connect("localhost""username""password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

//Delete the friend from the database
mysql_query("DELETE FROM friends_list WHERE friendid='$friendid' AND userid='$userid' ") or die(mysql_error());
//display a success message
echo "Your friend #$friendid, has been removed from your friends list!";
exit;}
?>
__________________
Go Kirby! <(" . "<) (^" . "^) (>" . ")> Talkupation!

Last edited by microcolt; 06-04-2007 at 03:45 PM..
microcolt is offline
Reply With Quote
View Public Profile Visit microcolt's homepage!
 
 
When You Register, These Ads Go Away!
Old 06-04-2007, 04:05 PM Re: Ajax, MySQL, and PHP help!
tripy's Avatar
Do not try this at home!

Posts: 3,438
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
You seems to have misunderstood what ajax is.

Basically, it's a bit of javascript that makes a call behind a user browser windows to a url passing parameters, and act upon the response of the server.

So, obviously, your php stay the way it is, but you must develop the javascript frontend that will do the requests and update your page content upon each responses sent back by your php page.

I usually use this structure:
PHP backend :
PHP Code:
$ary=array();
foreach(
$_REQUEST as $key=>$val){
  
$ary[$key]=htmlentities(trim($val));
}

switch(
$ary['action']){
  case 
'insert':
    
$q="insert into someTable (something) values ('{$ary['value']}')";
    
$r=$dbWrapper->doQuery($q);
    if(
$r===false){
      echo 
"something went wrong in insert...";
    }
    break;
  case 
'delete':
    
$q="delete from someTable where id={$ary['id']}";
    
$r=$dbWrapper->doQuery($q);
    if(
$r===false){
      echo 
"something went wrong in delete...";
    }
    break;
  default:
    echo 
"Action {$ary['action']} is not defined";

And then, I create a simple html page that will hold the controls (snippet here...) :
HTML Code:
<ul>
  <li>
    <a href="javascript:insertVal('aValue');">Insert a value</a>
  </li>
  <li>
    <a href="javascript:delVal(id);">Delete a value</a>
  </li>
</ul>
And now for the js.
I work with the prototype js library, so I won't go in the mechanics of explaining how hand instantiate ajax query objects. Do a bit of googling, you'll find many resources about that.

Code:
function insVal(_newVal){
  var url="/backend.php";
  var pars="action=insert&value="+_newVal+"&hash="+Math.random();
  var myAjax = new Ajax.Request(
    url, 
    {
      method: 'post', 
      parameters: pars, 
      onComplete: function(request){
        if(request.responseText=='SUCCESS'){
          alert('insert successful');
        else
          alert('insert unsuccessful:'+request.responseText);
        }
      }
    }
  );
}

function delVal(_id){
  var url="/backend.php";
  var pars="action=delete&id="+_id+"&hash="+Math.random();
  var myAjax = new Ajax.Request(
    url, 
    {
      method: 'post', 
      parameters: pars, 
      onComplete: function(request){
        if(request.responseText=='SUCCESS'){
          alert('delete successful');
        else
          alert('delete unsuccessful:'+request.responseText);
        }
      }
    }
  );
}
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 06-05-2007 at 05:09 PM..
tripy is online now
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-05-2007, 03:20 PM Re: Ajax, MySQL, and PHP help!
The PHP Professor

Posts: 341
Name: Alex
Location: Behind You
Trades: 0
Well, i understand some of what you posted, but not all. Can you explain what the scripts are doing when you would "add" or "delete" a buddy in this situation.

I mostly do not understand the .JS part and what is going on with the functions.

and Thanks BTW.
__________________
Go Kirby! <(" . "<) (^" . "^) (>" . ")> Talkupation!
microcolt is offline
Reply With Quote
View Public Profile Visit microcolt's homepage!
 
Old 06-05-2007, 04:57 PM Re: Ajax, MySQL, and PHP help!
tripy's Avatar
Do not try this at home!

Posts: 3,438
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
The function don't do anything useful.
It's just a showcase of what I'd do to implement ajax call.
I obviously would pass more datas to the record creation, whatever the record will hold.

And for the syntax, as I stated in my first post, I'm using the prototype js library, and it's a json notation.
http://www.prototypejs.org/

Look here for a reference:
http://www.sergiopereira.com/articles/prototype.js.html

But it seems pretty clear to me...
I define a target url where to send the datas
Code:
var url="/backend.php";
I set up the method I want to send to the backend (post or get):
Code:
method: 'post',
I set up the datas to send to the backend:
Code:
var pars="action=insert&value="+_newVal+"&hash="+Math.random();
and I define how to handle the ajax response in an anonymous javascript function:
Code:
onComplete: function(request){
        if(request.responseText=='SUCCESS'){
          alert('insert successful');
        else
          alert('insert unsuccessful:'+request.responseText);
        }
And everything else is handled by prototype.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 06-05-2007 at 05:08 PM..
tripy is online now
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Ajax, MySQL, and PHP help!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.10140 seconds with 13 queries