Current location: Hot Scripts Forums » General Web Coding » JavaScript » [SOLVED] multiple dropdowns via ajax / php


[SOLVED] multiple dropdowns via ajax / php

Reply
  #1 (permalink)  
Old 05-09-08, 03:37 PM
soloWebDev soloWebDev is offline
Wannabe Coder
 
Join Date: Apr 2007
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] multiple dropdowns via ajax / php

I am hoping one of the javascript pros here can offer assistance.
home.php
HTML Code:
<script type="text/javascript" language="javascript" src="includes/ajaxFunctionItem.js"></script>
<form name="addItems_form" method="post" action="" onSubmit="update_totals();">
<table width="100%" cellpadding="0" cellspacing="0" bgcolor="#f7f8f8" align="left" >
<tr> 
<td>Category:</td>
<td colspan="2"> <select name="category" onChange="getCapacity(this.value)"><?=category($category)?></select> </td>
</tr>
<tr> 
<td>Capacity:</td>
<td colspan="2"><div id="capacitydiv"><select name="capacity" ><option>Select category First</option></select></div></td>
</tr>
<tr> 
<td>Items:<input name="discprice" type="hidden" value=<?=$discrate?>></td>
<td colspan="2"><div id="itemsdiv"><select name="items"><option>Please select a Category</option></select></div></td>
</tr>
</table>
</form>
ajaxFunctionItem.js
javascript Code:
  1. function getXMLHTTP() { //fuction to return the xml http object
  2.         var xmlhttp=false
  3.         try{
  4.             xmlhttp=new XMLHttpRequest();
  5.         }
  6.         catch(e)    {      
  7.             try{           
  8.                 xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
  9.             }
  10.             catch(e){
  11.                 try{
  12.                 req = new ActiveXObject("Msxml2.XMLHTTP");
  13.                 }
  14.                 catch(e1){
  15.                     xmlhttp=false;
  16.                 }
  17.             }
  18.         }
  19.             
  20.         return xmlhttp;
  21.     }
  22.    
  23.     function getCapacity(categoryId) {   
  24.        
  25.         var strURL="includes/findcapacity.php?category="+categoryId;
  26.         var req = getXMLHTTP();
  27.        
  28.         if (req) {
  29.            
  30.             req.onreadycapacitychange = function() {
  31.                 if (req.readycapacity == 4) {
  32.                     // only if "OK"
  33.                     if (req.status == 200) {                       
  34.                         document.getElementById('capacitydiv').innerHTML=req.responseText;           
  35.                     } else {
  36.                         alert("There was a problem while using XMLHTTP:\n" + req.statusText);
  37.                     }
  38.                 }            
  39.             }         
  40.             req.open("GET", strURL, true);
  41.             req.send(null);
  42.         }      
  43.     }
  44.     function getItems(categoryId,capacityId) {   
  45.         var strURL="includes/finditems.php?category="+categoryId+"&capacity="+capacityId;
  46.         var req = getXMLHTTP();
  47.        
  48.         if (req) {
  49.            
  50.             req.onreadycapacitychange = function() {
  51.                 if (req.readycapacity == 4) {
  52.                     // only if "OK"
  53.                     if (req.status == 200) {                       
  54.                         document.getElementById('itemsdiv').innerHTML=req.responseText;      
  55.                     } else {
  56.                         alert("There was a problem while using XMLHTTP:\n" + req.statusText);
  57.                     }
  58.                 }            
  59.             }         
  60.             req.open("GET", strURL, true);
  61.             req.send(null);
  62.         }
  63.                
  64.     }
findcapacity.php
PHP Code:

<?
    $category
=intval($_GET['category']);
    
//Making the connection to the DB
    
include('../../../_admin/db_connection/connectToDB.php');
    
//Connection To DB Through This Function
    
connectToDB();
?>

<select name="capacity" onchange="getItems(<?=$category?>,this.value)"><?=capacity($capacity)?></select>
finditems.php
PHP Code:

<?
    $categoryId
=intval($_GET['category']);
    
$capacityId=intval($_GET['capacity']);
    
//Making the connection to the DB
    
