1

I'm trying to figure out the best way to do this...I am using JQuery to submit data to a PHP function, which sends back data from the DB as JSON, which is working. The thing is, on success, I want the JQuery to execute a PHP function...and I'd rather not have to make yet another AJAX call on top of the first AJAX success - especially since the php function is something I've already used elsewhere on my page. This is my code:

JQUERY:

$.ajax({
    type: "POST",
    url: post_url,
    success: function(group) //we're calling the response json array 'tree'
    {
        //WANT TO CALL THE PHP FUNCTION HERE
    } //end success
 }); //end AJAX

PHP:

<?php
foreach($groups as $group){
    echo '<option value="' . $group->id . '">' . $group->group_name . '</option>';
}
?>
4
  • 1
    why not place the additional function call in your PHP file? Commented Jun 1, 2011 at 21:14
  • Is this just an example, or are you really trying to avoid writing one line of php in javascript? Commented Jun 1, 2011 at 21:15
  • Execute the function while creating the result in the first function. php function exists on the sever while jQuery is on the client. Without AJAX, they can't talk to each other. Commented Jun 1, 2011 at 21:16
  • to answer your question, you make the call like any other $.ajax or $.json call. I support @brian_d's response as well. Just execute both blocks of code at the same time. You have the context already, why pass it to client only to pass back to server to get more info? Just wrap all info up in the response. Commented Jun 1, 2011 at 21:18

4 Answers 4

3

In your php script that builds your response, just add an extra property to your object that is the html string you want to put into your page and then have javascript put it where it needs to go. You have no choice but to run php functions on the server.

Sign up to request clarification or add additional context in comments.

2 Comments

why don't you separate your display and your data and deal with the display on the javascript end?
I agree with you on that statement. I actually posted a comment to his question asking why he was avoiding writing that in javascript but decided to just answer his question.
1

If you want to call a php function from your javascript you will have to execute an AJAX request to the function, in its own file, on the server. This is because javascript executes client side and PHP executes server side. Therefore, javascript does not have direct access to PHP functions.

You can use your PHP to write JavaScript, or you can call a php file from a javascript via AJAX.

Can you replicate the functionality of the PHP function with a JavaScript function?

Would this, or something similar, work for you:

$.ajax({
  type: "POST",
  url: post_url,
  success: function(groups) //we're calling the response json array 'tree'
  {
        for(group in groups){
            document.writeln("<option value='"+groups[group].id+"'>"+groups[group].group_name+"</option>";
        }
  }
}); //end AJAX

Comments

0

Alternatives:

  1. Change the PHP that backs the original AJAX call so that, instead of sending back JSON, it sends back HTML exactly as you want it.
  2. Chain a second AJAX call "inside" the success of the first, so that a second round-trip to the server is done for the JSON->HTML conversion.
  3. Accept that sometimes you need to "duplicate" presentation-rules when you are working with multiple languages, and use something like EJS to do the same <option> stuff that PHP does elsewhere.

Comments

0

You have to be aware that you're dealing with two paradigms here, the first one is server-side logic (PHP) and the second one is client-side logic (JavaScript).

Unfortunately there is no way to call PHP from JS from the client without performing an AJAX call and rendering the content you want to show. However, you could prepare the result and set the body of the AJAX response with your requested HTML string.

If this is not possible, I suggest to look at the diverse number of JS frameworks that will provide helpers to generate options for select fields.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.