-1

It's clear from reading through threads that I can call a PHP function using Ajax / JQuery, but I can't quite get my head around the particulars.

Let's say I have a function foo() which simply adds 'bar' to some string I pass in. foo() lives in foo.php. To use this in my JavaScript-less HTML, I'd do something like this:

<?php include 'foo.php';?>
<?php echo foo('bar')?>

...and my output is "foobar". Great.

I suspect I do something like so to grab the output of foo() via JavaScript:

<script>
$.post("http://foo.com/foo.php", {
 <Magic happens here> 
}, function(response) {
useTheOutput(response);
});
</script>

....but I'm not clear on whether I should be POSTing or GETing...and how to pass in a value, like 'bar'.

Can anyone push me in the right direction? Here are other threads that discuss the same thing, I just can't connect all the dots:

Call php function from javascript

call function in jquery-ajax

asked Jun 20, 2012 at 13:38
4
  • 5
    You seem to lack the understanding of what php is, how it differs from javascript and what ajax does. I recommend using a tutorial which covers this stuff. Commented Jun 20, 2012 at 13:41
  • 1
    There is nothing 'magic' about web development. It is a series of requests and responses. Commented Jun 20, 2012 at 13:45
  • Actually, I get it. I have a php function which generates a "ticket". This function must be fired server-side as the request itself needs to come from a trusted IP address, which is the server on which the .php page lives. I need to call this function from JavaScript - I see other people have accomplished this on StackOverFlow, but I still need a little help. Commented Jun 20, 2012 at 13:48
  • For your POST/GET question, use POST for destructive actions such as deletion, editing, and creation. Reason for this is because you can't hit a POST action in the address bar of your browser. Use GET when it's safe to allow a person to call an action from the address bar, i.e. guiding you to a specific page, setting options in a form etc. Commented Jun 20, 2012 at 14:19

1 Answer 1

1

Solution:

PHP (foo.php):

<?php
function generateTicket()
{
 //get_trusted_ticket_direct is another function that does heavy lifting...
 $ticket= get_trusted_ticket_direct($_POST['server'], $_POST['user'], $_POST['targetsite']);
 echo $ticket;
}
if ($_POST['toDo'] == 'generateTicket') {
 generateTicket();
}
?>

JavaScript:

$(document).ready(function() {
 // Handler for .ready() called.
 $.post('http://foo.com/foo.php', {
 toDo: 'generateTicket',
 server: 'serverName',
 user: 'userName',
 targetsite: '' 
 }, function(response) {
 alert(response);
 });
});​
answered Jun 20, 2012 at 16:22
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.