在CodeIgniter中的同一页面上打印表单的结果

时间:2013-06-01 04:28:28

标签: php codeigniter

在普通的PHP中,您可以在同一页面上发送表单:

<form action = "" method = "POST">
    <input type = "text" name = "hi" value = "hi"/>
    <input type = "submit" name = "send" value = "Send"/>
</form>
<?
   if (isset($_POST['send'])) 
   echo $_POST['hi'];
?>

我如何在CodeIgniter中执行此操作?

我试过这个,但我不知道怎么做:

查看:

<? echo form_open('form'); ?>
.....
</form>
<?
if (isset($_POST['data'])){
    $array = $data;
    foreach ($array as $array){
       echo array_sum($array)/count($array)."<br>";
    }
}
?>

控制器:

function index() {
    $name = $this->input->post('select');
    $this->load->model('select');
    $data['result'] = $this->select->index();
    $this->load->view('theview', $data);
}

4 个答案:

答案 0 :(得分:1)

您可以使用ajax请求提交表单,以便不刷新页面并在同一页面上显示结果。示例代码:: 查看:

 <?php
$attributes = array('id'=>'profile-form','name'=>'personal_pro_form');
echo form_open('update_user_profile',$attributes);?>
<label class="control-label myprofilefont" for="email"> Email Address:<em  class="colorred" >*</em></label>
  <input type="text" id="email" placeholder="Email Address" name="user_email_id" value=>
                     ..
   <img src="submit-button.png"  id="Submit_Personal_Details">
   </form>
  $('#Submit_Personal_Details').live('click',function() {
  $.ajax({
             url:'update_user_profile',
            type:"POST",
            data:{'user_email_id':uemail,...},
            success:function(response){
            },
            error:function(req,status,error){
                alert(error);

            }
        });//end of ajax

}

控制器:

 function update_user_profile() {
... 

}

答案 1 :(得分:0)

如果您的视图位于view / controller / index.php,那么在您的视图中,只需使用

即可
echo form_open('controller/index');
...

if(isset($hi))echo $hi

在你的控制器/索引方法中只需输入类似

的内容
function index() {
    if($_POST){
        if (isset($_POST['send'])){
            $this->set('hi', $_POST['hi']);
        }
    }else{
        $name = $this->input->post('select');
        $this->load->model('select');
        $data['result'] = $this->select->index();
        $this->load->view('theview', $data);
    }
}

答案 2 :(得分:0)

你可以这样做: PHP

function index(){

    if($_SERVER['REQUEST_METHOD']=="GET")
    {
        $data['hi'] = "";
    }else //if POST
    {
        $data['hi'] = $this->input->post("hi");
    }
    //load one view for both GET and POST
    $this->load->view('theview', $data);
}

<强> HTML

<form action = "URL_TO_INDEX_FUNCTION" method = "POST">
    <input type = "text" name = "hi" value = "hi"/>
    <input type = "submit" name = "send" value = "Send"/>
</form>
<?
   if (isset($hi) && $hi != "") 
        echo $hi;
?>

答案 3 :(得分:0)

这将为您提供所需的帖子字段,

$var = $this->input->get_post('some_data', TRUE);

但如果该字段为空(首次进入页面时),则可能会遇到未定义变量的问题。因此,请务必在控制器中设置以下内容:

$var = NULL; $var = $this->input->get_post('some_data', TRUE);
$data = array('var' => $var);
$this->load->view('some_view', $data);

在视图中,如果您调用echo $var,您应该从帖子中获取输出,如果变量为NULL,则不显示任何内容。