• We just launched and are currently in beta. Join us as we build and grow the community.

Populate Multiple Select Options from database

geee

Monetization Workflow Designer
G Rep
0
0
0
Rep
0
G Vouches
0
0
0
Vouches
0
Posts
80
Likes
26
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Submitted by
OzReaction

on
Thu, 01/09/2014 - 18:53

I am hoping someone can point me in the right direction.
I have a need to create a form that requires user selection of choices in 3 SELECT drop down lists.
I have trawled the web forums and youtube etc to try and find a resolution to my problems... tried multiple so called resolutions and have yet to achieve success.

Whether it is something to do with my code or the format of my database I do not know.

Please be kind as I am only a VERY novice programmer and will appreciate any assistance offered.

I have 3 files....
index.php (Form)
loadsubcat.php (PHP processing)
config.php (db connection)

The basics of the files were "borrowed" from user kabiru10 (thank you).
Using kabiru10 "Creating a Dependent Dropdown List with PHP, jQuery and Ajax" example code everything works. It's once I change databases and tables I get stuck and it breaks.

======= CONTENTS of index.php ---------------

  1. <?php
  2. include

    (

    'config.php'

    )

    ;
  3. $query_parent

    =

    mysql_query

    (

    "SELECT DISTINCT State FROM Locations"

    )

    or die

    (

    "Query failed: "

    .

    mysql_error

    (

    )

    )

    ;
  4. ?>
  5. <!doctype html>
  6. <html>
  7. <head>
  8. <meta charset="utf-8">
  9. <title>Dependent DropDown List</title>
  10. <script type="text/javascript" src="js/jquery.js"></script>
  11. <script type="text/javascript">
  12. $(document).ready(function() {
  13. $("#parent_cat").change(function() {
  14. $.get('loadsubcat.php?parent_cat=' + $(this).val(), function(data) {
  15. $("#sub_cat").html(data);
  16. $('#loader').slideUp(200, function() {
  17. $(this).remove();
  18. });
  19. });
  20. });
  21. });
  22. </script>
  23. </head>
  24. <body>
  25. <form method="get">
  26. <label for="category">Parent Category</label>
  27. <select name="parent_cat" id="parent_cat">
  28. <option>Select State</option>
  29. <?php

    while

    (

    $row

    =

    mysql_fetch_array

    (

    $query_parent

    )

    )

    :

    ?>
  30. <option value="<?php

    echo

    $row

    [

    'ID'

    ]

    ;

    ?>

    "><?php

    echo

    $row

    [

    'State'

    ]

    ;

    ?>

    </option>
  31. <?php

    endwhile

    ;

    ?>
  32. </select>
  33. <br/><hr/>

  34. <label>Sub Category</label>
  35. <select name="sub_cat" id="sub_cat">
  36. <option>Select Suburb</option>
  37. </select>
  38. </form>
  39. </body>
  40. </html>

====== Contents of loadsubcat.php ===============
  1. <?php
  2. include

    (

    'config.php'

    )

    ;

  3. $parent_cat

    =

    $_GET

    [

    'parent_cat'

    ]

    ;

  4. $query

    =

    mysql_query

    (

    "SELECT Area FROM Locations WHERE ID = {$parent_cat}

    "

    )

    ;
  5. while

    (

    $row

    =

    mysql_fetch_array

    (

    $query

    )

    )

    {
  6. echo

    "<option value='$row[Area]

    '>$row[Area]

    </option>"

    ;
  7. }

  8. ?>

====== Contents of config.php ==================
Not Provided
=======================================

====== Partial Database Dump ======================
-- phpMyAdmin SQL Dump
-- version 4.0.8
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 09, 2014 at 11:47 AM
-- Server version: 5.5.29
-- PHP Version: 5.4.10

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Database: `ozr48210_FitFirst`
--

-- --------------------------------------------------------

