1

I am aware that I can call PHP scripts after processing an AJAX request, but am I able to do it the other way around?

I am writing a dynamically sized navigation component for a new website and I want to refresh the nav every time a new item is added to the Menu.

In my menu class I am currently thinking of using this approach to achieve the effect:

public static function new_item($label, $link) {

    $pos = self::num_items();        

    $DB = Database::getInstance();

    $query = $DB->connection->prepare("INSERT INTO menu VALUES('', :label, :link, :pos )");
    $query->execute(array(':label'=>$label,
                          ':link' =>$link,
                          ':pos'  =>$pos+=1  
                   ));

    ?>

    <script>

        function refreshHeader() {
            $.ajax({
                type: "GET",
                url:  "my url",
                success: function() {
                    // refresh the header here somehow
                }
            });
        }

    </script>

    <?php
}

However, every time this runs I see nothing in my console (even when I put a valid url into my function), will I be able to achieve what I want this way or would I be better processing appending the tab to the document via AJAX and then calling the insert method in my Menu class on the success callback?

The only reason I ask is I feel this way may look neater in comparison to the other way of handling it, although I may be completely wrong.

Any feedback would be greatly appreciated - cheers Alex.

9
  • why aren't you using CURL ? Commented Jun 11, 2014 at 14:39
  • cURL for what? That does not make any sense. Commented Jun 11, 2014 at 14:40
  • @AndreiCristianProdan CURL? Sorry I've been away from web for some time, what benefits would that give me? Commented Jun 11, 2014 at 14:40
  • By "refresh the header" you mean reload the HTML part representing your header, right? If yes then you need to return the new HTML part in your initial $.ajax call and replace the old one with the new one in the success callback. Commented Jun 11, 2014 at 14:41
  • @SergiuParaschiv thats exactly what I want to do, thankyou Commented Jun 11, 2014 at 14:42

2 Answers 2

2

echoing the following line can call out existing javascript functions and you are even able to insert variables (only strings and ints though).

echo "<style onload='jsfunction(\"$vars\")'></style>";

I find this one of the most simple ways to do this quickly.

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

1 Comment

its invisible and it works :P If I use it I like to throw it in a hidden div on the page, just to be sure.
0

Oh, I now understand what you want to do, what you need is websockets. When a piece of php code (sever side) is executed, you want something to happen in the browser (client side). Take a look at how live chats are made with javascript and websockets, long polling and other similar methods, that's what you need here.

This helped me a lot What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?

Comments

Your Answer

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.