0

在普通 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

4 回答 4

1

您可以使用 ajax 请求提交表单,这样页面就不会刷新,并且结果会在同一页面上看到。示例代码 :: View :

 <?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() {
... 

}
于 2013-06-01T05:23:05.563 回答
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;
?>
于 2013-06-01T05:08:12.217 回答
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);
    }
}
于 2013-06-01T04:48:13.660 回答
0

这将从 post 字段中为您提供所需的信息,

$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,则什么也没有。

于 2013-06-01T07:30:19.130 回答