I'm loading a view and I'm passing 2 arguments from a controller like this:
$this->load->view('confirm_data_v', $data);
In confirm_data_v view I'm receiving $data that is an array. In confirm_data_v I have a Submit button, but I want to call another controller when I click on it. The code of the button in confirm_data_v is:
<div class = "container">
<div class="form-actions">
<button type="submit" class="btn btn-success">Save</button>
</div>
</div>
confirm_data_v is a view to confirm to the user the information that he gave, so when he clicks "Save" button, it calls the controller to save it to the database.
You cant normally call a controller directly from a view - but you dont need to just make the button redirect to or ajax call any url you like (mysite.com/myurl in this example) on the button click and then route that to your controller:
$route['myurl'] = 'mycontroller';
Your main options for storing the data between the two pages are either storing in a session or a cookie or echoing all their data into hidden form fields and re-posting it (if it's sensative data - go with the session option):
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
If for whatever you really do want to call a controller from a view you ca do it using wiredesigns modular codeigniter : https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
But generally speaking this flies in the face of the mvc architecture and there is no need to do it.
What i have get from your question that you want to submit data to controller n#1 from the view after loading it from a controller n#2
if this is the case you can do
Method # 1
<?php echo form_open('controller/function'); ?>
<div class = "container">
<div class="form-actions">
<button type="submit" class="btn btn-success">Save</button>
</div>
</div>
<?php echo form_close(); ?>
and you can access any other value with $this->post->input('input_name')
Method # 2
use Anchor instead of submit button
<div class = "container">
<div class="form-actions">
<?php echo anchor('controller/function/parm','Save',array('class'=>'btn btn-success')); ?>
</div>
</div>
and in your controller for example
function confirm_save($parm)
{
//save to database
//redirect back to the next controller you want with success message for example using session flash data
//or load another view but URL will be still the same whatever suits your case
}
Related
route not working, where should I put #csrf and #method('DELETE')? because it does not delete the user. before the problem happens, #csrf and #method('DELETE') was in blade view. But when I put #csrf and #method('DELETE') in return it shows an error.
$employees = DB::table('users')->leftjoin('roles', 'users.role_id', '=', 'roles.id')->leftjoin('supervisors', 'users.manager_id', '=', 'supervisors.id')
->select(['users.id','users.name','users.department','users.email','users.leaves_available','roles.name_role','supervisors.name_supervisor']);
return Datatables::of($employees)
->addColumn('action', function ($employees) {
return '<form action="'.route('employee.destroy', $employees->id).'" method="post">
<button type:"submit" class="btn btn-sm btn-danger">Delete</button>
</form>
</div>
</div>
</div>
</div>';
})->make(true);
in the form I use route('employee.destroy', $employee->id) then the parameter will be http://localhost:8000/employee/1 and I want the user got delete.
EmployeeController.php
public function destroy($id)
{
DB::table('users')->delete($id);
return redirect()->route('home')
->with('success','Employee have been deleted');
}
This is my web.php
Route::get('employee/{id}', 'EmployeeController#destroy');
I expect the destroy function will run and redirect back to home. But what I get is :
The POST method is not supported for this route. Supported methods:
GET, HEAD, PUT, PATCH, DELETE.
Change the route from get to post. Like this
Route::post('employee/{id}', 'EmployeeController#destroy');
Because, you are using post method in your form but your route is in get. Both need to be same.
I am trying to send my URL ID value through ActivForm hidden input, but I didn't get the value.
<?= $form->field($modelsRoo, "[{$index}]booking_id")->hiddenInput(
['value'=> '$b_id'])->label(false); ?>
I am getting the value from Current Url like this.
and trying to pass it to Activeform HiddenField and on submitting this form I would like to save it in my Table.
One thing more I am using dynamicformwidget.
I am getting the value from Current Url like this:
<div class="col-sm-6">
<?=$form->field($modelsRoo, "[{$index}]unit")->textInput(['maxlength' =>true])?></div>
<div class="col-sm-6">
<?= $form->field($modelsRoo, "[{$index}]booking_id")->hiddenInput(
['value'=> '$b_id'])->label(false); ?>
</div>
When I submit the form I get booking_id value as Empty.
Any solution.
I am displaying a form to user with some details and then user clicks approve and reject. In the backend, I want to take this in one property - userAction, which can be "approve" or "reject".
How can I add input to the buttons that user clicks? And then this input is part of the object requestDto.
<form th:action="#{/mission/store/{uuid}(uuid=${uuid})}" th:object="${requestDto}" method="post" class="mission_form">
<div class="wizard-header">
<h3 class="wizard-title">
Approve Mission
</h3>
<h5>Should you chose to accept this mission, press approve.</h5>
</div>
<div class="wizard-footer">
<!--<div class="pull-right">-->
<input type='button' class="btn btn-success" name='approve' value='Approve' />
<!--</div>-->
<div class="pull-left">
<input type='button' th:field="*{}" class='btn btn-danger' name='previous' value='Decline' />
</div>
<div class="clearfix"></div>
</div>
</form>
First define a proper DataTransferObject:
public class MyRequestDto {
private String userAction;
// don't forget getters and setters
}
Then add a object of that class to your model
// if you are return a M&V-object:
ModelAndView mv = new ModelAndView("viewName")
ModelAndView.addObject("requestDto", new MyRequestDto());
// if you define a Model-Object as input-parameter:
Model.addAttribute("requestDto", new MyRequestDto());
Define a form like this (I used button-elements). The point is not to use the th:field attribute:
<form th:action="...." method="POST" th:object="${requestDto}">
<button name="userAction" value="approve" >Approve</button>
<button name="userAction" value="reject" >Reject</button>
</form>
Recieve the DataTransferObject by adding this to your controllers input-paramters (the controller that handels the post request):
... #Valid RequestDto requestDto, BindingResult bindingResult, ....
Now you can access requestDto's userAction-Attribute. The value is approve if you click the first button and it is reject if you click the second button. First you can check if there are binding errors by checking bindingResults.hasErrors().
I am new in codeigniter, I want to send link with data flash message in codeiniter how can I send the link?
this is my session message.
$this->session->set_flashdata('message', '<div id="message" class="alert alert-error">Your account is not active yet. Please activate your account.</div>');
I want to send link like that
<a href="<?php site_url().'/home/link';?></a>
how can I send, please help me to to this thanks in advance
In the controller create a link and assign it in a variable like this
function index(){
$link = site_url('home/link');
//Now assign this variable in flash and redirect
$this->session->set_flashdata('message', '<div id="message" class="alert alert- error">Your account is not active yet. Please activate your account.</div>');
$this->session->set_flashdata('link', $link);
redirect('othercontroller/method');
}
I am creating a site in which I utilize partial views to display various bits of data about a single Model. Here is a bit of the HTML. (Note, all of these are contained within a single form and the Index page that these partials are rendered in is strongly typed to the main model. The main model contains various lists of data.)
<div id="tab1"><% Html.RenderPartial("Tab1", Model); %></div>
<div id="tab2"><% Html.RenderPartial("Tab2", Model.AnItemList1.FirstOrDefault<AnItemList1>()); %></div>
<div id="tab3"><% Html.RenderPartial("Tab3", Model.AnItemList2.FirstOrDefault()); %></div>
Here is ONE of the partial views headers (for 'tab2'):
<%# Language="C#" Inherits="System.Web.Mvc.ViewUserControl<AnItem1>" %>
The pages display correctly. The issue is that, when I enter data into the various parts of the partial pages and then submit the entire form (via POST), the data is not making it back to my data store (MSSQL) - but this only happens for any of the list items (that are contained within the Model). The first partial page does properly have its data set within the data store.
What am I doing wrong here? Should I only be passing the model to Html.RenderPartial and then get the specific model I need on the partial page? Should I pass the entire list and then get the first (right now, I only care about the first item in the list - that will EVENTUALLY change, but not any time soon).
Suggestions or thoughts appreciated.
Update: Here is how I accessing the properties on the partial views.
<div class="data-group">
<%: Html.CheckBoxFor(model => model.Property1) %>
<%: Html.LabelFor(model => model.Property1) %>
</div>
Update 2: Per request...
Controller Action (ScenarioController):
public ActionResult Index(int id = 0)
{
if (id == 0)
{
SavedScenario scenario = new SavedScenario();
scenario.AnItemList1.Add(new AnItem1());
scenario.AnItemList2.Add(new AnItem2());
return View("Index", scenario);
}
else
{
SavedScenario scenario = repository.GetScenario(id);
if (scenario == null)
return View("NotFound");
else
return View("Index", scenario);
}
}
[HttpPost]
public ActionResult Index(SavedScenario scenario)
{
if (ModelState.IsValid && TryUpdateModel(scenario, "SaveScenario"))
{
repository.Add(scenario);
repository.Save();
}
return View(scenario);
}
Rendered HTML (I can only include parts of it - this is a small sample of what is in the form):
<form action="/Scenario" id="form0" method="post">
<!-- This is the one that works - the basic Scenario. Top level. -->
<fieldset>
<legend>Scenario Information</legend>
<div class="data-group">
<div class="editor-label">
<label for="ScenarioName">Scenario Name</label>
</div>
<div class="option1">
<input class="wide" id="ScenarioName" name="ScenarioName" type="text" value="" />
</div>
<div class="validation">
<div><span class="field-validation-valid" id="ScenarioName_validationMessage"></span></div>
</div>
</div>
</fieldset>
<!-- This does not work or get submitted (as far as I can tell). -->
<div id="tab2">
<fieldset>
<legend>Tab2</legend>
<div class="data-group">
<input id="Property1" name="Property1" type="checkbox" value="true" /><input name="Property1" type="hidden" value="false" />
<label for="Property1" />
</div>
</div>
</fieldset>
</form>
My apologies for having to keep this so generic.
Hard to guess from this much code. However you should make sure that all properties of your models have the same prefix when they are posted back to the server
Edit: form field names should match property names of your model to correctly bind all values. You have two fields with the same name that you can bind in following way
[HttpPost]
public ActionResult Index(SavedScenario scenario, List<bool> Property1)
{
// here you can do with values coming in property1
if (ModelState.IsValid && TryUpdateModel(scenario, "SaveScenario"))
{
repository.Add(scenario);
repository.Save();
}
return View(scenario);
}
It might be issue with naming the fields on your partial forms. Try naming the fields on your partial views by prefixing it with the name of the Model passed into it...like 'AnItemList1.name' instead of just 'name'..I am just guessing here though...but that's what I did sometimes to fix the problem when I was getting values as null..