Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disappearing form values from select tags

Status
Not open for further replies.

ttuser4

MIS
Jun 19, 2008
147
CA
I have this code

<?php
session_start();
session_cache_limiter('private,must-revalidate');
header("Cache-control: private");
?>

on the top of page to save and show entered data when click on back button. however, values from two select tags (populated by AJAX) disappeared anyway (all other fields are saved/recovered). is tyhere any way how to recover all fields, please?
 
it is probably the browser that is autopopulating the fields, not your code.

but in any event, what code are you using to provide a value to the select boxes?
 
Here is the code:

<script language="javascript" type="text/javascript">

function getXMLHTTP() {
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}

return xmlhttp;
}

function getSpan(Frame) {

var strURL="findSpan.php?Frame="+Frame;
var req = getXMLHTTP();

if (req) {

req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
document.getElementById('Spandiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
function getModel(Frame,Span) {
var strURL="findModel.php?Frame="+Frame+"&Span="+Span;
var req = getXMLHTTP();

if (req) {

req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
document.getElementById('Modeldiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}

}
</script>
...
<table width="750" id="spec">
<th colspan="3">
A. Dimensions and specifiations
</th>
<tr>
<td width="190"><b>1. Snowload:</b></td>
<td width="140"><input type="text" name="load" size="2" value="<?="$load"?>" />
<?
//echo $load;
?> </td>
<td>Enter the snowload for the project area.</td>
</tr>

<tr>
<td> <b>2. Select your frame type:</b> </td>
<td>
<?
include('../global/includes/_db_info.php');
mysql_connect($dbserver,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$connection = mysql_connect($dbserver,$username,$password);

$load=@$_GET['load'];
if (empty($load)) {
$load=0;
}

$query="Select `bays`.`FS` AS `cat_id`,if((`bays`.`FS` = -(1)),'Free standing','Lean to') AS `category` from `bays` group by `bays`.`FS` order by `bays`.`FS`";
$result=mysql_query($query);

?>
<select name="Frame" onChange="getSpan(this.value)">
<option>--Select here--</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['cat_id']?>><?=$row['category']?></option>
<? } ?>
</select></td><td width="*\"><b>F</b> - Free standing:
<img src="/dealer/images/sh_e.gif" width="39" height="23" alt="Free standing" /> |<b> L</b> - Lean-to: <img src="/dealer/images/lean2.gif" width="30" height="26" alt="Lean_to" />
</td></tr>
<tr style="">
<td><b>3. Select your span:</b></td>
<td ><div id="Spandiv"><select name="Span" >
<option>--Select here--</option>
</select></div></td><td>&nbsp;</td></tr>

<tr style="">
<td> <b>4. Select your model:</b></td>
<td ><div id="Modeldiv"><select name="Model">
<option>--Select here--</option>
</select></div></td><td>Profile: <b>C</b> 125 (Classic) |<b> S</b> 150 (Select) |<b> T</b> 190 (Titan)<br /><br /> Model: <b>E</b>
<img src="/dealer/images/sh_e.gif" width="39" height="23" alt="E Shape 4 Frames" />
|<b> D</b> <img src="/dealer/images/sh_d.gif" width="60" height="24" alt="D Shape 6 Frames" />
|<b> V</b> <img src="/dealer/images/sh_v.gif" width="67" height="23" alt="D Shape 6 Frames" />
</td></tr>
</tr>
<tr>
<td><b>5. Length (enclosure total):</b> </td>
<td> <input type="text" name="tlength" size="2" onkeyup="data_change(this);f2m(this.form);" />ft or <input type="text" name="lm" size="4" onkeyup="m2f(this.form);" />m</td>
<td>The optimum length for the encloure should be multiples of 7ft/2.14m.</td>
</tr>
</table>


findspan.php:
<? $Frame=intval($_GET['Frame']);
include('../global/includes/_db_info.php');
mysql_connect($dbserver,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$connection = mysql_connect($dbserver,$username,$password);

mysql_select_db('db1');
$query="SELECT span FROM bays where fs=$Frame group by span order by span";
$result=mysql_query($query);

?>
<select name="Span" onchange="getModel(<?=$Frame?>,this.value)">
<option>--Select here--</option>
<? while($row=mysql_fetch_array($result)) {
$spanm=round(($row['span'])*0.3048,2);
$mystring=intval($row['span'])."ft :: ".$spanm."m";
?>
<option value=<?=$row['span']?>><?=$mystring?></option>
<? } ?>
</select>

findmodel.php:
<? $Frame=intval($_GET['Frame']);
$Span=intval($_GET['Span']);
include('../global/includes/_db_info.php');
mysql_connect($dbserver,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$connection = mysql_connect($dbserver,$username,$password);
mysql_select_db('db1');
$query="SELECT qbay.type as type, qbay.load as myload, qbay.bprice/(qbay.length*qbay.span) as myprice, qbay.length as width FROM qbay where qbay.span=$Span and qbay.fs=$Frame order by myprice, qbay.load, qbay.type";
$result=mysql_query($query);

?>
<select name="Model">
<option>--Select here--</option>
<? while($row=mysql_fetch_array($result)) {
$myvalue=$row['type'].$row['width'];
$mystring2=$row['type']." : ".number_format($row['myprice'],2)."$/sqf : ".number_format($row['width'],0)."'bays : ".number_format($row['myload'],0)."psf";
?>
<option value='<?=$myvalue?>'><?=$mystring2?></option>
<? } ?>
</select>
 
you are not preselecting any option in your code. to do so you need to something like this

Code:
//building options
while ($row = mysql_fetch_assoc($result)){
  $selected = ($row['value'] == $_POST['selectValue']) ?'selected="selected"':'';
  echo <<<HTML
<option value="{$row['value']}" $selected>{$row['text']}</option>
HTML;
} //endwhile
 
it's not an ajax issue, no. it's that different browsers do things in different ways.

the way to auto-populate fields is to serve the correct html to the browser with the value attributes set and the selected/checked attributes specified. If you have more questions I suggest that the html forum would be the best port of call.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top