Reputation: 13
Any PHP Experts: I'm a beginner. I have this Reference Electrode Converter:
<!DOCTYPE html>
<html>
<head>
<title>Referance Electrode Converter</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1>Referance Electrode Converter</h1>
<h6>After Pressing Convert The Dropdown Goes Back to It's Defaults</h6>
<form action="index.php" method="get">
<input type="text" name="input" placeholder="Enter Amount"></input>
<select name="dropdown">
<option value="SHE" selected><sub>SHE</sub></option>
<option value="SCE"><sub>SCE</sub></option>
<option value="AG"><sub>Ag/AgCl</sub></option>
<option value="MSE">MSE</option>
<option value="CSE">CSE</option>
</select>
<br />
<?php
$cc_input = $_GET['input'];
$cc_dropdown = $_GET['dropdown'];
$cc_odropdown = $_GET['odropdown'];
if(isset($_GET['sbmt']))
{
if($cc_dropdown == 'SHE') {
if($cc_odropdown == 'SHE') {
$output = $cc_input * 1 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'SCE') {
$output = $cc_input + 0.2415 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'AG') {
$output = $cc_input + 0.222 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'MSE') {
$output = $cc_input - 0.615 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'CSE') {
$output = $cc_input - 0.318 . "V";
echo "<input type='text' value='$output' disabled/>";
}
}
elseif($cc_dropdown == 'SCE') {
if($cc_odropdown == 'SHE') {
$output = $cc_input - 0.2415 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'SCE') {
$output = $cc_input * 1 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'AG') {
$output = $cc_input + 0.0195 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'MSE') {
$output = $cc_input - 0.3735 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'CSE') {
$output = $cc_input - 0.765 . "V";
echo "<input type='text' value='$output' disabled/>";
}
}
elseif($cc_dropdown == 'AG') {
if($cc_odropdown == 'SHE') {
$output = $cc_input - 0.222 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'SCE') {
$output = $cc_input - 0.0195;
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'AG') {
$output = $cc_input * 1 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'MSE') {
$output = $cc_input - 0.393 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'CSE') {
$output = $cc_input - 0.096 . "V";
echo "<input type='text' value='$output' disabled/>";
}
}
elseif($cc_dropdown == 'MSE') {
if($cc_odropdown == 'SHE') {
$output = $cc_input + 0.615 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'SCE') {
$output = $cc_input + 0.3735 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'AG') {
$output = $cc_input + 0.393 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'MSE') {
$output = $cc_input * 1 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'CSE') {
$output = $cc_input + 0.297 . "V";
echo "<input type='text' value='$output' disabled/>";
}
}
elseif($cc_dropdown == 'CSE') {
if($cc_odropdown == 'SHE') {
$output = $cc_input + 0.318 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'SCE') {
$output = $cc_input + 0.0765 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'AG') {
$output = $cc_input + 0.096 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'MSE') {
$output = $cc_input - 0.297 . "V";
echo "<input type='text' value='$output' disabled/>";
} elseif($cc_odropdown == 'CSE') {
$output = $cc_input * 1 . "V";
echo "<input type='text' value='$output' disabled/>";
}
}
}
?>
<select name="odropdown">
<option value="SHE"><sub>SHE</sub></option>
<option value="SCE" selected><sub>SCE</sub></option>
<option value="AG"><sub>Ag/AgCl</sub></option>
<option value="MSE">MSE</option>
<option value="CSE">CSE</option>
</select>
<br />
<input type="submit" name="sbmt" value="Convert!"></input>
</form>
</body>
</html>
The weird thing is that when I click "Convert," the dropdown menus go back to whatever the HTML "selected" variable was. What can I do to fix this issue?
Thanks -Lorelo
Upvotes: 0
Views: 51
Reputation: 372
OK, I'm not sure if this is only a problem on my server, but it threw errors when I tried to get undefined GET request variables, so I did this:
if(isset($_GET['input'])) $cc_input = $_GET['input'];
if(isset($_GET['dropdown'])) $cc_dropdown = $_GET['dropdown'];
if(isset($_GET['odropdown'])) $cc_odropdown = $_GET['odropdown'];
Anyway, in order to fix your original problem, you should check what the values of $cc_dropdown and $cc_odropdown are. Depending on that, you should decide where to echo the "selected" attribute.
UPDATE
Here's what I did:
...
<body>
<?php
if(isset($_GET['input'])) $cc_input = $_GET['input'];
if(isset($_GET['dropdown'])) $cc_dropdown = $_GET['dropdown'];
if(isset($_GET['odropdown'])) $cc_odropdown = $_GET['odropdown'];
?>
<h1>Referance Electrode Converter</h1>
<h6>After Pressing Convert The Dropdown Goes Back to It's Defaults</h6>
<form action="index.php" method="get">
<input type="text" name="input" placeholder="Enter Amount"></input>
<select name="dropdown">
<?php if(!isset($_GET['sbmt'])): ?>
<option value="SHE" selected><sub>SHE</sub></option>
<option value="SCE"><sub>SCE</sub></option>
<option value="AG"><sub>Ag/AgCl</sub></option>
<option value="MSE">MSE</option>
<option value="CSE">CSE</option>
<?php else: ?>
<option value="SHE" <?php if($cc_dropdown=="SHE") echo "selected"; ?> ><sub>SHE</sub></option>
<option value="SCE" <?php if($cc_dropdown=="SCE") echo "selected"; ?> ><sub>SCE</sub></option>
<option value="AG" <?php if($cc_dropdown=="AG") echo "selected"; ?> ><sub>Ag/AgCl</sub></option>
<option value="MSE" <?php if($cc_dropdown=="MSE") echo "selected"; ?> >MSE</option>
<option value="CSE" <?php if($cc_dropdown=="CSE") echo "selected"; ?> >CSE</option>
<?php endif; ?>
</select>
<br />
<?php
if(isset($_GET['sbmt']))
{
...
UPDATE 2
I forgot to mention that you must also do this for the other dropdown list. This is only for the upper one. Don't worry, though - it's analogous.
Upvotes: 2