views:

76

answers:

1

hi from these for loop iam using javascript to calculate the total of mark1,mark2,mark3 and the average of the total...

<input type="text" name="tblRowCount" id="tblRowCount"  />  
    <table align="center" width="100%" border="0" class="table_Style_Border" id="table">
        <input type="text" name="txtrowcount" id="txtrowcount" />
my for loop is this
    $a = 1;
           for($i=0;$i<sizeof($StudName);$i++) { ?>
        <tr align="left">
        <td class="table_label"><? echo $code[$i].' - '.$StudName[$i]; ?></td>

        <td><input type = "text" name="mark1<?= $a ?>" id="mark1<?= $a ?>" ></td>
        <td><input type = "text" name="mark2<?= $a ?>" id="mark2<?= $a ?>" ></td>
        <td><input type = "text" name="mark3<?= $a ?>" id="mark3<?= $a ?>" 
        onkeyup="return percent('mark1<?= $a ?>','mark2<?= $a ?>','mark3<?= $a ?>','total<?= $a ?>','Average<?= $a ?>')" ></td>
        <td><input type = "text" name="total<?= $a ?>" id="total<?= $a ?>"
         onkeyup="return percent('mark1<?= $a ?>','mark2<?= $a ?>','mark3<?= $a ?>','total<?= $a ?>','Average<?= $a ?>')"></td>
        <td><input type = "text" name="Average<?= $a ?>" id="Average<?= $a ?>" ></td>
    </tr>
    <? $a++; } ?>

this is my javascript

function percent(mark1,mark2,mark3,total,Average)
{
    var tbl = document.getElementById('table');
    var mark1value=document.getElementById(mark1).value;
    var mark2value=document.getElementById(mark2).value;
    var mark3value=document.getElementById(mark3).value;
    var totalvalue=document.getElementById(total).value;
    var averagevalue=document.getElementById(Average).value;
    total_1 = parseInt(mark1value)+parseInt(mark2value)+parseInt(mark3value);
    average_1 = total_1/3;
    document.getElementById(total).value = total_1.toFixed(2);
    document.getElementById(Average).value = average_1.toFixed(2);

}
function tblrowcount()
    {
        var tblid = document.getElementById('table'); 
         //alert(tblid);

        //alert(tblRowcount);
        document.getElementById('txtrowcount').value = tblid.rows.length-1;
        return true;
    }
function removeRowFromTable()
    {
        var tbl = document.getElementById('table');
        var lastRow = tbl.rows.length;
        if (lastRow > 2) 
        {
        tbl.deleteRow(lastRow - 1);
        document.getElementById('tblRowCount').value=tbl.rows.length-1;
        }
     }

how can i receive the post values i tried it this way but that didnt work for me

function Addmark()
    {

    for($a=1;$a<=$rowcount;$a++)
    {

        $markI='mark1'.$a;
        $markII='mark2'.$a;
        $markIII='mark3'.$a;
        $total='total'.$a;
        $avg='Average'.$a;

        $mI[] = $this->input->post($mark1);
        $mII[] = $this->input->post($mark2);
        $mIII[] = $this->input->post($mark3);
        $Total[] = $this->input->post($total);
        $Avrg[] = $this->input->post($Average);

    }

        $res = $this->staffModel->Addmark($mI,$mII,$mIII,$Total,$Avrg);


                if($res==true)

                       {
                            $this->session->set_flashdata('response', 'data added successfully !');
                        }
                    else
                        {
                            $this->session->set_flashdata('response', 'data already exist !');
                        }

                $this->load->view('Sstudentlist',$data);
}   

how can i get the values

+1  A: 

First create your inputs as an array by suffixing their name with [] eg:

<input type = "text" name="mark[]" id="mark1<?= $a ?>" >
<input type = "text" name="mark[]" id="mark2<?= $a ?>" >
<input type = "text" name="mark[]" id="mark3<?= $a ?>" >
...and so on

Now you can access this from POST var like this:

$_POST['mark']

Now in your function, instead of writing:

function Addmark()
{

  for($a=1;$a<=$rowcount;$a++)
  {

    $markI='mark1'.$a;
    $markII='mark2'.$a;
    $markIII='mark3'.$a;
    $total='total'.$a;
    $avg='Average'.$a;

Write this:

function Addmark()
{
   global $_POST; // i assume that your post gets through on this page

  for($a=1;$a<=$rowcount;$a++)
  {

    $markI=$_POST['mark'][0]; // since arrays start at 0
    $markII=$_POST['mark'][1];
    $markIII=$_POST['mark'][2];
    $total=$_POST['mark'][3];
    $avg='Average'.$a;

   // and then same rest of the code....
Sarfraz