Solved

PHP and?  Need Javascript type behavior...

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

828 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