Any idea about, why the drop down menu for the following script would not stay on the selected value:
if (isset($_POST['Submit1'])) {
$frequency = $_POST['frequency'];
$fileName = 'ConfigurationFile.txt';
$lines = file($fileName);
$lines[0]="Center_frequency= ". $frequency."\n";
file_put_contents($fileName, implode($lines));
} else {
$fileName = 'ConfigurationFile.txt';
$lines = file($fileName);
$frequency=explode(" ",$lines[0]);
$frequency=$frequency[1];
}
?>
but if I put my number in a quotation mark meaning
<?php
if (isset($_POST['Submit1'])) {
$frequency = $_POST['frequency'];
$fileName = 'ConfigurationFile.txt';
$lines = file($fileName);
$lines[0]="Center_frequency= ".'"'. $frequency.'"'."\n";
file_put_contents($fileName, implode($lines));
} else {
$fileName = 'ConfigurationFile.txt';
$lines = file($fileName);
$frequency=explode("\"",$lines[0]);
$frequency=$frequency[1];
}
?>
It would work like a charm meaning the selected value would stay and the drop down menu would not go to the first value on the list. Here is the html code for the drop down menu
<form action="test.php" name="Calculation" method="post">
Center Frequency:
<select name="frequency" >
<?php $attr= 'selected="selected"'; ?>
<option value="4.463" <?php echo $frequency == '4.463' ? $attr : ''; ?>>4.463</option>
<option value="5.2625" <?php echo $frequency == '5.2625' ? $attr : ''; ?>>5.2625 </option>
<option value="9.33" <?php echo $frequency == '9.33' ? $attr : ''; ?>>9.33 </option>
<option value="13.5" <?php echo $frequency == '13.5' ? $attr : ''; ?>>13.5 </option>
<option value="16.15" <?php echo $frequency == '16.15' ? $attr : ''; ?>>16.15 </option>
<option value="24.525" <?php echo $frequency == '24.525' ? $attr : ''; ?>>24.525</option>
<option value="26.275" <?php echo $frequency == '26.275' ? $attr : ''; ?>>26.275</option>
</select>
<Input Type = "Submit" Name ="Submit1" Value = "Save Parameters">
</form>
$fileName = 'ConfigurationFile.txt';
$lines = file($fileName);
$frequency=explode(" ",$lines[0]);
$frequency=$frequency[1];
var_dump($frequency);
//string(5) "13.5 " <== space
solution
$fileName = 'ConfigurationFile.txt';
$lines = file($fileName);
$frequency=explode(" ",$lines[0]);
$frequency=trim($frequency[1]); //trim function
http://php.net/manual/pl/function.trim.php
There is nothing wrong with your explode function, this problem is that your $frequency is returning a string with an extra white-space on the right. try this:
if (isset($_POST['Submit1'])) {
$frequency = $_POST['frequency'];
$fileName = 'ConfigurationFile.txt';
$lines = file($fileName);
$lines[0]="Center_frequency= ". $frequency."\n";
file_put_contents($fileName, implode($lines));
} else {
$fileName = 'ConfigurationFile.txt';
$lines = file($fileName);
$frequency=explode(" ",$lines[0]);
$frequency=trim($frequency[1]);
}
?>
and it should work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With