为什么我不接收文本框的帖子值

时间:2010-02-11 03:48:50

标签: php codeigniter

从这些for循环iam使用javascript计算mark1,mark2,mark3的总和以及总数的平均值...

<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++; } ?>

这是我的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;
        }
     }

我如何收到帖子值 我试过这种方式,但这对我没有用

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);
}   

我怎样才能获得值

1 个答案:

答案 0 :(得分:1)

首先将输入作为数组创建,方法是将其名称后缀为[],例如:

<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

现在您可以从POST var访问此内容:

$_POST['mark']

现在在你的函数中,而不是写:

function Addmark()
{

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

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

写下这个:

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....