Reputation: 51
I'm using ajax with jQuery and I'm trying to use a function the php rather that the file.
My current code
jQuery
$.get("ajax2.php", function(data){
$('#textbox').html(data);
});
php
<?php
..php code
echo $theData;
?>
How do I update my jQuery.get() function call a function in the php rather than the file.
<?php
function test()
{
..php code
echo $theData;
}
?>
Upvotes: 0
Views: 3395
Reputation: 5897
@mblase75 The answer is blow... Make the ajax call to something like:
<script>
$.get("ajax.php?command=dance", function(data){
$('#textbox').html(data);
});
</script>
//------PHP(ajax.php)--------
witch($_GET['command') {
case 'delete':
delete_record();
break;
case 'dance':
cut_a_rug();
break;
default:
return;
}
function cut_a_rug(){
//do something
}
Upvotes: 1
Reputation: 360742
Javascript cannot call PHP functions directly. They're two utterly different environments. You'd need to create a simple web service API to allow external agents (e.g. javascript) to call things in PHP.
e.g
<?php
switch($_GET['command') {
case 'delete':
delete_record();
break;
case 'dance':
cut_a_rug();
break;
default:
return;
}
And "call" the functions by hitting appropriate urls:
http://example.com/yourscript.php?command=delete&id=42
http://example.com/yourscript.php?command=dance&type=waltz
Upvotes: 2