使用控制器和codigniter的两个数的总和

时间:2017-11-27 15:00:17

标签: php

我试图在视图文件中输入值的codigniter中这两个数字的总和,但它在编码中得到错误 消息:extract()期望参数1为数组,给定null 消息:未定义的变量:t1和t2

controller file

Home.php
<?php 
  class home extends CI_Controller 
{

  public function index()
  {
        echo"hello freinds....";
  }
    public function display()
  {
    extract($_post);
    $sum=$t1+$t2;
    $data=array('sum'=>$sum);
    $this->load->view('disp',$data);
  }
}

 View file
 disp.php

<html>
<body>
 <h1>hello it is disply.php</h1>
 <form action="sum" method=post>
 Enter number 1 :<input type=text name=t1>
 <br>
 Enter number 2 :<input type=text name=t2>
 <br>
 <input type=submit value="sum">
 <?php
 if(isset($_POST['sum']))
  {
 ?>
  sum of two number : <?= $sum ?>
  <?php
    }
   ?>
  </form>
  </body>
  </html>

3 个答案:

答案 0 :(得分:0)

在这种情况下,$_post为空,因为它区分大小写,您必须使用$_POST

(...)
extract($_POST);
(...)

示例:

var_dump($_post);
// Output:
// <b>Notice</b>:  Undefined variable: _post in <b>[...][...]</b> on line <b>2</b><br />
// NULL


var_dump($_POST);
// Output:
// array(0) {
// }

答案 1 :(得分:0)

Home.php
<?php 
class home extends CI_Controller 
{

  public function index()
  {
    echo"hello freinds....";
  }
  public function display()
  {

    $data = array(
        'sum' => (isset($_POST['t1']) && isset($_POST['t2'])) 
                    ? $_POST['t1'] + $_POST['t2'] : 0
    );
    $this->load->view('disp',$data);

  }
}

 View file
 disp.php

<html>
    <body>
        <h1>hello it is disply.php</h1>
        <form action="/home/display" method="post">
            Enter number 1 :<input type="text" name="t1">
            <br>
            Enter number 2 :<input type="text" name="t2">
            <br>
            <input type="submit" value="sum">

            <?php if(isset($sum) && $sum != ''): ?>
                sum of two number : <?php echo $sum; ?>
            <?php endif; ?>

        </form>
    </body>
  </html>

答案 2 :(得分:0)

controller file

Home.php
<?php 
  class home extends CI_Controller 
{

  public function index()
  {
        echo"hello freinds....";
  }
    public function display()
  {
    extract($_post);
    $sum=$t1+$t2;
    $data=array('sum'=>$sum);
    $this->load->view('disp',$data);
  }
}

 View file
 disp.php

<html>
<body>
 <h1>hello it is disply.php</h1>
 <form action="action="<?php echo base_url().'home/display';?>" method=post>
 Enter number 1 :<input type=text name=t1>
 <br>
 Enter number 2 :<input type=text name=t2>
 <br>
 <input type=submit value="sum" name="sum">
 <?php
 if(isset($_POST['sum']))
  {
 ?>
  sum of two number : <?= $sum ?>
  <?php
    }
   ?>
  </form>
  </body>
  </html>