views:

229

answers:

4

Hi, I don't know how to ask this question that's why I'm going to simulate what I need, some developers make what I need by rendering javascript with php. A simple sample file would look like:

my_javascript_file.php

<?php include "my_class.php"; ?>

$(document).ready(function(){    

  $.ajax({
    url: "<?php $my_obj->post_url(); ?>",
    success: function(){
      alert("Post successful")
    }
  });
};

which outputs following:

$(document).ready(function(){    

  $.ajax({
    url: "/post_handler.php",
    success: function(){
      alert("Post successful")
    }
  });
};

my question comes there, is there any way to achieve that with pure js techniques?

Thanks, Sinan.

P.S. This is just a simple example of course I can hardcode post url here, or get url by location object etc with javascript I hope you get what I mean...

EDIT: Sorry for confusions by the terms I used, it is just getting some information from my application and writing to my javascript dynamically. I don't mean comet or data encode etc.

What I want to know is how to use server-side data on javascript files(if there is any way). Like what php method does on my example, it simply echoes a string which comes from my application. Is there a way to do that by a javascript library or a framework, like a template engine you assign vars and use it in that template system. Hope it's more clear now.

A: 

i am not sure i know what you mean by "push" ... if it is a real server push, you should look at comet servers ...

also, to me it's unclear what "pure js techniques" are ... you can do this without jQuery of course ... but pure JS, as the languages primarily designed to manipulate the DOM will not suffice of course ... you need AJAX-like approaches ... however there was a proposal for push in HTML5, but yeah, this is gonna take some time ...

so i hope this helps ... but maybe you should clarify your question a little, i am sure you will get better answers ... :)

back2dos
Thanks for answer, but that was not what i meant:( i edited the question little bit more. hope it makes it more clear...
Sinan Y.
btw that html 5 link was cool :)
Sinan Y.
A: 

================= Edit: New Potential Answer =====================

Say you manage to retrieve JavaScript Data Objects from the server, do you wish to inject the JavaScript data you obtained in your HTML markup, client-side?

If that's the case you might look at IBDOM: http://ibdom.sf.net/

I wrote it, we've had it in production on a number of a sites for a couple of years.

================= Old Answer Below ====================

So you want "eventData" to be "something" which can be consumed by JavaScript.

I'd recommend first taking a look at JSON in PHP:

[REMOVED link to JSON in PHP, not needed]

Then your PHP Code might resemble this:

<?php include "my_class.php"; ?>

$(document).ready(function(){    

  $.ajax({
    url: "<?php $my_obj->post_url(); ?>",
    data: <?php json_encode ($my_obj->getData()); ?>,
    success: function(){
      alert("Post successful")
    }
  });
};

So the idea here, is that the value of your "data:" field would become a JSON Object graph generated by PHP on the server.

Chris Holland
Thanks for the answer but It was not my problem, what i want to know is more general. It was just a mistake to leave eventData there, I know about json_encode in php. I edited the question if you could check it again, maybe i'm more to the point now.
Sinan Y.
Thanks for that too i checked the ibdom site, it seems interesting. It makes easy to manipulate or create the html markup i suppose. What David offered above is closer to what i mean but not the exact thing. thanks anyways...
Sinan Y.
A: 

You could ask the server for the URL using .get or .ajax (assuming your urls.php will handle this properly and return a proper string - or JSON, in which case you might as well use .getJSON - with the URL in it). For example (untested of course):

$.get("urls.php", { operation: "post_handler" },
    function(data){
        $.ajax({
            url: data,
            data: eventData,
            success: function(){
                alert("Post successful");
            }
        });
    });
dalbaeb
that is not the answer to what i ask. sorry for misunderstanding. I'm going to make it more clear by rephrasing the question. thanks anyways...
Sinan Y.
+1  A: 

Yes there is a way, but its odd. You can use the jaxer server. It lets you run javascript on the server.

EDIT:

As I said in my comment I don't think you want to be downloading extra js to the client in order to implement a templating engine. Its probably a separation or decoupling that you're talking about. In which case you could have the following in a separate js file:

var myObject = {
var1 : '',
var2 : 0,
init : function(options) {
 var1 = options.var1;
 var2 = options.var2;

 $('#someElem').click(function() {
  // do stuff
  return false;
 };
}

};

and then call it like this from your html page

<head>
<script type="text/javascript">
 var options = {
  var1 : <?php echo "hello" ?>,
  var2 : <?php echo 1 ?>
 }
 myObject.init(options);
...
David Archer
Thanks, i had seen that several months ago, however what i'm looking for is more like a template engine if you ever used smarty you might get what i mean, you assign vars to smarty engine and you create template files and markup generated from those templates you create by smarty. Such thing i was looking but it is getting quite confusing here.
Sinan Y.
I get that you're wanting a js templating engine. Purely client side? I'd have to ask why. It would involve downloading a lot more to the client (library code etc). Why not pass just pass vars and set them. give me a few mins I'll edit my answer to demonstrate
David Archer
I was thinking such an approach too but i feel that there should be a better way to do that, using php outputs in scripts, as i say it's what i feel maybe there is not a better way :) thanks, anyways...
Sinan Y.
no probs, just bear in mind that you're talking about client and server here. Jaxer server tries to let you do something like what you want, but otherwise you're talking doing everything on the client which ultimately would mean downloading a lot more to the client. That doesn't make sense because it slows down the client/user experience. Otherwise, good luck :-)
David Archer
ps. I google javascript template engine, and found http://beebole.com/pure/ maybe there's something out there
David Archer