5 Replies - 1831 Views - Last Post: 24 April 2012 - 04:41 AM Rate Topic: -----

#1 adamsalam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-April 12

Drop down menus dependency PHP/MySQL

Posted 23 April 2012 - 11:48 AM

Hi ,

this is my first post, i hope I can _GET and _POST what can be considered good contribution in this forum..

I am designing a registration form, this form has some drop down about 2 of them depending on each other. I am using PHP / MySQL
the drop down items are saved in MySQL and the form entries should be inserted in a main table. how I can accomplish that, I tried to adopt some of the code but seem i should create my own from scratch whis is impossible in my level of experience:(

the form is as following:


<form action="the_action.php" method="post">


Full Name<br/>

<input type="text" id="Full Name" name="Full Name" value="Full name" size="30" maxlength="1024"/><br/><br/>

Date of birth<br/>
Day:<input type="text" id="dob" name="dob" value="DD" size="2" maxlength="2"/>
Month:<input type="text" id="mob" name="mob" value="MM" size="2" maxlength="2"/>
Year:<input type="text" id="yob" name="yob" value="YYYY" size="4" maxlength="4"/>

<br/><br/>

Sex<br/><select id="Sex" name="Sex" size="1">

<option value="Male">Male</option>

<option value="Female">Female</option>

</select><br/><br/>

Highest Degree<br/>

<select id="Highest Degree" name="Highest Degree" size="1">

<option value="Certification">Certification</option>

<option value="Higher Diploma">Higher Diploma</option>

<option value="BSc">BSc</option>

<option value="Masters">Masters</option>

<option value="PhD ">PhD </option>

</select><br/><br/>

Scientific Discipline<br/><br/>

<select id="Scientific Discipline" name="Scientific Discipline" size="1">

<option value="Applied Mathematics">Applied Mathematics</option>

<option value="Economics">Economics</option>

<option value="Engineering">Engineering</option>

<option value="Humanity">Humanity</option>

<option value="Natural">Natural</option>

<option value="Medical">Medical</option>

</select><br/><br/>

Precise Speciality<br/>

<select id="Precise Speciality" name="Precise Speciality" size="1">

<option value="One">One</option>

<option value="Two">Two</option>

<option value="Three">Three</option>

</select><br/><br/>

Current Job<br/>

<select id="Current Job" name="Current Job" size="1">

<option value="Academic">Academic</option>

<option value="Professional">Professional</option>

</select><br/><br/>

Years of job experience<br/>
<select id="Years of job experience" name="Years of job experience" size="1">

<option value="0">0</option>

<option value="1-2">1-2</option>

<option value="3-5">3-5</option>

<option value="5-10">5-10</option>

<option value="More than 10">More than10</option>

</select><br/><br/>

Current place of work<br/>

<select id="Current place of work" name="Current place of work" size="1">

<option value="Libya">Libya</option>

<option value="Abroad">Abroad</option>

</select><br/><br/>

Email contact<br/>

<input type="text" id="Email contact" name="Email contact" value="email address" size="30" maxlength="1024"/><br/><br/>

Skype contact<br/>

<input type="text" id="Skype contact" name="Skype contact" value="" size="30" maxlength="1024"/><br/><br/>

Telephone contact<br/>

<input type="text" id="Telephone contact" name="Telephone contact" value="" size="30" maxlength="1024"/><br/><br/>

Facebook URL<br/>

<input type="text" id="Facebook URL" name="Facebook URL" value="" size="30" maxlength="1024"/><br/><br/>

Like to contribute to GLASP?<br/>

<select id="Like to contribute to GLASP?" name="Like to contribute to GLASP?" size="1">

<option value="Yes">Yes</option>

<option value="No">No</option>

</select><br/><br/>

<input type="submit" id="Register for GLASP" name="Register for GLASP" value="Register!"/><br/><br/>

<input id="form_id" type="hidden" name="form_id" value="189757"/><br/>

</form>



Is This A Good Question/Topic? 0
  • +

Replies To: Drop down menus dependency PHP/MySQL

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5662
  • View blog
  • Posts: 22,507
  • Joined: 23-August 08

Re: Drop down menus dependency PHP/MySQL

Posted 23 April 2012 - 05:36 PM

There is no PHP in your code.

We have lots of tutorials, including using dropdowns and MySQL.
Was This Post Helpful? 0
  • +
  • -

#3 adamsalam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-April 12

Re: Drop down menus dependency PHP/MySQL

Posted 24 April 2012 - 03:19 AM

View PostJackOfAllTrades, on 23 April 2012 - 05:36 PM, said:

There is no PHP in your code.

We have lots of tutorials, including using dropdowns and MySQL.


thank you for your reply, in fact I have the PHP code (which should be matching the HTML Form Above after testing, [Please see below] and some of the associated database Tables for the initiation of the code please see the screen shots below:
<?php
//DECLARE VARIABLES USED IN THE FORM  <SELECT >
$branch = null;
$speciality = null;
$expertise = null;

$conn = mysql_connect('localhost', 'username', 'password');
$db = mysql_select_db('glaspmembership',$conn); // glaspmembership = DATABASE NAME

if(isset($_GET["branch"]) && is_numeric($_GET["branch"])) // [1ST VARIABLE NAME DECLARED ABOVE]
{
    $branch = $_GET["branch"]; 
}

if(isset($_GET["speciality"]) && is_numeric($_GET["speciality"])) // [2ND VARIABLE NAME DECLARED ABOVE]
{
    $speciality = $_GET["speciality"]; 
}

if(isset($_GET["expertise"]) && is_numeric($_GET["expertise"])) // [3RD VARIABLE NAME DECLARED ABOVE]
{
    $expertise = $_GET["expertise"]; 
}

?>

<script language="Javascript">

function autoSubmit()
{
    var formObject = document.forms['theForm'];
    formObject.submit();
}

</script>

<form name="theForm" method="get">

    <!-- branch SELECTION , in later development it should fetched from the db -->
    
    <select name="branch" onchange="autoSubmit();">
        <option value="null"></option>
        <option value="1" <?php if($branch == 1) echo " selected"; ?>>THE ASSOCIATION OF MEDICAL SCIENCES (BMS)</option>
        <option value="2" <?php if($branch == 2) echo " selected"; ?>>THE ASSOCIATION OF NATURAL SCIENCES (BNS)</option>
		<option value="3" <?php if($branch == 3) echo " selected"; ?>>THE ASSOCIATION OF ENGINEERING SCIENCES (BES)</option>
		<option value="4" <?php if($branch == 4) echo " selected"; ?>>THE ASSOCIATION OF ECONOMICS & APPLIED MATHEMATICAL SCIENCES (BEAMS)</option>
		<option value="5" <?php if($branch == 5) echo " selected"; ?>>THE ASSOCIATION OF HUMANITY SCIENCES (BHS)</option>
    </select>

    <br><br>

    <!-- speciality SELECTION BASED ON branch VALUE -->
        
    <?php
    
    if($branch != null && is_numeric($branch))
    {
        ?>
        <select name="speciality" onchange="autoSubmit();">        
        <option value="null"></option>
        
        <?php
        
        //POPULATE DROP DOWN MENU WITH SPECIALITIES FROM A GIVEN branch
        
        $sql = "SELECT speciality_id, speciality FROM tab_bms WHERE branch_id = $branch";
        $specialities = mysql_query($sql,$conn);
        
        while($row = mysql_fetch_array($specialities))
        {        
            echo ("<option value=\"$row[speciality_id]\" " . ($speciality == $row["speciality_id"] ? " selected" : "") . ">$row[speciality]</option>");        
        }
        
        ?>
        
    </select>
    
    <?php
    }
    
    ?>
    
    <br><br>
    
    <?php
    
    if($speciality != null && is_numeric($speciality) && $branch != null)
    {
    
    ?>
    
    <select name="expertise" onchange="autoSubmit();">
        <option value="null"></option>
        
        <?php
        
        //POPULATE DROP DOWN MENU WITH expertises FROM A GIVEN branch, speciality
        
         $sql = "SELECT expertise_id, expertise FROM tab_bmse WHERE speciality_id = $specilaity";
        $expertises = mysql_query($sql,$conn);
        
        while($row = mysql_fetch_array($expertises))
        {        
            echo ("<option value=\"$row[expertise_id]\" " . ($expertise == $row["expertise_id"] ? " selected" : "") . ">$row[expertise]</option>");        
        }
        
        ?>    
        
    </select>
    
    <?php
    
    }
    
    ?>
    
    <br><br>
    
  </form>



sorry I could attach images!!! so here the tables description:



mysql> describe tab_branches;
+-----------+-------------+------+-----+-------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+-------------+-------+
| branch_ID | int(2) | NO | | 0 | |
| branch | varchar(80) | NO | | branch_name | |
+-----------+-------------+------+-----+-------------+-------+

mysql> describe TAB_BMS;
+---------------+-------------+------+-----+----------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+----------------+-------+
| speciality_id | int(3) | NO | | 0 | |
| speciality | varchar(60) | NO | | bms_speciality | |
| branch_id | int(2) | NO | | 0 | |
+---------------+-------------+------+-----+----------------+-------+


mysql> describe TAB_BMSE;
+---------------+-------------+------+-----+---------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------------+-------+
| EXPERTISE_id | int(3) | NO | | 0 | |
| EXPERTISE | varchar(60) | NO | | bms_EXPERTISE | |
| SPECIALITY_id | int(2) | NO | | 0 | |
+---------------+-------------+------+-----+---------------+-------+

now when I access the ACTION_FILE.PHP I get two dropdown menu instead of three
when I select the first option the dropdown displyed and associated rows are populated in the second dropdown from the associated table TAB_BMS
then when I select the option from the second dropdown NO drop menu displayed and the browser KEEP fetching for ever!

could you please provide help in the same?

thank you in advance
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Original Digital Gansta
  • member icon

Reputation: 4462
  • View blog
  • Posts: 24,906
  • Joined: 10-May 07

Re: Drop down menus dependency PHP/MySQL

Posted 24 April 2012 - 03:25 AM

You may not have the 3rd drop down because of your logic : see if this outputs from the else statement :

    <?php
    
    if($speciality != null && is_numeric($speciality) && $branch != null)
    {
    
    ?>
    
    <select name="expertise" onchange="autoSubmit();">
        <option value="null"></option>
        
        <?php
        
        //POPULATE DROP DOWN MENU WITH expertises FROM A GIVEN branch, speciality
        
         $sql = "SELECT expertise_id, expertise FROM tab_bmse WHERE speciality_id = $specilaity";
        $expertises = mysql_query($sql,$conn);
        
        while($row = mysql_fetch_array($expertises))
        {        
            echo ("<option value=\"$row[expertise_id]\" " . ($expertise == $row["expertise_id"] ? " selected" : "") . ">$row[expertise]</option>");        
        }
        
        ?>    
        
    </select>
    
    <?php
    
    }
    else {
	    echo "No 3rd drop down...";
    }
    
    ?>


This is just a guess, but if your 2nd is running 'forever', it could be because you manually start the select in html, but your php is dependent on a result that you didn't validate existed :

        <?php
        
        //POPULATE DROP DOWN MENU WITH SPECIALITIES FROM A GIVEN branch
        
        $sql = "SELECT speciality_id, speciality FROM tab_bms WHERE branch_id = $branch";
        $specialities = mysql_query($sql,$conn);
        if(!$specialities) {
	        echo "<p>The following SQL Failed</p>";
	        echo "<p>$branch</p>";
	        die();
        }
        
        while($row = mysql_fetch_array($specialities))
        {        
            echo ("<option value=\"$row[speciality_id]\" " . ($speciality == $row["speciality_id"] ? " selected" : "") . ">$row[speciality]</option>");        
        }
        
        ?>
        
    </select>
    
    <?php
    }
    
    ?>

Was This Post Helpful? 0
  • +
  • -

#5 adamsalam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-April 12

Re: Drop down menus dependency PHP/MySQL

Posted 24 April 2012 - 04:35 AM

THANK YOU,

the first drop working and fetching the second dropmenu items perfectly...
its when i SELECT from the second drop no dropdown appear and the browser keep fetchung forever
lease see the screenshot attcahed...


could lt be database side problem,

sorry I am new to PHP :(

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#6 adamsalam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-April 12

Re: Drop down menus dependency PHP/MySQL

Posted 24 April 2012 - 04:41 AM

dear if you like to test I will send you all the associated scripts .....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1