Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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. <strong>Just Copy and paste this code to a blank file and run on localhost.</strong> </p> <pre><code>&lt;body&gt; &lt;form name="form1" action="matrixaddjs.php" method="post" onsubmit="return checkMatrix()"&gt; Enter the number of rows for matrix : &lt;input type="text" name="ar" id="ar"&gt; Enter the number of columns for matrix : &lt;input type="text" name="ac" id="ac"&gt; &lt;input type="button" name="create" value="Create Matrix" onclick="createMatrix()"&gt; &lt;div id="matrixA"&gt; &lt;/div&gt; &lt;div id="matrixB"&gt; &lt;/div&gt; &lt;input type="submit" name="submit" value="submit"&gt; &lt;script&gt; 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 += '&lt;table&gt;'; for (i = 0; i &lt; row; i++) { amatrix[i] = new Array(); htmlA += '&lt;tr&gt;'; for (j = 0; j &lt; col; j++) { htmlA += '&lt;td&gt;'; htmlA += '&lt;input type="text" name="amatrix[' + i + '][' + j + ']"&gt;'; htmlA += '&lt;/td&gt;'; } htmlA += '&lt;/tr&gt;'; } htmlA += '&lt;/table&gt;'; document.getElementById('matrixA').innerHTML = htmlA; var bmatrix = new Array(); var htmlB = 'Enter the B matrix :'; htmlB += '&lt;table&gt;'; for (i = 0; i &lt; row; i++) { bmatrix[i] = new Array(); htmlB += '&lt;tr&gt;'; for (j = 0; j &lt; col; j++) { htmlB += '&lt;td&gt;'; htmlB += '&lt;input type="text" name="bmatrix[' + i + '][' + j + ']"&gt;'; htmlB += '&lt;/td&gt;'; } htmlB += '&lt;/tr&gt;'; } htmlB += '&lt;/table&gt;'; 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; } } &lt;/script&gt; &lt;/form&gt; &lt;?php if ($_POST['submit'] === 'submit') { $amatrix = $_POST['amatrix']; $bmatrix = $_POST['bmatrix']; if (!is_array($amatrix) &amp;&amp; !is_array($bmatrix)) { echo "Please Create Matrix First by Clicking on Create Matrix!"; exit; } echo "&lt;table&gt;"; echo "The resultant matrix is :"; for ($m = 0; $m &lt; $ar; $m++) { echo "&lt;tr&gt;"; for ($n = 0; $n &lt; $ac; $n++) { $cmatrix[$m][$n] = $amatrix[$m][$n] + $bmatrix[$m][$n]; echo "&lt;td&gt;"; echo $cmatrix[$m][$n]; echo "&lt;/td&gt;"; } echo "&lt;/tr&gt;"; } echo "&lt;/table&gt;"; } ?&gt; &lt;/body&gt; </code></pre> <p></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload