Solved

PHP and?  Need Javascript type behavior...

Posted on 2004-09-20
4
178 Views
Last Modified: 2006-11-17
I am very proficiant with php, and have done some Javascript.  I am creating a form where later inputs need to be dynamiclly updated based on selections within the same form.   There are a million example how to do this with Javascript (<option onChange=foo();>, etc.), and I have even hacked a PHP'ed Javascript version where I use print"" or echo"" to build the javascript functions and vaiables.  MY QUESTION IS: IS THIS THE BEST WAY?  It is tedious and a pain in the butt.


Thanks,
Chuck
0
Comment
Question by:ChuckVincent
  • 2
4 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 12107582
Hi ChuckVincent,

Well, there are millions of ways. The main ways are to store what you need to add in a Javascript array, you can populate it with PHP variables from a database, for instance.

Regards,
Zyloch
0
 
LVL 10

Accepted Solution

by:
eeBlueShadow earned 500 total points
ID: 12108846
Hi,

The way I always do it is to write the javascript functions in an external .js file and use
<script src='functions.js' type='text/javascript'></script>

Then I have a second section where I initialise any page specific JavaScript variables, these are dynamically generated, for example a PHP array I want available in JavaScript
<script type='text/javascript'>
<!--
  jsarray = new Array();
<?php
foreach($phparray as $item)
{
  echo "  jsarray[jsarray.length] = $item;\n";
}
?>
// -->
</script>

Finally, the form inputs call the functions in the external JS file, and bingo.

This is only the way I do it, rather than the definitive best way to do it, but as Zyloch said there are probably hundreds of ways to achieve the same result.

_Blue
0
 

Author Comment

by:ChuckVincent
ID: 12111201
How about the effects of haveing the <script></script> section NOT in the header of the docuement?  Any draw backs?  I use dynamic header and footer functions that are written in php and are stored in included files, but only certin pages need the overhead of the javascript functions and the building of the javascript variables from php/mysql calls, so I do not want to make them part of the header functions of every page if I don't have to.



Thanks,
Chuck
 
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12121241
Not much, but you'd have to experiment around to find if it really does affect your page. There are instances, but it's vague and difficult to know. If it doesn't work in the <body>, then it goes in the <head>, that's pretty much it... lol
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question