?
Solved

PHP and?  Need Javascript type behavior...

Posted on 2004-09-20
4
Medium Priority
?
189 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 1500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

840 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