2010-11-15 66 views
0

我有一個頁面上下面的JavaScript函數:拆分數據未在FF

function setFields(){ 
var menu = document.getElementById('EditLocation'); 
var itemDataArray = menu[menu.selectedIndex].value.split('|'); 
form.LocationShortName.value = itemDataArray[0]; 
form.LocationLongName.value = itemDataArray[1]; 
form.Phone.value = itemDataArray[2]; 
form.Address1.value = itemDataArray[3]; 
form.CityStateZip.value = itemDataArray[4]; 
form.MapLink.value = itemDataArray[5]; 
} 

下的形式,我有以下幾點:

<select class="input2" name="EditLocation" id="EditLocation" onchange = "setFields();"> 
        <option value="-Add New-"<?php if($editlocation=='-Add New-'){echo(' selected="selected"');} ?>>-Add New-</option> 
        <?php require_once('connection.php'); 

     $connection = mysql_connect($hostname,$username,$password) or die (mysql_errno().": ".mysql_error()."<BR />"); 
     mysql_select_db($database); 

     $sql = "SELECT * FROM directions ORDER BY dirshortname"; 

     $query = mysql_query($sql); 

     while ($row = mysql_fetch_array($query)) { 
      echo('<option value="'.stripslashes($row['dirshortname']).'|'.stripslashes($row['dirlongname']).'|'.stripslashes($row['dirphone']).'|'.stripslashes($row['dirstreet']).'|'.stripslashes($row['dircsz']).'|'.stripslashes($row['dirmaplink']).'"'); 
      if ($editlocation==stripslashes($row['dirshortname'])) 
      { 
       echo(' selected="selected"'); 
      } 
      echo('>'.stripslashes($row['dirshortname']).'</option>'); 
     } 
     ?> 

實質上,PHP應該將從MySQL中提取的數據元素打包到SELECT框的OPTION VALUE部分中。一旦用戶選擇了一條記錄,JavaScript就會將打包的數據分開並在FORM上填充其他數據元素。它在IE中的所有功能都非常好,但是在FF中這些字段不會填充數據。

表單有點長,但爲了完整起見,我會將其包含在內。

