Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 10th, 2007, 01:02 AM
Registered User
 
Join Date: Jan 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default PHP & Javascript

I have 2 drop downs. Both the drop downs are related. In the sense that after choosing some option from one drop down ,the options in the second drop down should change relative to the value chosen in first drop down. options in both dropdowns are coming from the database. I guess i will need to use javascript. I am working on PHP.
BUT sending values from one script to another is a problem.
Can anybody please help!!!

 
Old January 10th, 2007, 02:18 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran
Default

hai,
it is easy if you follow ajax, try to do with ajax, i have done a site call www.toys4brain.co.uk, view this site left hand menu i am using ajax, if u want any help about doing with ajax contact me, i will ready to help you.

surendran
(Anything is Possible)
http://ssuren.spaces.msn.com
 
Old January 18th, 2007, 03:45 PM
NC NC is offline
Registered User
 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by ketki
 I have 2 drop downs. Both the drop downs are related. In the sense that after choosing some option from one drop down ,the options in the second drop down should change relative to the value chosen in first drop down. options in both dropdowns are coming from the database. I guess i will need to use javascript. I am working on PHP.
BUT sending values from one script to another is a problem.
Basically, you have three options.

The new and fashionable one is AJAX; your write an onChange event handler for the first drop-down that sends an asynchronous HTTP request to the database and retrieves the content to put into the second drop-down to match the option selected in the first.

There are two more traditional approaches. One is to reload the script when the first dropdown changes and populate the second one based on the value chosen in the first. Another is to use PHP to dump all data that can be used to populate the second dropdown, but do the actual populating with JavaScript.

Here is a static example of the last approach:

<script language="JavaScript">

function populateSelect2(n) {
  switch (n) {
    case "1":
      document.forms[0].elements[1].options[0] = new Option("Option 1-1", "1");
      document.forms[0].elements[1].options[1] = new Option("Option 1-2", "2");
      document.forms[0].elements[1].options[2] = new Option("Option 1-3", "3");
      document.forms[0].elements[1].options.length = 3;
      break;
    case "2":
      document.forms[0].elements[1].options[0] = new Option("Option 2-1", "1");
      document.forms[0].elements[1].options[1] = new Option("Option 2-2", "2");
      document.forms[0].elements[1].options[2] = new Option("Option 2-3", "3");
      document.forms[0].elements[1].options[3] = new Option("Option 2-4", "4");
      document.forms[0].elements[1].options[4] = new Option("Option 2-5", "5");
      document.forms[0].elements[1].options.length = 5;
      break;
    case "3":
      document.forms[0].elements[1].options[0] = new Option("Option 3-1", "1");
      document.forms[0].elements[1].options[1] = new Option("Option 3-2", "2");
      document.forms[0].elements[1].options[2] = new Option("Option 3-3", "3");
      document.forms[0].elements[1].options[3] = new Option("Option 3-4", "4");
      document.forms[0].elements[1].options.length = 4;
      break;
  }
}

</script>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <select name="select1" onChange="populateSelect2(select1.value)">
    <option value=1>Option 1
    <option value=2>Option 2
    <option value=3>Option 3
  </select>
  <select name="select2">
    <option value=0>Select more options...
  </select>
</form>

The content inside the populateSelect2() function can be generated dynamically by PHP. The advantage of this approach is that changes in the second dropdown are instantaneous. The disadvantage is that in some situations you may have too much data to transfer this way.





Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP & JavaScript variables? mlewitz Beginning PHP 2 December 2nd, 2005 08:18 AM
PHP & JavaScript variables? mlewitz PHP Databases 2 November 30th, 2005 10:16 PM
Javascript && keeps turnig into &amp;&amp; ayrton Pro VB.NET 2002/2003 3 June 27th, 2005 03:34 PM
Javascript & PHP rajuru Beginning PHP 6 February 22nd, 2005 06:15 AM
Error: movie.php & commit.php on p182-186, ch6 willburke BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 October 12th, 2004 02:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.