I am trying to do matrix addition in php. Here i am trying to get the input from the user and make the array for addition. I tried this. i have some more mistakes here. any one can give the solution for this... Thanks in advance...
<html>
<body>
<form name="form1" action="matrixaddjs.php" method="post">
Enter the number of rows for matrix : <input type="text" name="ar">
Enter the number of columns for matrix : <input type="text" name="ac">
<input type="submit" name="submit" value="submit">
<script>
var row=document.getElementById('ar').value;
var col=document.getElementById('ac').value;
var i;
var j;
var k;
var l;
var amatrix= new array();
document.write('<table>');
for(i=0;i<row;i++)
{
amatrix[i]=new array(j);
document.write('Enter the A matrix :');
document.write('<tr>');
for(j=0;j<col;j++)
{
document.write('<td>');
document.write('<input type="text" name="amatrix[i][j]">');
document.write('</td>');
}
document.write('</tr>');
}
document.write('</table>');
var bmatrix= new array();
document.write('Enter the B matrix :');
document.write('<table>');
for(i=0;i<row;i++)
{
bmatrix[i]=new array(j);
document.write('<tr>');
for(j=0;j<col;j++)
{
document.write('<td>');
document.write('<input type="text" name="bmatrix[i][j]">');
document.write('</td>');
}
document.write('</tr>');
}
document.write('</table>');
</script>
</form>
<?php
if($_POST['submit']=== 'submit')
{
$amatrix=$_POST['amatrix'];
$bmatrix=$_POST['bmatrix'];
echo "<table>";
echo "The resultant matrix is :";
for($m=0;$m<$ar;$m++)
{
echo "<tr>";
for($n=0;$n<$ac;$n++)
{
$cmatrix[$m][$n]=$amatrix[$m][$n]+$bmatrix[$m][$n];
echo "<td>";
echo $cmatrix[$m][$n];
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>
if user gives:
Enter the number of rows for matrix : 2
Enter the number of columns for matrix : 2
Enter the A matrix :
2 4
3 5
Enter the B matrix :
3 4
5 6
The resultant matrix is:
5 8
8 11
This is my expected result.