<form action="admin-dirs.php" method="post" enctype="multipart/form-data" style="margin:0px; padding:0px " id="form"> 
       <table width="587" border="0" cellspacing="0" cellpadding="0"> 
       <tr> 
        <td width="60">&nbsp;</td> 
        <td width="185">Select Location to Edit: </td> 
        <td width="342"><select class="input2" name="EditLocation" id="EditLocation" onchange = "setFields();"> 
        <option value="-Add New-"<?php if($editlocation=='-Add New-'){echo(' selected="selected"');} ?>>-Add New-</option> 
        <?php require_once('connection.php'); 

     $connection = mysql_connect($hostname,$username,$password) or die (mysql_errno().": ".mysql_error()."<BR />"); 
     mysql_select_db($database); 

     $sql = "SELECT * FROM directions ORDER BY dirshortname"; 

     $query = mysql_query($sql); 

     while ($row = mysql_fetch_array($query)) { 
      echo('<option value="'.stripslashes($row['dirshortname']).'|'.stripslashes($row['dirlongname']).'|'.stripslashes($row['dirphone']).'|'.stripslashes($row['dirstreet']).'|'.stripslashes($row['dircsz']).'|'.stripslashes($row['dirmaplink']).'"'); 
      if ($editlocation==stripslashes($row['dirshortname'])) 
      { 
       echo(' selected="selected"'); 
      } 
      echo('>'.stripslashes($row['dirshortname']).'</option>'); 
     } 
     ?> 
        </select></td> 
       </tr> 
       <tr> 
        <td width="60">&nbsp;</td> 
        <td colspan="2"><span class="main" style=" padding-left:12px; padding-right:12px; padding-top:6px"><br /> 
        (Note: 
      Leaving the Long Name blank will duplicate the Short Name.)</span></td> 
        </tr> 
       <?php if(!$errlocationshortname==''){echo(' 
       <tr> 
        <td width="60">&nbsp;</td> 
        <td width="185">&nbsp;</td> 
        <td width="342"><span class="redtxterror">'.$errlocationshortname.'</span></td> 
       </tr>');} ?> 
       <tr> 
        <td>&nbsp;</td> 
        <td>Location Short Name: <span class="red_star">*</span> </td> 
        <td><input name="LocationShortName" id="LocationShortName" type="text" class="input2<?php if(!$errlocationshortname==''){echo('r');} ?>" value="<?php echo($locationshortname); ?>" maxlength="50"></td> 
       </tr> 
       <?php if(!$errlocationlongname==''){echo(' 
       <tr> 
        <td width="60">&nbsp;</td> 
        <td width="185">&nbsp;</td> 
        <td width="342"><span class="redtxterror">'.$errlocationlongname.'</span></td> 
       </tr>');} ?> 
       <tr> 
        <td>&nbsp;</td> 
        <td>Location Long Name: <span class="red_star">*</span> </td> 
        <td><input name="LocationLongName" id="LocationLongName" type="text" class="input2<?php if(!$errlocationlongname==''){echo('r');} ?>" value="<?php echo($locationlongname); ?>" maxlength="50"></td> 
       </tr> 
       <?php if(!$erraddress==''){echo(' 
       <tr> 
        <td width="60">&nbsp;</td> 
        <td width="185">&nbsp;</td> 
        <td width="342"><span class="redtxterror">'.$erraddress.'</span></td> 
       </tr>');} ?> 
       <tr> 
        <td>&nbsp;</td> 
        <td>Street Address: <span class="red_star">*</span> </td> 
        <td><input name="Address1" id="Address1" type="text" class="input2<?php if(!$erraddress==''){echo('r');} ?>" value="<?php echo($address); ?>"></td> 
       </tr> 
       <?php if(!$errcsz==''){echo(' 
       <tr> 
        <td width="60">&nbsp;</td> 
        <td width="185">&nbsp;</td> 
        <td width="342"><span class="redtxterror">'.$errcsz.'</span></td> 
       </tr>');} ?> 
       <tr> 
        <td>&nbsp;</td> 
        <td>City, State, Zip: <span class="red_star">*</span> </td> 
        <td><input name="CityStateZip" id="CityStateZip" type="text" class="input2<?php if(!$errcsz==''){echo('r');} ?>" value="<?php echo($csz); ?>"></td> 
       </tr> 
       <?php if(!$errphone==''){echo(' 
       <tr> 
        <td width="60">&nbsp;</td> 
        <td width="185">&nbsp;</td> 
        <td width="342"><span class="redtxterror">'.$errphone.'</span></td> 
       </tr>');} ?> 
       <tr> 
        <td>&nbsp;</td> 
        <td>Location Phone Number: <span class="red_star">*</span> </td> 
        <td><input name="Phone" id="Phone" type="text" class="input2<?php if(!$errphone==''){echo('r');} ?>" value="<?php echo($phone); ?>" maxlength="20"></td> 
       </tr> 
       <?php if(!$errmaplink==''){echo(' 
       <tr> 
        <td width="60">&nbsp;</td> 
        <td width="185">&nbsp;</td> 
        <td width="342"><span class="redtxterror">'.$errmaplink.'</span></td> 
       </tr>');} ?> 
       <tr> 
        <td>&nbsp;</td> 
        <td>Paste Link to Map: <span class="red_star">*</span> </td> 
        <td><input name="MapLink" id="MapLink" type="text" class="input2<?php if(!$errmaplink==''){echo('r');} ?>" value="<?php echo($maplink); ?>" maxlength="125"></td> 
       </tr> 

       <tr> 
        <td>&nbsp;</td> 
        <td>&nbsp;</td> 
        <td><div align="right" style="padding-right:25px"> 
        <input type="hidden" id="action" name="action" value="submitform" /> 
        <input type="submit" id="savenew" name="savenew" value="Save & New" /> 
        <input type="submit" id="submit" name="submit" value="Save & Close" /> 
        <?php if(!isset($_POST['action'])) {?> 
        <input type="reset" id="reset" name="reset" value="Reset" /> 
        <?php } ?> 
        </div></td> 
       </tr><tr> 
        <td>&nbsp;</td> 
        <td>&nbsp;</td> 
        <td class="main_d"><div align="right" style="padding-right:25px">Your IP Address is Logged as: <?php echo($ip); ?></div></td> 
       </tr> 
       </table> 
       </form> 
+0

如果它在一個瀏覽器中工作,但不是另一個,它與MySQL或PHP無關(因爲它們對任何瀏覽器都產生相同的結果)。簡化問題。 – Hamish 2010-11-15 19:30:48

回答

0

您訪問表單不是正確的方法。

形式創建一個變量像你做的選擇:

var form = document.getElementById('form'); 
+0

謝謝,那就是訣竅! – MTSzabo 2010-11-17 10:56:57

1
  1. 你檢查FF的JavaScript控制檯(按住Shift鍵CTRL-J)的任何錯誤?
  2. 您是否嘗試過使用Javascript調試器(如FF的FireBug)來設置它們後,看看menuitemDataArray值是什麼?

我猜你會得到「窗體不是一個對象」或類似的東西。 IE有自動創建標籤和標籤ID對象的惱人的副作用。你沒有在你的setFields()函數中定義「表單」,所以FF無疑會抱怨form變量不存在。在IE中,因爲它可以隨時自動創建東西,所以它最有可能爲你創建'form',但FF不會,所以它不起作用。

+0

感謝您的指點!我感覺這是一個「IE允許懶惰編程」的問題。我添加了表單變量,現在一切都很好。 – MTSzabo 2010-11-17 11:00:05