Reputation: 9
In normal PHP, you can send a form on the same page:
<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'];
?>
How can I do this in CodeIgniter?
I've tried this, but I don't know how to do it:
View:
<? echo form_open('form'); ?>
.....
</form>
<?
if (isset($_POST['data'])){
$array = $data;
foreach ($array as $array){
echo array_sum($array)/count($array)."<br>";
}
}
?>
Controller:
function index() {
$name = $this->input->post('select');
$this->load->model('select');
$data['result'] = $this->select->index();
$this->load->view('theview', $data);
}
Upvotes: 0
Views: 4807
Reputation: 924
This will get you what you need from the post field,
$var = $this->input->get_post('some_data', TRUE);
but if the field is blank (when first entering the page) you might run into a problem with the variable not being defined. So be sure to set the following in your controller:
$var = NULL; $var = $this->input->get_post('some_data', TRUE);
$data = array('var' => $var);
$this->load->view('some_view', $data);
In the view if you call echo $var
you should get the output from the post, or nothing if the variable is NULL.
Upvotes: 0
Reputation: 2401
You can submit the form using ajax request so that the page is not refreshed and the results are seen on the same page.Sample code :: 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
}
Controller:
function update_user_profile() {
...
}
Upvotes: 1
Reputation: 387
you can do like this :
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;
?>
Upvotes: 0
Reputation: 1503
If your view is at view/controller/index.php, then in your view, simply use
echo form_open('controller/index');
...
if(isset($hi))echo $hi
And in your controller/index method just put something like
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);
}
}
Upvotes: 0