--
-- Table structure for table `Locations`
--

  1. CREATE

    TABLE

    `Locations`

    (
  2. `State`

    VARCHAR

    (

    3

    )

    DEFAULT

    NULL

    ,
  3. `Area`

    VARCHAR

    (

    23

    )

    DEFAULT

    NULL

    ,
  4. `ClubID`

    INT

    (

    5

    )

    NOT

    NULL

    DEFAULT

    '0'

    ,
  5. `ClubName`

    VARCHAR

    (

    33

    )

    DEFAULT

    NULL

    ,
  6. `ClubType`

    VARCHAR

    (

    8

    )

    DEFAULT

    NULL

    ,
  7. `Lattitude`

    DECIMAL

    (

    10

    ,

    7

    )

    DEFAULT

    NULL

    ,
  8. `Longditude`

    DECIMAL

    (

    10

    ,

    7

    )

    DEFAULT

    NULL

    ,
  9. `ID`

    INT

    (

    1

    )

    DEFAULT

    NULL

    ,
  10. PRIMARY

    KEY

    (

    `ClubID`

    )

    ,
  11. UNIQUE

    KEY

    `ClubID`

    (

    `ClubID`

    )
  12. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    utf8;

  13. --
  14. -- Dumping data for table `Locations`
  15. --

  16. INSERT

    INTO

    `Locations`

    (

    `State`

    ,

    `Area`

    ,

    `ClubID`

    ,

    `ClubName`

    ,

    `ClubType`

    ,

    `Lattitude`

    ,

    `Longditude`

    ,

    `ID`

    )

    VALUES
  17. (

    'NSW'

    ,

    'Sydney West'

    ,

    52958

    ,

    'Auburn'

    ,

    'Passport'

    ,

    -

    33.8474460

    ,

    151.0455480

    ,

    2

    )

    ,
  18. (

    'WA'

    ,

    'Balga'

    ,

    52977

    ,

    'Balga'

    ,

    'Passport'

    ,

    -

    31.8477860

    ,

    115.8245160

    ,

    5

    )

    ,
  19. (

    'NSW'

    ,

    'Sydney Northern Beaches'

    ,

    52998

    ,

    'Balgowlah'

    ,

    'Platinum'

    ,

    -

    33.7931690

    ,

    151.2643430

    ,

    2

    )

    ,
  20. (

    'NSW'

    ,

    'Sydney West'

    ,

    53017

    ,

    'Bankstown'

    ,

    'Passport'

    ,

    -

    33.9320540

    ,

    151.0295710

    ,

    2

    )

    ,
  21. (

    'VIC'

    ,

    'Melbourne South East'

    ,

    53037

    ,

    'Bayside'

    ,

    'Passport'

    ,

    -

    37.9546882

    ,

    145.0312471

    ,

    4

    )

    ,
  22. (

    'NSW'

    ,

    'Sydney Eastern Suburbs'

    ,

    53064

    ,

    'Bondi (The Edge)'

    ,

    'Passport'

    ,

    -

    33.8922310

    ,

    151.2477680

    ,

    2

    )

    ,
  23. (

    'NSW'

    ,

    'Sydney Eastern Suburbs'

    ,

    53083

    ,

    'Bondi'

    ,

    'Platinum'

    ,

    -

    33.8923240

    ,

    151.2473300

    ,

    2

    )

    ,
  24. (

    'VIC'

    ,

    'Melbourne South East'

    ,

    53106

    ,

    'Brighton'

    ,

    'Passport'

    ,

    -

    37.9092540

    ,

    145.0119030

    ,

    4

    )

    ,
  25. (

    'QLD'

    ,

    'Brisbane Central'

    ,

    53136

    ,

    'Brisbane CBD - Brisbane City'

    ,

    'Passport'

    ,

    -

    27.4647970

    ,

    153.0290020

    ,

    3

    )

    ,
  26. (

    'ACT'

    ,

    'Canberra'

    ,

    53197

    ,

    'Canberra City'

    ,

    'Passport'

    ,

    -

    35.2798580

    ,

    149.1337130

    ,

    1

    )

    ,
  27. (

    'WA'

    ,

    'Cannington'

    ,

    53212

    ,

    'Cannington'

    ,

    'Passport'

    ,

    -

    32.0188900

    ,

    115.9347500

    ,

    5

    )

    ,
  28. (

    'QLD'

    ,

    'Brisbane South'

    ,

    53241

    ,

    'Carindale'

    ,

    'Passport'

    ,

    -

    27.5170800

    ,

    153.0980070

    ,

    3

    )

    ,
  29. (

    'NSW'

    ,

    'Sydney North West'

    ,

    53267

    ,

    'Carlingford'

    ,

    'Passport'

    ,

    -

    33.7778210

    ,

    151.0520690

    ,

    2

    )

    ,
  30. (

    'NSW'

    ,

    'Sydney North West'

    ,

    53292

    ,

    'Castle Hill - Castle Towers'

    ,

    'Passport'

    ,

    -

    33.7328193

    ,

    151.0049599

    ,

    2

    )

    ,
  31. (

    'VIC'

    ,

    'Melbourne South East'

    ,

    53320

    ,

    'Chadstone'

    ,

    'Passport'

    ,

    -

    37.8878763

    ,

    145.0952034

    ,

    4

    )

    ,
  32. (

    'VIC'

    ,

    'Melbourne South East'

    ,

    53336

    ,

    'Malvern Valley'

    ,

    'Passport'

    ,

    -

    37.8773090

    ,

    145.0829280

    ,

    4

    )

    ,
  33. (

    'NSW'

    ,

    'Sydney North Shore'

    ,

    53360

    ,

    'Chatswood'

    ,

    'Platinum'

    ,

    -

    33.7957960

    ,

    151.1821880

    ,

    2

    )

    ,
  34. (

    'WA'

    ,

    'Perth CBD'

    ,

    53391

    ,

    'Brookfield Place'

    ,

    'Platinum'

    ,

    -

    31.9544740

    ,

    115.8552560

    ,

    5

    )

    ;

=======================================================

 

442,401

317,942

317,951

Top