Reputation: 2630
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.
Upvotes: 0
Views: 8277
Reputation: 4506
I Don't Know About your php code logic. But This code will create a matrix as u wish , and having some basic validation. To Use in real App, Please according to your feasibility. Just Copy and paste this code to a blank file and run on localhost.
<body>
<form name="form1" action="matrixaddjs.php" method="post" onsubmit="return checkMatrix()">
Enter the number of rows for matrix :
<input type="text" name="ar" id="ar">
Enter the number of columns for matrix :
<input type="text" name="ac" id="ac">
<input type="button" name="create" value="Create Matrix" onclick="createMatrix()">
<div id="matrixA">
</div>
<div id="matrixB">
</div>
<input type="submit" name="submit" value="submit">
<script>
function createMatrix() {
var row = document.getElementById('ar').value;
var col = document.getElementById('ac').value;
var i;
var j;
var k;
var l;
if (row == '') {
alert('Please Enter Number Of Rows!');
return false;
}
if (col == '') {
alert('Please Enter Number Of Columns !');
return false;
}
var amatrix = new Array();
var htmlA = 'Enter the A matrix :';
htmlA += '<table>';
for (i = 0; i < row; i++) {
amatrix[i] = new Array();
htmlA += '<tr>';
for (j = 0; j < col; j++) {
htmlA += '<td>';
htmlA += '<input type="text" name="amatrix[' + i + '][' + j + ']">';
htmlA += '</td>';
}
htmlA += '</tr>';
}
htmlA += '</table>';
document.getElementById('matrixA').innerHTML = htmlA;
var bmatrix = new Array();
var htmlB = 'Enter the B matrix :';
htmlB += '<table>';
for (i = 0; i < row; i++) {
bmatrix[i] = new Array();
htmlB += '<tr>';
for (j = 0; j < col; j++) {
htmlB += '<td>';
htmlB += '<input type="text" name="bmatrix[' + i + '][' + j + ']">';
htmlB += '</td>';
}
htmlB += '</tr>';
}
htmlB += '</table>';
document.getElementById('matrixB').innerHTML = htmlB;
}
function checkMatrix() {
var row = document.getElementById('ar').value;
var col = document.getElementById('ac').value;
if (row == '' || col == '') {
alert('Please Create Matrix First!');
return false;
}
else {
return true;
}
}
</script>
</form>
<?php
if ($_POST['submit'] === 'submit') {
$amatrix = $_POST['amatrix'];
$bmatrix = $_POST['bmatrix'];
if (!is_array($amatrix) && !is_array($bmatrix)) {
echo "Please Create Matrix First by Clicking on Create Matrix!";
exit;
}
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>
Upvotes: 2
Reputation: 21
#1
If you use document.getElementById, there have to be an ID not NAME
<input type="text" name="ar" id="ar">
#2
Put the generated code of A and B matrix to a function. And put them in a DIV
<a href="javascript:renderMatrix();">Render</a>
<div id="target"></div>
<script>
function renderMatrix() {
var html = '';
html += '<table>'; // use a variable instead of document.write
...
html += '<input type="text" name="amatrix['+i+']['+j+']">'; // !!!
...
document.getElementById('target').innerHTML = html;
}
</script>
Upvotes: 1