Jump to content

PHP Drop Down Menu


hobbiton73

Recommended Posts

Hi, i wonder whether someone may be able to help me please.

 

I am using a combination of PHP and AJAX to create two drop down menus on a HTML form. The data is being pulled from a mySQL database with the options available in the second drop down dependent on the value selected in the first.

 

The initial drop down menu called 'detectors' and the behaviours for the second drop down menu, 'searchheads' are created with the following AJAX code:

 

Function AjaxFunction(detectorid)
            {
            var httpxml;
            try
            {
            // Firefox, Opera 8.0+, Safari
            httpxml=new XMLHttpRequest();
            }
            catch (e)
            {
            // Internet Explorer
            try
            {
            httpxml=new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
            try
            {
            httpxml=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
            alert("Your browser does not support AJAX!");
            return false;
            }
            }
            }
            function stateck() 
            {
            if(httpxml.readyState==4)
            {
            
            var myarray=eval(httpxml.responseText);
            // Before adding new we must remove previously loaded elements
            for(j=document.addfindstolocation.searchheads.options.length-1;j>=0;j--)
            {
            document.addfindstolocation.searchheads.remove(j);
            }
            
            
            for (i=0;i<myarray.length;i++)
            {
            var optn = document.createElement("OPTION");
            optn.text = myarray[i];
            optn.value = myarray[i];
            document.addfindstolocation.searchheads.options.add(optn);
            
            } 
            }
            }
            var url="searchheaddetails.php";
            url=url+"?detectorid="+detectorid;
            url=url+"&sid="+Math.random();
            httpxml.onreadystatechange=stateck;
            httpxml.open("GET",url,true);
            httpxml.send(null);
            }

 

The following code is the file 'searchheaddetails.php' (as highlighted above) which populates the second drop down menu.

 

<?
$detectorid=$_GET['detectorid'];
require "config.php";
$q=mysql_query("SELECT * FROM searchheads WHERE detectorid='$detectorid' ORDER BY 'searchheadname' ASC");
echo mysql_error();
$myarray=array();
$str="";
while($nt=mysql_fetch_array($q)){
$str=$str . "\"$nt[searchheadname]\",";
}
$str=substr($str,0,(strLen($str)-1)); // Removing the last char , from the string
echo "new Array($str)";

?>

 

And this is the section of my form that pulls together the two drop down menus.

 

<form name="addfindstolocation" method="post" id="addfindstolocation">
<div align="left">
                                        <select name=detectors id="detectorid" onchange="AjaxFunction(this.value);">
                                            <option value=''>Select One</option>
                                            <?
                                            require "phpfile.php";// connection to database 
                                            $q=mysql_query("SELECT * from detectors WHERE userid='1'ORDER BY 'detectorname' ASC");
                                            while($n=mysql_fetch_array($q)){
                                            echo "<option value=$n[detectorid]>$n[detectorname]</option>";
                                            }
                                            
                                            ?>
                                      </select>
                                    </div>
                                </div>
                                <p align="left">
                                    <label></label>
                                    <label>Search Head Used</label></p>
                                <div>
                                    <div align="left">
                                        <select name=searchheads id="searchheadid">
                                        </select>
                                    </div>

 

The drop down menus work fine, but I'm having a little difficulty with the data that is being saved. For the 'detectors' drop down menu the data being saved upon a selection being made is the 'id' pertient to the relevant detector e.g. 'Detector1' is selected and the 'id' value of '1' is saved which is exactly what I want. However when it comes to the second drop down menu, the value saved is the text value that the user selects, rather than the 'id'. e.g. 'Deep Search Head ' rather than an 'id' of '1'. Could someone perhaps tell me please what I need to change so that the 'id' value is saved rather than the text value.

 

If it helps, the coding is taken from the following http://www.plus2net.com/php_tutorial/ajax_drop_down_list.php.

 

Many thanks and kind regards.

 

Chris

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.