hey guys i am new in codeigniter,i have a form like this
<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment/$id" name="application" method="post" >
//some code
</form>
i have a controller methode
function input_investment($id)
{
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('mod_user');
$this->mod_user->insertinvestment($id);
}
i want to get $id from form action to controller methode how can i do that . . pls help me . .
better to pass the value in the hidden field
<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment" name="application" method="post" >
<input type="hidden" name="my_id" value="<?php echo $id; ?>"/>
</form>
in your ci function
function input_investment() {
$id = $this->input->post('my_id');
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('mod_user');
$this->mod_user->insertinvestment($id);
}
or if you want (A test)
// Sample view
<?php $id = 1; ?>
<form action="<?php echo base_url('my_class/my_method/' . $id); ?>" method="post" >
<input type="submit" />
</form>
// Controller
class My_class extends CI_Controller {
public function index() {
$this->load->view('my_class');
}
public function my_method($id) {
echo $id; // outputs 1
}
}
You need to use PHP and echo $id in the element if you want the value, right now you're sending '$id' to input_investment($id).
<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment/<?php echo $id; ?>" name="application" method="post" >
//some code
</form>
Here your form method is post so you cont get the id through the get method ,you can do like
<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment" name="application" method="post" >
<input type="hidden" name='id' value="<?php echo $id;?>">
</form>
and in your controller you can try with post like
$id = $_POST['id'];
or
$id = $this->input->post('id');
it willl better option for you in all cases if you are trying to send single or multiple data to the controller from an form....
$route['ctl_dbcont/input_investment/(:num)'] = "ctl_dbcont/input_investment/$1";
Just add this line at your config/route :) .
This will work with numbers only, if you have other type of IDs you can use (:any)
Other option is to catch the id directly using :
$id = $this->uri->segment(3);
Where segment(3) is the third element after your domain :
http://domain/segment1/segment2/segment3
Related
I have a form contains one input (type button) and one image.
when i click on the button it supposed to delete the image (submit the form and get the value of the input which is the id of the image, using post method).
But i can't access the value of the input when i have it inside foreach loop.
because every input created inside foreach has the same name.
https://i.imgur.com/ed9Vv9m.png
i tried var_dump and there is just null value.
this is the form inside the camera view:
foreach($data['galleries'] as $gallery) :
?>
<div align=center>
<form action="<?php echo URLROOT; ?>/gelleries/camera"
method="post">
<input type="button" class="button" name="delete" id="abc"
value="<?php echo $gallery->galleryId; ?>" onclick="return
Deleteqry(<?php echo $gallery->galleryId; ?>);">
</div>
</form>
<?php endforeach; ?>
and this is the controller:
<?php
class Galleries extends Controller {
$this->galleryModel = $this->model('Gallery');
}
$galleries = $this->galleryModel->hiFive();
$datashow = [
'galleries' => $galleries
];
.....
public function camera(){
if (isset($_POST['delete']) && !empty($_POST["delete"])){
$imgid = $_POST["delete"];
$this->galleryModel->deleteimg($imgid);
echo "deleted!";
exit;
}
else
echo "error";
$this->view('/galleries/camera', $datashow);
}
and this is the model where i execute the queries:
<?php
class Gallery {
private $db;
public function __construct(){
$this->db = new Database;
}
.....
public function deleteimg($id){
$this->db->query("DELETE FROM galleries WHERE id = :id");
$this->db->bind(':id', $id);
if($this->db->execute()){
return true;
} else {
return false;
}
}
}
The Deleteqry inside onclick event of the button it's just a function where i check if i get the id of the image when i click on the button:
function Deleteqry(id)
{
if(confirm("Are you sure you want to delete this row?")==true)
window.location="http://localhost:8001/camagru/galleries/camera?
&del="+id;
return false;
}
Add hidden input field in your form block with value of gallery ID, like this:
<form ...>
<input type="button" class="button" name="delete" value="DELETE NOW">
<input type="hidden" name="GallID" value="<?php echo $gallery->galleryId;?>" >
</form>
And in your controller read that value from hidden field:
if (isset($_POST['delete']) && isset($_POST['GallID']) && !empty($_POST["delete"])){
$imgid = $_POST["GallID"];
$this->galleryModel->deleteimg($imgid);
echo "deleted!";
}
This works completly without javascript.
Iam a newbie in Codeigniter , Iam learning it from watching videos , the instructor did the same what I did , But it gives me an error like this "The action you have requested is not allowed." ,and it worked with him, I don't know why, any help ! .
this is my Controller code
public function index(){
if($this->input->post('submit')){
echo $this->input->post('first_name');
}
$this->load->view('forms');
}
this is my View code
<form method="POST">
<input type="text" name="first_name" />
<input type="submit" name=submit" />
</form>
Use form_open() helper which automatically adds hidden input with CSRF value.
So your view should be:
<?php echo form_open('action_url'); ?>
<input type="text" name="first_name" />
<input type="submit" name=submit" />
<?php echo form_close(); ?>
Disabling CSRF protection also works but it's a bad idea.
you almost right, you need to add some parts like action in your form, and isset or empty in your controller like
class Test_form extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->view('form_test');
}
//using your example. good
public function check_form(){
if( isset($this->input->post('first_name', TRUE))){
echo "success <br>$$this->input->post('fisrt_name', TRUE)";
}
else{
echo "error";
}
}
//using form_validation. best
public function check_form_validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('first_name', 'first Name', 'trim|required|xss_clean');
if( ! $this->form_validation->run()){
echo "error <br>" . validation_errors();
}
else{
echo "success <br>$$this->input->post('fisrt_name', TRUE)";
}
}
}
form_test.php
first method
<form method="post" action="<?= base_url()?>index.php/test_form/check_form">
<input type="text" name="first_name">
<input type="submit" value="test">
</form>
<hr>
second method
<form method="post" action="<?= base_url()?>index.php/test_form/check_form_validation">
<input type="text" name="first_name">
<input type="submit" value="test">
</form>
I'm new to opencart. I have to write a custom Log-in form for users. Then i design a small code for log-in form in opencart like below. path is (MyTheme/temlate/auth/Sign.tpl)
<form action="<?php echo $Sub; ?>" method="GET" enctype="multipart/form-data">
Name:<Input type="text" name="txtUser">
<br>
Password:<input type="password" name="txtPassword"><br>
<input type="submit">
and controller is like (Path is controller/auth/Sign.php)
<?php
class ControllerAuthSign extends Controller{
public function index() {
$data['Sub']=$this->url->link('auth/result','','SSL');
if(file_exists(DIR_TEMPLATE . $this->config->get('config_template'). '/template/auth/sign.tpl')){
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/auth/sign.tpl',$data));
}
else{
$this->response->setOutput($this->load->view('default/template/account/login.tpl'));
}
}
}
?>
when a user submit the form have to navigate to Result page (Path is /auth/result.tpl)
<?php
echo "Welcome : Mr./Mrs. ".$User;
?>
<br><p>Your are Loged-In</p>
and the controller for Result is.. (Path is /auth/result.php)
<?php
class ControllerAuthResult extends Controller{
public function index() {
$data['User']=$_REQUEST['txtUser'];
$data['Password']=$_REQUEST['txtPassword'];
if(isset($data)){
$this->response->redirect($this->url->link('auth/sign', '', 'SSL'))
}
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/auth/result.tp',$data));
}
}
?>
but the problem is when i click on submit , page navigate to
http://localhost/opencart/index.php?txtUser=Narayana&txtPassword=narayana
and displayed index page. Can any one help how to navigate to result page...?
Thanks in Advance.
Use this
<form action="<?php echo $Sub; ?>" method="POST" enctype="multipart/form-data">
Name:<Input type="text" name="txtUser">
<br>
Password:<input type="password" name="txtPassword"><br>
<input type="submit">
I am trying to create a data-entry,with controller which inputs the data, displays it and confirm for any edit then uses a method to submit it to the model.
The problem is that in the load the model in the function post_data() the value of $this->input->post() return an empty array.
I am entering the data returning to the get_data function and then displaying it in the data.php in the view.
using
data.php in view post the data to the post_data method.
<form id='form' action="<?php echo base_url("welcome/post_data"); ?>" method="POST" style="display:inline;">
<input type="text" name="xyz" value="<?php echo $this->input->post("xyz") ?>" />
the controller is
protected $arr;
public function index(){
$this->load->view('index/index');
// $this->load->library('Controllerlist');
// print_r($this->controllerlist->getControllers());
}
public function get_data(){
echo "matoercod";
$this->load->view("index/data");
}
public function post_data(){
$this->load->model("form1","form",TRUE);
print_r($this->input->post());
$blue=$this->form->insert_data($this->arr);
print_r($blue);
if($blue){
echo "Successfully added to database";
}
}}
Why does print_r() method return an empty array?
$this->input->post() in the post_data method return empty array.
if Iam right $this->input->post() should is global to all the method in Controller CI class.
Your input tag in form doesn't have a name attribute.
<form id='form' action="<?php echo base_url("welcome/post_data"); ?>" method="POST" style="display:inline;">
<input name="xyz" type="text" value="<? php echo $this->input->post("xyz") ?>" />
</form>
Edit:
Also the input tag is closed in a wrong way (closing before the value attribute).
<input type="text name="xyz" value="<? php echo $this->input->post("xyz") ?>" />
If you are loading the view data.php in get_data:
public function get_data(){
echo "matoercod";
$this->load->view("index/data");
}
Then how are you accessing the get_data url? If you are just typing it in the browser, then you are not POSTing you are GETing and so $this->input->post is always going to be an empty array.
It's not entirely clear to me how you are calling your method get_data, but you should probably try to alter it so that it more carefully constructs the data you want to be displayed in any view that it loads
public function get_data(){
$view_data = array(
"xyz" => "here is some value" // you could get this value from anywhere
);
$this->load->view("index/data", $view_data);
}
Then in your view data.php you would want to refer to just $xyz instead of $this->input->post("xyz"):
<form id='form' action="<?php echo base_url("welcome/post_data"); ?>" method="POST" style="display:inline;">
<input type="text" name="xyz" value="<?php echo $xyz; ?>" />
Note that second parameter to the view loading function. It's an array and each associative key in the array will be expanded into a variable within your view.
I have to devellop an internal web based application with codeigniter and I need to chain different forms (generate upon data choosen with previous form).
Right now, I tried to use form validation in the same method of the controller but the chaining only validate the first form, I tried also with $_SESSION variables but I have to send a large amount of data between each form. I tried with class variable (in controllers and models) but every time the form is send the variable are initialise...
So i wonder if there is a way to switch from a method to another one in my controller giving the data to the new controller.
my first form:
<p>Filtres: </p>
<br/><br/>
<form action="" method="post" id="form_ajout_manip" >
<label for="thematique[]">Thématique</label><br/>
<select name="thematique[]" size="20" multiple>
<?php
foreach($list_thema->result() as $thema)
{
echo "<option value='".$thema->THEMATIQUE_ID."'>".$thema->PARENT_THEMATIQUE_ID." - ".
$thema->NOM."</option>";
}
?>
</select>
<input type="hidden" value="true"/>
<br/>
<br/>
<br/>
<input type="submit" value="Rechercher" />
</form>
my second form:
<form action="" method="post" id="form_ajout_manip_cdt">
<label for="nom_manip" >Nom manipulation: </label>
<br/>
<input type="text" name="nom_manip"/>
<TABLE border="1">
<CAPTION><?php echo $data->num_rows.' '; ?>resuuultat</CAPTION>
<TR>
<?php
foreach($data->list_fields() as $titre)
{
echo '<TH>'.$titre.'</TH>';
}
?>
</TR>
<?php
foreach($data->result() as $ligne)
{
echo '<TR>';
foreach($ligne as $case)
{
echo '<TD>'.$case.'</TD>';
}
echo '<TD><input type="checkbox" name="cdt[]" value="'.$ligne->ID_CANDIDAT.'"
checked="true"</TD>';
echo '</TR>';
}
?>
</TABLE>
<br/><br/>
<input type="submit" value="créer"/>
</form>
Those are the two method of my controller
public function choix()
{
//controller for the second form
$this->info_page['title']='Ajout manipulation';
$this->load->view('ui_items/header',$this->info_page);
$this->load->view('ui_items/top_menu');
$this->load->view("manipulation/choix",$data);
}
public function filtre()
{
//controller for the first form
$this->form_validation->set_rules('thematique[]','Thematique','');
if($this->form_validation->run())
{
$data['data']=$this->manipulation_mod->select_par_filtre($this->input->post('thematique'));
//need to send $data to the second method "choix()"
}
else
{
$this->info_page['title']='Filtre ajout manipulation';
$this->load->view('ui_items/header',$this->info_page);
$this->load->view('ui_items/top_menu');
$data= array();
$data['list_op']= $this->candidat_mod->list_operateur();
$data['list_thema']= $this->thematique_mod->list_all_thematique();
$data['list_gene']= $this->candidat_mod->list_gene();
$this->load->view('manipulation/filtre', $data);
}
}
Have you any idea? I totally stuck...
Based on your clarification, let me give you an outline on what will work
View
Have both the forms in the same page
<? if(!$filtered): ?>
<input type="hidden" name="filtered" value="true"/>
/* Form 1 content here */
<? else: ?>
<input type="hidden" name="filtered" value="true"/>
/* Form 2 content here */
<? endif; ?>
Controller
You just need to use one controller
public function filter() {
$filtered = $this->input->post('filtered');
$data['filtered'] = $filtered;
if(empty($filtered)) {
/* Form validation rules for Form 1 */
/* Run form validation etc. */
/* Set title etc. for Form 1 */
} else {
/* Form validation rules for Form 2 */
/* Run form validation etc. */
/* Set title etc. for Form 2 */
}
/* Load view */
}
There might just be a better way to do this, but I am sure this will work. Good luck!