Solved

PHP and?  Need Javascript type behavior...

Posted on 2004-09-20
4
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL Persistent Connections 10 50
PHP Curl Problem 10 68
WordPress  Failed to Import Media 8 60
How to confirm and grey the submit button using JavaScript ? 14 61
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

738 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