Posts: 3,438
Name: Thierry
Location: I'm the uber Spaminator !
|
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..
|