Need some help with this currency convertor script below
<script language="JavaScript" type="text/javascript">
<
function temp(form)
{
var a = parseFloat(form.Amount.value, 10);
var f = parseFloat(form.RateFrom.value, 10);
var t = parseFloat(form.RateTo.value, 10);
var c = 0;
c = a * f / t;
c = Math.round(1000*c)/1000;
form.Exchange.value = c;
}
converter =
'<div class=\"fxborder\">'
+ ''
+ '<form action=\"\">'
+ '<p class=\"fx\">Amount:<br />'
+ ' <input name=\"Amount\" value=\"1\" maxlength=\"15\" size=\"15\" class=\"fx\" />'
+ '<br />From:<br />'
+ ' <select name=\"RateFrom\" class=\"fx\">'
+ '<option class=\"fx\" value=\"1.00\" selected="selected">British Pound (£)</option>'
+ '</select>'
+ '<br />To:<br />'
+ '<select name=\"RateTo\" class=\"fx\">'
+ '<option class=\"fx\" value=\"0.00851292\" selected="selected">my currency($)</option>'
+ '</select>'
+ '</p><p class=\"fx\">'
+ ' <input name=\"calc\" value=\"Convert\" type=\"button\" onclick=\"temp(this.form)\" class=\"fxsubmit\" />'
+ '<br />'
+ 'Result:<br />'
+ ' <input name=\"Exchange\" readonly=\"readonly\" size=\"15\" class=\"fxresults\" />'
+ '</p>'
+ '</form>'
+ '</div>'
// end_var_declaration
document.write(converter);
</script>
I have managed to write a file called flat.txt containing the rate to the server using php after reading a php tutorial:
<form name="input" method="post"
action="<?php echo $_SERVER['SCRIPT_NAME']?>">
<p><label for="rate">Today's rate: </label>
<input type="text" name="name" id="rate" size="20" /></p>
<p><input type="reset" name="reset" value="reset" />
<input type="submit" name="submit" value="submit" /></p>
</form>
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$fp = fopen("flat.txt","w+");
if(!$fp) {
echo 'Error, the file could not be opened or there was an error creating it.';
exit;
}
fwrite($fp, $name."\n");
fclose($fp);
}
?>
Does anyone know how I can read the value from the flat.txt file, and replace the 'value' field in the converter with the new rate?
Update: Got it sorted, instead of saving to flat.txt, I saved to ex.js as a variable and popped it in the head document of my webpage and then called it from there.

<script language="JavaScript" type="text/javascript">
<
function temp(form)
{
var a = parseFloat(form.Amount.value, 10);
var f = parseFloat(form.RateFrom.value, 10);
var t = parseFloat(form.RateTo.value, 10);
var c = 0;
c = a * f / t;
c = Math.round(1000*c)/1000;
form.Exchange.value = c;
}
converter =
'<div class=\"fxborder\">'
+ ''
+ '<form action=\"\">'
+ '<p class=\"fx\">Amount:<br />'
+ ' <input name=\"Amount\" value=\"1\" maxlength=\"15\" size=\"15\" class=\"fx\" />'
+ '<br />From:<br />'
+ ' <select name=\"RateFrom\" class=\"fx\">'
+ '<option class=\"fx\" value=\"1.00\" selected="selected">British Pound (£)</option>'
+ '</select>'
+ '<br />To:<br />'
+ '<select name=\"RateTo\" class=\"fx\">'
+ '<option class=\"fx\" value=\"0.00851292\" selected="selected">my currency($)</option>'
+ '</select>'
+ '</p><p class=\"fx\">'
+ ' <input name=\"calc\" value=\"Convert\" type=\"button\" onclick=\"temp(this.form)\" class=\"fxsubmit\" />'
+ '<br />'
+ 'Result:<br />'
+ ' <input name=\"Exchange\" readonly=\"readonly\" size=\"15\" class=\"fxresults\" />'
+ '</p>'
+ '</form>'
+ '</div>'
// end_var_declaration
document.write(converter);
</script>
I have managed to write a file called flat.txt containing the rate to the server using php after reading a php tutorial:
<form name="input" method="post"
action="<?php echo $_SERVER['SCRIPT_NAME']?>">
<p><label for="rate">Today's rate: </label>
<input type="text" name="name" id="rate" size="20" /></p>
<p><input type="reset" name="reset" value="reset" />
<input type="submit" name="submit" value="submit" /></p>
</form>
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$fp = fopen("flat.txt","w+");
if(!$fp) {
echo 'Error, the file could not be opened or there was an error creating it.';
exit;
}
fwrite($fp, $name."\n");
fclose($fp);
}
?>
Does anyone know how I can read the value from the flat.txt file, and replace the 'value' field in the converter with the new rate?
Update: Got it sorted, instead of saving to flat.txt, I saved to ex.js as a variable and popped it in the head document of my webpage and then called it from there.

Last edited: