i have to pass multiple variables in form_open() in codeigniter - codeigniter

i want to pass two variables in view page
my code.
$user=$this->session->userdata('username');
$fyear=$this->session->userdata('fyear');
echo form_open('Erp_c/usercustomerinsert/'.$user);?>
I want to pass the other variable $fyear too..
I tried this.
<?php
$user=$this->session->userdata('username');
$fyear=$this->session->userdata('fyear');
echo form_open('Erp_c/usercustomerinsert/'.$user.$fyear);?>
value is not getting.

Try this
<?php
$user=$this->session->userdata('username');
$fyear=$this->session->userdata('fyear');
$add = $user.$fyear;
echo form_open('Erp_c/usercustomerinsert/'.$add);
?>
and in config/autoload.php
$autoload['helper'] = array('url','form','html');

You can pass it using by using hidden variable
$user=$this->session->userdata('username');
$fyear=$this->session->userdata('fyear');
$attributes = array('user' => $user, 'fyear' => $fyear);
echo form_open('Erp_c/usercustomerinsert/','', $attributes);
The above code make you form like
<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/Erp_c/usercustomerinsert/">
<input type="hidden" name="user" value="user_VALUE" />
<input type="hidden" name="fyear" value="fyear_VALUE" />

Related

Route [profile.store] not defined

My Form tag is
<form action="<?php echo route('profile.store'); ?>" method="post">
<input type="hidden" name="_token" id="csrf-token" value="<?php echo csrf_token(); ?>" />
and my web.php file have
Auth::routes();
Route::view('/','welcome');
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/profile','ProfileController#index');
Route::get('/profile/add','ProfileController#create');
my ProfileController have store function
public function save(Request $request){
print_r($request);
}
First, you don't have the store method in your ProfileController, instead of that, you are using save method. So, you can do that.
Create a new route for your save method in your web.php
Route::post('profile/save', ProfileController#save)->name('profile.store');
Then, your final code will be:
In your view:
<form action="<?php echo route('profile.store'); ?>" method="post">
<input type="hidden" name="_token" id="csrf-token" value="<?php echo csrf_token(); ?>" />
In your controller:
public function save(Request $request){
print_r($request);
}
In your web.php
Route::post('profile/save', ProfileController#save)->name('profile.store');
First you need to define the route in your web.php, and secondly you need to name a route as profile.store.
https://laravel.com/docs/master/routing#named-routes
If you use the resource function then the routes are named already.
https://laravel.com/docs/5.0/controllers#restful-resource-controllers
You can see the list of available routes using php artisan command.
php artisan route:list
More help about this command:
http://laravel-school.com/posts/laravel-php-artisan-route-list-command-9
You don't have route named profile.store.
Try changing the form tag from
route('profile.store');
to this
action('ProfileController#store');
Note: I am assuming you have that route (as you have not shown it in the question)
Route::post('/profile','ProfileController#store');

retrieving user input in code igniter but not getting the value inside a foreach loop

I am working with code igniter and got stuck when i compare the user input with the already existing values in database. I have a form from where i get a user input. The form is as follows:
<form method="post" action="my_controller/my_method">
<label>Mark :</label>
<input type="text" name="avg_mark"><br>
<label> Message </label>
<input type="text" name = "message"><br>
<input type="submit" class="btn btn-info" value="send message">
</form>
In my_controller/my_method
function my_method{
$avg_mark =$this->input->post('avg_mark');
$message = $this->input->post('message');
echo $avg_mark;
}
when i echo $avg_mark iam getting the value. But problem arises when i take that value inside a foreach loop.
foreach($students as $row){
echo $avg_mark;
}
the page is blank and shows nothing which means iam not getting the input value inside the loop. And i tried this too inside the loop. Still the result is same.
foreach($students as $row){
echo $this->input->post('avg_mark');
}
What should i do to get the value inside a foreach loop.
First check that variable empty or not. Then you pass it in to foreach loop:-
if (!empty($students)) {
foreach($students as $row){
echo $avg_mark;
}
}

form submit in codeigniter going to blank page

I am submitting form using form helpers and it's going to blank page and when I see the view source, the action in the view source is like this http://::1/ci1/login_validation I don't understand the ::1 in it shouldn't it be localhost:8080/? But if I use simple form tags like regular html it works fine?
<?php
echo form_open ('login_validation');
echo form_input('email');
echo form_password('password');
echo form_submit('login_submit', 'login');
echo form_close();
?>
View Source
<form action="http://::1/ci1/login_validation" method="post" accept-charset="utf-8">
<p>Email:<input type="text" name="email" value="" /></p>
<p>Password:<input type="password" name="password" value="" /></p>
<p><input type="submit" name="login_submit" value="login" /></p>
</form>
I would think it is a couple of thing like.
If your form action has http://::1/ then does not submit correct because base url is empty
$config['base_url'] = '';
Fill base url example:
$config['base_url'] = 'http://localhost/project_name/';
On codeigniter 3 version not best idea to leave it blank because you will run in to that issue.
Make sure your class and file names of controllers etc have first letter upper case.
Filename: Login_validation.php
Controller:
<?php
class Login_validation extends CI_Controller {
public function index() {
// form submit controller code.
}
}
View
<?php
echo form_open ('login_validation');
echo form_input('email');
echo form_password('password');
echo form_submit('login_submit', 'login');
echo form_close();
?>
This is a common mistake people for get to check.
Why http://::1/??
This mean your base_url() is empty.
Then how this http://::1/ comes??
When your project URL is empty, CI detect your project URL. So this http://::1/ knows your project path.
How to set base_url()??
In config/config.php set $config['base_url'] = ''; the project URL.
Keeping base_url() empty any harm??
When you in local its ok and fine. But when you host that just add your site URL to it.
$config['base_url'] = 'www.stackoverflow.com';

Laravel 5 Plain Html MethodNotAllowedHttpException

Hello so I am trying to make an update form in Laravel using plain html. The url of my edit form is http://localhost:8000/profile/sorxrob/edit, sorxrob is the username. And the code in that url is:
<form class="form-horizontal" role="form" action="" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
//more inputs
</form>
And in my controller named AccountController:
public function update(Request $request, $id)
{
$account = Accounts::findOrFail($id);
$input = $request->all();
$account->fill($input)->save();
return 'success';
}
And I am getting the error MethodNotAllowedHttpException when I click the update button. Is it because my action is equal to nothing? If yes, what is the correct way of routing there?
This is due to your action url which is not correct routing url. Use the following
(1) First define a route in your route.php file
Route::post('profile/{username}/edit', array('as' => 'profile.update', 'uses' => 'AccountController#update'));
(2) Change your action attribute from your form tag
action="{{ URL::route('profile.update', [$username]) }}"
here $username variable will be passed from your AccountController#edit method.

How to create new records in multiple tables at the same time?

I was wondering how can I implement a view that allows the user to enter multiple(unknown) number of records in related models..
for example, I have two models called groups and users. And I want to implement a view that allows me to create a group ( which is easy ) and in the same view let me create multiple number of users who belong to this group.
I hope that my question is clear, if not just let me know ..
In the backend, use a saveAll($data), preparing your data like:
// Prepare the data for the Group hasMany User
$data = array('Group' => array(
'id' => 5,
'name' => 'my group',
'User' => array(
array('id'=>1,'username' => 'john'),
array('id'=>2,'username' => 'pachelbel'))
)
)
// OR if it's being passed in, just use $this->data in place of $data
// Save all the data
$this->Group->saveAll($data)
In the view, you want to prepare your form such that it has correctly array'd keys:
<form id="GroupCreateForm" method="post" action="/groups/create">
<input type="hidden" name="data[Group][id]" value="5" />
<input type="hidden" name="data[Group][name]" value="my group" />
<input type="hidden" name="data[Group][User][0][id]" value="1" />
<input type="hidden" name="data[Group][User][0][username]" value="john" />
<input type="hidden" name="data[Group][User][1][id]" value="2" />
<input type="hidden" name="data[Group][User][1][username]" value="pachelbel" />
</form>
If you want to dynamically add users to the form, consider using javascript to inject new input elements.
See this for more info.
You can do it smarter with using Cake's Form helper. In the example above you can rewrite like this:
<?php
echo $this->Form->Create('Group');
echo $this->Form->input('Group.id');
echo $this->Form->input('Group.name');
for($i=0;$i<10;$i++){
$this->Form->input('User.'.$i.'.id');
$this->Form->input('User.'.$i.'.name');
}
?>
This way you can benefit from auto error handling as well as field values population.

Resources