include('../../../_admin/db_connection/connectToDB.php');
    
//Connection To DB Through This Function
    
connectToDB();
    
?>
<select name="items" size="11"><?=inventory($category,$capacity,$items,$qtetype)?></select>
Thank in advance for your help.
Problem: I cant get the second drop down to populate.
__________________
We will see what happens next.

Last edited by Nico; 05-10-08 at 02:27 AM.
Reply With Quote
  #2 (permalink)  
Old 05-10-08, 07:13 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I couldn't get your ajaxFunctionItem.js to work unless I changed it like this:

Javascript Code:
  1. function getXMLHTTP() { //fuction to return the xml http object
  2.         var xmlhttp=false;
  3.         try{
  4.             xmlhttp=new XMLHttpRequest();
  5.         }
  6.         catch(e)    {
  7.             try{
  8.                 xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
  9.             }
  10.             catch(e){
  11.                 try{
  12.                 req = new ActiveXObject("Msxml2.XMLHTTP");
  13.                 }
  14.                 catch(e1){
  15.                     xmlhttp=false;
  16.                 }
  17.             }
  18.         }
  19.  
  20.         return xmlhttp;
  21.     }
  22.  
  23.     function getCapacity(categoryId) {
  24.  
  25.         var strURL="includes/findcapacity.php?category="+categoryId;
  26.         var req = getXMLHTTP();
  27.  
  28.         if (req) {
  29.  
  30.             req.onreadystatechange = function() {
  31.                 if (req.readyState == 4) {
  32.                     // only if "OK"
  33.                     if (req.status == 200) {
  34.                         document.getElementById('capacitydiv').innerHTML=req.responseText;
  35.                     } else {
  36.                         alert("There was a problem while using XMLHTTP:\n" + req.statusText);
  37.                     }
  38.                 }
  39.             }
  40.             req.open("GET", strURL, true);
  41.             req.send(null);
  42.         }
  43.     }
  44.     function getItems(categoryId,capacityId) {
  45.         var strURL="includes/finditems.php?category="+categoryId+"&capacity="+capacityId;
  46.         var req = getXMLHTTP();
  47.  
  48.         if (req) {
  49.  
  50.             req.onreadystatechange = function() {
  51.                 if (req.readyState == 4) {
  52.                     // only if "OK"
  53.                     if (req.status == 200) {
  54.                         document.getElementById('itemsdiv').innerHTML=req.responseText;
  55.                     } else {
  56.                         alert("There was a problem while using XMLHTTP:\n" + req.statusText);
  57.                     }
  58.                 }
  59.             }
  60.             req.open("GET", strURL, true);
  61.             req.send(null);
  62.         }
  63.  
  64.     }

Also I noticed you are using short PHP tags in your PHP code.
I would suggest not doing this, as most commercial servers do not allow it.
__________________
Jerry Broughton
Reply With Quote
  #3 (permalink)  
Old 05-12-08, 07:58 AM
soloWebDev soloWebDev is offline
Wannabe Coder
 
Join Date: Apr 2007
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you for the advise on the short tags, Ill give the above a shot.
__________________
We will see what happens next.
Reply With Quote
  #4 (permalink)  
Old 05-12-08, 08:26 AM
soloWebDev soloWebDev is offline
Wannabe Coder
 
Join Date: Apr 2007
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
[solved]
Works Great Thank you for your help.
__________________
We will see what happens next.
Reply With Quote
  #5 (permalink)  
Old 05-12-08, 08:34 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Quote:
Originally Posted by soloWebDev View Post
[solved]
Follow the link in my signature to see how to mark your own threads as [SOLVED].
Reply With Quote
  #6 (permalink)  
Old 05-12-08, 06:17 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
My Pleasure!!!
__________________
Jerry Broughton
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 10:17 AM
PHP and AJAX pdaniel2006 PHP 1 09-06-06 12:50 AM
help with multiple select used with php and mysql isaacmlee PHP 2 10-15-04 01:34 PM
Triangle Solutions Ltd presents PHP Multiple Newsletters usmwf PHP 0 03-26-04 09:29 AM


All times are GMT -5. The time now is 11:38 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.