Hi All,
I have set up two AJAX demos on my site at
http://joomla170.iplappdev.co.uk/
The first demo in the "Flexi Custom Code 1" module works fine. The second demo in the "Flexi Custom Code 2" module is a single file so it has to refresh the whole page to display the calculated values. I want to split the file into two smaller files so that the "Flexi Custom Code 2" deom works the same as the "Flexi Custom Code 1" demo.
Here is the code I have tried, without success. I'm stumped by this simple code. The file TankCalculator.php calls gettank.php and, using AXAX, should display the tank values without having to refresh the whole page. Unfortunately, TankCalculator.php just sits there doing nothing when the submit button is clicked. Where have I gone wrong?
Regards
Gary
==========================================
Code:
File name: TankCalculator.php
<html>
<head>
<script type="text/javascript">
function TankCalc()
{
if (str=="")
{
document.getElementById("TankValues").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
var xmlhttp = new XMLHttpRequest();
var cdia = encodeURIComponent(document.getElementsByName('c_dia')[0].value);
var cheight = encodeURIComponent(document.getElementsByName('c_height')[0].value);
}
else
{// code for IE6, IE5
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
var cdia = encodeURIComponent(document.getElementsByName('c_dia')[0].value);
var cheight = encodeURIComponent(document.getElementsByName('c_height')[0].value);
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("TankValues").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open('POST', 'gettank.php', true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send('c_dia=' + cdia + '&c_height=' + cheight);
}
</script>
</head>
<body>
<form name="frm_c_tank">
<table>
<tr>
<td>Cylindrical Tank</td>
</tr>
<tr>
<td>Height(m)</td>
<td><input name="c_height" type="text"></td>
<td>Diameter(m)</td>
<td><input name="c_dia" type="text"></td>
</tr>
<tr>
<td><input name="calculate" type="button" value="Calculate" onClick="TankCalc()"></td>
</tr>
</table>
</form>
<div id="TankValues"><b>Tank Values Appear Here.</b></div>
</body>
</html>
===============================================
File Name: gettank.php
<?php
$c_dia = $_POST['c_dia'];
$ht_t = $_POST['c_height'];
$dia_t = $c_dia;
$dia_t = (($dia_t * $dia_t)/4);
$ctank_val = (3.1459 * $dia_t * $ht_t * 1000);
$ctank_val_ml = (3.1459 * $dia_t * $ht_t);
echo "The tank height is : <b> $ht_t </b><br>";
echo "The tank diameter is : <b> $c_dia </b><br>";
?>
<table>
<tr>
<td>
<input name="c_size_ml" value="<?php echo $ctank_val; ?>" type="text"> Litres
</td>
<td>
<input name="c_size_l" value="<?php echo $ctank_val_ml; ?>" type="text"> m3
</td>
</tr>
</table