Alright so i have in my views a index that has a input type submit like so
<input type="submit" name="submit" value="Submit">
When using the controller like so (to tests if it works)
public function indexAction()
{
$message = 'not submited';
if(isset($_POST['submit']) === TRUE){
$message = 'submited';
}
$this->view->setVars([
'message' => $message
]);
}
and calling the variable message in my html with echo $message i get not submited, what am i missing here ?
just erase " === TRUE"
just try this:
if (isset($_POST['submt'])).
And make sure that ur form method is set to "POST" and action to "#"
Related
this is my controller page:
function saveNewForm()
{
//----------- verifying code-------------------------
$transactionCode = 0010; // any secret code
echo "
<input type='text' name='value'>
<input type='submit' name='submit'>
";
if($this->input->post('value') == $transactionCode){
//----------- posting data from view--------------------
$form = $this->input->post();
$data = $form;
$this->load->model('user_model');
$result = $this->user_model->saveNewTransaction($data);
redirect('transactions');
}
}
I know there is some mistake in if statement that I am comparing $this->input->post('value') which is not actually coming from the FORM but it is already in the controller.
$form gets the data from view page which also have a FORM.
How do make this work fine.
I want to verify user by let him enter password to submit the form.
I have tried to use onsubmit in form but this code can be removed from inspect element on browser and unfortunately user can bypass that.
I am doing this for security purpose so that I can minimize the user to make changes in data using various techniques.
I am not sure about this , but u can try this code
function saveNewForm()
{
$transactionCode = 0010; // any secret code
echo "
<form action='' method='post'>
<input type='text' name='value'>
<input type='submit' name='submit'>
</form>
";
if(!empty($this->input->post('value')){
if($this->input->post('value') <> $transactionCode){
exit();
}
}
$form = $this->input->post();
$data = $form;
$this->load->model('user_model');
$result = $this->user_model->saveNewTransaction($data);
redirect('transactions');
}
but when u are click submit the post data only have "value"
and for what "$form = $this->input->post();" ?
I have a problem on getting post data after submitting a form. What I'm trying to do is that, when the user clicks the submit button, input values stored in the hidden fields will be assigned to a variable into another controller class. However, when I tried to print out the form value, its always giving me NULL
This is my code:
Controller
public function updateOrder(){
$this->form_validation->set_rules('delivery_status_id', 'Delivery Status', 'xss_clean');
$this->form_validation->set_rules('remarks', 'Remarks', 'xss_clean');
$this->form_validation->set_rules('total_amt', 'Total Amount', 'xss_clean');
$data = array('remarks' => $this->input->post('remarks'),
'delivery_status_id' => $this->input->post('delivery_status_id'),
'total_amt' => $this->input->post('total_amt'));
if ($data['delivery_status_id'] == $getDeliveryStatusIdDelivered->row('id'))
{
$this->db->select('*');
$this->db->from('user_has_penalty');
$this->db->where("user_id =" . $this->session->userdata['id']);
$query = $this->db->get();
foreach ($query->result() as $row)
{
$this->db->delete('user_has_penalty', array('user_id' => $this->session->userdata['id']));
$this->db->delete('penalty', array('id' => $row->penalty_id));
}
}else if($data['delivery_status_id'] == $getDeliveryStatusIdCancelled->row('id'))
{
$penalty_amt = $data['total_amt'] / 2;
$data = array('amount' => $penalty_amt);
$this->penalty->insert($data);
$penalty_id = $this->db->insert_id();
//var_dump($penalty_id);
}
var_dump($this->input->post); // Outputs NULL
}
View
...
<?php
$name_c = 'Cancelled';
$query = $this->DeliveryStatus->getDeliveryStatusByName($name_c);
echo form_open('Order_Controller/updateOrder');
?>
<input type="hidden" name="delivery_status_id" value="<?php echo $query->row('id'); ?>"/>
<button type="submit" class="btn btn-danger">YES</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<?php echo form_close(); ?>
Is there something wrong with the code? Any help would be appreciated
In your form tag, if you do not specify the method it will be get method.
<form action="Order_Controller/updateOrder" method="post">
...
</form>
You are missing the method in your form tag.
form_open('order_Controller/updateOrder', array('method'=>'post'));
Note:
Please use model for database related queries.
Pass the query result to the controller and then to the view.
You can add your validation in configuration. To handle all validation at one place.
// This Will outputs always null
var_dump($this->input->post);
// To Get Value Array from codeigniter input class
var_dump($this->input->post());
method will return the values in $_POST variable
because you are accessing uninitialized property of a object
The correct way to get post variable from codeigniter input class is
$this->input->post() is equivalent to $_POST;
$this->input->post('data') is equivalent to $_POST['data']
I am using codeigniter 3.1 .
How to return or get posted email after submit data from different function?
HTML
<form action="settings/valid" class="form-horizontal" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<input type="email" name="email" value="<?php echo $email ?>" />
<input type="submit"/>
</form>
PHP
public function index() {
// how to get post email ?
$email = $this->input->post("email"));
$this->template->loadContent("settings/index.php", array(
"email" => $email
)
);
}
public function valid() {
$email = $this->input->post("email"));
$this->user->add($this->user->ID, array(
"email" => $email
));
redirect(site_url("index.php"));
}
This may answer your question better.
public function index() {
// how to get post email ?
$email = $this->session->flashdata("email");
$this->template->loadContent("settings/index.php", array(
"email" => $email
)
);
}
public function valid() {
$email = $this->input->post("email"));
$this->user->add($this->user->ID, array(
"email" => $email
));
$this->session->set_flashdata("email",$email);
redirect(site_url("index.php"));
}
What you are doing is called routing. That means that it makes no sense to get the email in index, because no data has been sent to index. What you can do is redirect the user to index and pass a post argument to index.
To put it another way. Let's say you open a web page and give it your name. You cannot expect another page to know your name as well, because that page does not exist yet (it has not been generated yet by php).
But you can send the user to another page and tell that page the user's name.
For instance, in your case:
in valid()
redirect(site_url("index.php?email=".$email));
and in index()
$email = $this->input->get("email")
I am creating a search page in my CodeIgniter project.
On submit, the form calls the controller function, data is fetched via model function and the resulting array is passed to the view
The problem is that when I refresh the result page the form is resubmitting because the $_POST data is still there in the request headers.
How can I avoid that resubmit confirmation message
Following is the code for my form :
<!--form-->
<form id="find" action="<?php echo base_url()?>search/find" method="post">
<input type="text" name="search_key" class="tb4" id="search_key" placeholder="Search here"/>
<input type="button" value="search"/>
</form>
Following is the code for my controller:
/*
* function for fetching search results
* #param void
* #return void
*/
public function find()
{
$data['search_result']=$this->search_model->search($this->input->post('search_key'));
$this->load->view('template/header');
$this->load->view('pages/search_result',$data);
$this->load->view('template/footer');
}
Kindly help me with this.I can't use redirect instead of loading the view since I am bound to pass the result array $data to the view.
Try redirect to itself
public function find()
{
$data['search_result']=$this->search_model->search($this->input->post('search_key'));
if($this->input->post('search_key')) {
redirect('yourcontroller/find');
}
$this->load->view('template/header');
$this->load->view('pages/search_result',$data);
$this->load->view('template/footer');
}
Simple solution is to have a hidden timestamp field in the form.
<?php echo form_hidden( 'TS', time() ); ?>
When the form is processed, save this timestamp in the session,
$this->session->set_userdata( 'form_TS', $this->input->post( 'TS' ) );
Before processing the form check that two timestamps doesn't match
if ( $this->input->post( 'TS' ) != $this->session->userdata('form_TS') )
{...}
IF you want to avoid the resubmit then please after save redirect on same controller like this
It can be solved using session. If there is any POST form submit,
ie
if (count($_POST) > 0){
$this->session->set_userdata('post_data', $_POST );
redirect('same_controller');
}
else{
if($this->session->userdata('post_data')){
$_POST = $this->session->userdata('post_data');
$this->session->unset_userdata('post_data');
}
}
Kindly use this:
$post_data = $this->session->userdata('post_data');
if ($post_data == $_POST){
$this->session->unset_userdata('post_data');
redirect(current_url(), 'refresh');
}else{
$this->session->set_userdata('post_data', $_POST );
}
I have a controller which loads the view like this:
class A extends Controller{
public function simpleForm(){
//this generates the form
}
public function simpleFormSubmitted(){
// Simple form is submitted here. Here I perform validation and if
// validation fails I want to display simpleform again with all the values
// inputted by the user as it is and if validation succeeds I want to
// redirect to some other page. I am using simple HTML to generate the
// form and not the formhelper of CodeIgniter because I am more comfortable
// with HTML rather than remembering the syntax of CodeIgniter.
}
}
Here is how to preserve the form fields when dealing with errors...
$fields['username'] = 'Username';
$fields['password'] = 'Password';
$fields['passconf'] = 'Password Confirmation';
$fields['email'] = 'Email Address';
$this->validation->set_fields($fields);
This is how to re-populate the HTML form...
<?php echo $this->validation->error_string; ?>
<?php echo form_open('form'); ?>
<h5>Username</h5>
<input type="text" name="username" value="<?php echo $this->validation->username;?>" size="50" />
For more information look over here - Form Validation in Codeigniter
Look for the heading Re-populating the form
in function simpleForm add default variables so, after validation you can call it again with userform values
class A{
public function simpleForm($val1="", val2="", $val3=""){
//this generates the form
}
public function simpleFormSubmitted(){
//simple form is submitted here. Here is perform validation and if validation fails i
if(!$valid){
$result = $this->simpleForm($val1, $val2, $val3 etc...)
}
else{
$result = //whatever you need to output after validation success
}
return $result
}
}