Solved

PHP and?  Need Javascript type behavior...

Posted on 2004-09-20
4
170 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now