0

I am trying to clean up my pages, and one of the major ways that I am doing so is to put JavaScript and JQuery functions into a file called 'scripts.js' that is automatically accessed upon page load.

I've run into a problem with functions that use php to call from the page itself. For example, the following function doesn't work, and in fact 'kills' the script for all pages (so now things that were supposed to be hidden are not, and things are not loading properly). I've narrowed it down to the fact that I use to call a variable. I would really like to be able to keep functions using PHP in this universal file as opposed to clogging up the HTML template pages, any thoughts on either how to make this work, or if not how else I may be able to call the values needed? They are always extracted to the page before rendering if that helps.

function positiveSelect()
{
   var size = <?php echo $idea[0]["size"]; ?> * 1;
   if (size > 5)
      return true;
   else
      return false; 
}
cschippe
  • 81
  • 2
  • 10
  • 1
    You could store that data somewhere in your html and access it with js. For example, if you are outputting `$idea[]` onto the page as divs with the class "idea", you could do `var size = $(".idea").length` – Kevin B Jan 16 '13 at 16:54
  • Is there any commonly accepted way of doing that? Or do you have any thoughts on the best way? I'd rather not have to just insert a bunch of paragraphs with white text. Essentially, is there any way to store it in the HTML in such a way that it won't show up on the page at all? – cschippe Jan 16 '13 at 16:56
  • turn your .js file into a php script, and include/run whatever code is necessary to populate that $idea var. – Marc B Jan 16 '13 at 16:57
  • What does the data represent in this case? how is it used? what is it for? there are many different ways to store it, but it should be done relative to the elements that it represents (if any). – Kevin B Jan 16 '13 at 16:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22845/discussion-between-kevin-b-and-finalform) – Kevin B Jan 16 '13 at 17:06
  • @FinalForm Are we looking at php or at js with some php echoed in? Although there are some missing brackets, as a js function this would probably work. – jeroen Jan 16 '13 at 17:11
  • @jeroen It's JS, it works fine when the script is applied to the page that it acts on (eg, the page that the php variables have been extracted to). However, if the page does not have one of the php variables, it kills the script (which is why it is causing me trouble when I try to put it in my universal functions folder) – cschippe Jan 16 '13 at 17:21
  • @Chris S That's what I figured, but there seemed to be some miscommunication between KevinB and FinalForm... – jeroen Jan 16 '13 at 17:35

5 Answers5

4

if you can't retrive your data form the DOM itself you can store values with the corresponding object:

 <div data-size=20>

and then retrive it with:

 $(element).data("size");

or if you have global data you want to store you can create a value "container" in the head of you html document like this:

<script type="text/x-json" class="global-data">{"value1":"1","value2":"2"}</script>

and then read the content of that element and parse it with JSON.parse

t.niese
  • 39,256
  • 9
  • 74
  • 101
  • This makes sense and seems like the easiest and best way to do it, any thoughts on the best html element to use? I'm thinking – cschippe Jan 16 '13 at 17:12
  • i only would create a new element if it is really required. if you e.g. want to ensure that at least 5 checkboxes of a group are selected and not more then 10, you could group them (if not already done) inside an element and add this informations: `
    ... ....
    `
    – t.niese Jan 16 '13 at 18:24
  • @ChrisS created a little jsfiddle example http://jsfiddle.net/AnaAh/ hope this helps you – t.niese Jan 16 '13 at 18:33
2

If this function is that specific to a certain page, you might want to add a second js script that just gets loaded on that page.

An alternative would be to echo out a js variable in that php page and have your code call that function with that variable as a parameter.

jeroen
  • 91,079
  • 21
  • 114
  • 132
2

You can give the javascript a ".php" extension and call it in the script in the same exact way:

<script type="javascript" src="path/to/scripts.php"></script>
Luca Simonetti
  • 127
  • 1
  • 8
  • i would avoid mixing php and javascript whenever possible, because this would prevent using e.g. jslint or minimizing tools. – t.niese Jan 16 '13 at 17:07
  • I tried this but it didn't seem to work for me, and after reading a few of the other comments a lot of people seem to think it's a bit 'dangerous' to do it this way. Huge thanks anyway though. – cschippe Jan 16 '13 at 17:09
  • @t.niese: I know that, but since he needed this kind of solution, that was the best I came up with. – Luca Simonetti Jan 25 '13 at 12:37
0

You could just name the generate scripts file scripts.php or scripts.js.php; then the PHP preprocessor will process the file and the PHP statements will be evaluated.

Frank Rosario
  • 2,512
  • 5
  • 31
  • 47
-1

When mixing php or any server side language with javascript you need to be aware that the php gets executed only once when the javascript file is created on the client side.

This is probably why you are getting unexpected results. As you move from page to page the php snippet in your global scripts.js will not get updated.

user1333371
  • 598
  • 3
  • 13