错误的数据被保存从AJAX下拉菜单



我一直在努力解决一个问题,我的一个数据字段是如何保存到mySQL数据库的。我的问题是,而不是被保存的文本值,文本值的"id"被保存,我不知道为什么。

该字段称为'detectorname',通过下拉菜单选择该字段的文本值,该下拉菜单依次打开第二个下拉菜单,称为searchheadname。这两个下拉菜单都是通过PHP和AJAX脚本填充的,并在用户选择时显示正确的文本值。

我很感激我没有包含任何代码,有太多了,我不得不承认我不确定哪些部分会有用。

我只是想知道是否有人可以帮助我解决这个问题,请让我知道他们希望我发布的代码的哪一部分。

HTML Form &AJAX脚本

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Location</title>
        <link rel="stylesheet" href="css/style.css" type="text/css" media="all" />
    link rel="stylesheet"   href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/base/jquery-ui.css" 
        type="text/css" media="all" />  
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>  
        <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js" type="text/javascript"></script>         
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=en"></script>  
        <script type="text/javascript" src="js/addfindstolocation.js"></script>
        <script type="text/javascript" language="javascript">
            <!--
            function savedata() { 
            var userid =  document.getElementById("userid").value;
            var locationid = document.getElementById("locationid").value; 
            var searchheadid = document.getElementById("searchheadid").value;
            var additionalcomments = document.getElementById("additionalcomments").value;            
            var url = "addfindstolocationsave.php?userid=" + userid + "&locationid=" + locationid + "&searchheadid=" + searchheadid + "&additionalcomments=" + additionalcomments; 
            downloadUrl(url, function(data, responseCode) { 
            }); 
            } 
            function downloadUrl(url, callback) { 
            var request = window.ActiveXObject ? 
            new ActiveXObject('Microsoft.XMLHTTP') : 
            new XMLHttpRequest; 
            request.onreadystatechange = function() { 
            if (request.readyState == 4) { 
            request.onreadystatechange = doNothing; 
            callback(request.responseText, request.status); 
            } 
            }; 
            request.open('GET', url, true); 
            request.send(null); 
            } 
            function doNothing() {}
            function MM_callJS(jsStr) { //v2.0
            return eval(jsStr)
            }
            //-->
        </script>
        <script type="text/javascript" language="javascript">
            $(function() {
            $( "#dateoftrip" ).datepicker({ 
            dateFormat:'dd/mm/yy',
            showOn: 'button',  
            buttonText: 'Select Date...'});
            });
        </script>
        <script type="text/javascript">
            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);
            }
            </script>
            </head>
            <body>
                <form name="addfindstolocation" method='POST'>
                    <p><label></label>
                    </p>
                    <p align="left">
                        <input name="userid" type="text" id="userid" value="1"/>
                        <input name="locationid" type="text" id="locationid"  value="2"/>
                        <br />
                    </p>
                    <div>
                        <label>
                            <div align="left">Click on the map to place the marker for the find that has been made and drag until the precise location has been found. </div>
                        </div>
                            <p align="left"><label>Find OSGB36 Latitude Co-ordinate<br />
                                </label>
                            </p>
                            <div>
                                <div align="left">
                                    <input name="findosgb36lat" type="text" id="findosgb36lat" size="20" />
                                </div>
                            </div>
                            <p align="left"><label>Find OSGB36 Longitude Co-ordinate<br />
                                </label>
                            </p>
                            <div>
                                <div align="left">
                                    <input name="findosgb36lon" type="text" id="findosgb36lon" size="20" />
                                </div>
                            </div>
                            <p align="left"><label>Date of Trip<br />
                                </label>
                            </p>
                            <div>
                                <div align="left">
                                    <input name="dateoftrip" type="text" id="dateoftrip" size="10" />
                                </div>
                            </div>
                            <p align="left"><label>Find Category</label>&nbsp;</p>
                            <div>
                                <div align="left">
                                    <?php
                                    mysql_connect("hostname", "username", "password") or die("Connection Failed");
                                    mysql_select_db("dbname")or die("Connection Failed");
                                    $query = "SELECT * FROM findcategories";
                                    $result = mysql_query($query);
                                    ?>
                                    <select name="findcategory" id="findcategory">
                                        <option value=''>Select One</option>
                                        <?php
                                        while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
                                        ?>
                                        <option value="<?php echo $line['findcategory'];?>"> <?php echo $line['findcategory'];?> </option>
                                        <?php
                                        }
                                        ?>
                                    </select>    
                                </div>
                                <p align="left">
                                    <label>Find Name<br />
                                    </label>
                                </p>
                                <div>
                                    <div align="left">
                                        <input name="findname" type="text" id="findname" size="35" />
                                    </div>
                                </div>
                                <p align="left">
                                    <label>Find Description<br />
                                    </label>
                                </p>
                                <div>
                                    <div align="left">
                                        <input name="finddescription" type="text" id="finddescription" size="150" />
                                    </div>
                                </div>
                                <p align="left"><label></label>
                                    <label>Detector Used</label></p>
                                <div>
                                    <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>
                                </div>
                                <p align="left">
                                    <label>Detector Settings</label>
                                    <label><br />
                                    </label>
                                </p>
                                <div>
                                    <div align="left">
                                        <textarea name="detectorsettings" cols="50" rows="12" id="detectorsettings"></textarea>
                                    </div>
                                </div>
                                <p align="left">
                                    <label>PAS Ref. (if known)<br />
                                    </label>
                                </p>
                                <div>
                                    <div align="left">
                                        <input name="pasref" type="text" id="pasref" size="9" />
                                    </div>
                                </div>
                                <p align="left"><label>Additional Comments</label>
                                </p>
                                <div>
                                    <div align="left">
                                        <textarea name="additionalcomments" cols="50" rows="12" id="additionalcomments"></textarea>
                                    </div>
                                </div>
                                <p align="left"><br />
                                    <label></label>
                                </p>
                                <p align="left">
                                    <input name="submit" type="submit" onclick="MM_callJS('savedata()')" value="Submit" />        
                                </form>
                                    <div id="map"></div>
                                </body>
                                </html>

**'Searchheadetails.php' File (Called in file above)**     
     <?
$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)";
?>  

我会尝试在下面一行代码中将detectorid更改为detectorname:

<div>
<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[detectorname]>$n[detectorname]</option>";
//***I changed the line above***  TODO:Remove this comment
}
?>
</select>
</div>
</div>

最新更新