This is my first time I'm doing this type of sending so I hope you can help me, I have a web page in laravel 5.4 which on a page I have a label that shows me a number and I want that number to appear On another page using ajax but I do not know how to do it.
This is the code of my page that I have the label (which I want to send):
<div class="col-md-3 col-sm-2 siga-cantidad">
<label id="preciototalEmpresarialSiga">$25.000</label>
</div>
I want that value to appear in another label on my page / tests that I also have my controller and my route.
This is my route
Route::get('/prueba', 'PruebaController#index')->name('prueba');
This is my controller
class PruebaController extends Controller
{
public function index()
{
return view('pruebas');
}
}
And I do not know how to do with ajax to send me that data of my label. I hope I could be clear and they can help me, in advance many thanks to all.
Yes, I think I was not very clear ... I have this web page in laravel 5.4, it is in the path and in the controller
Route::get('/portal-de-calidad', 'PortalController#index')->name('portal');
class PortalController extends Controller
{
public function index()
{
return view('portal');
}
}
In this url(/portal-de-calidad) I have this code
<div class="col-md-3 col-sm-2 siga-cantidad">
<label id="preciototalEmpresarialSiga">$25.000</label>
</div>
What I want is that the "$ 25.00" that is inside the label is displayed in another web page .. I understand that with ajax can be done but I do not know how to start or where to put the code ...
Here I put the route and the driver of the page where I want the information appears obviously on another label ..
Route::get('/prueba', 'PruebaController#index')->name('prueba');
<label>"Here I want you to show me the information on the portal page"</label>
class PruebaController extends Controller
{
public function index()
{
return view('prueba');
}
}
I hope I have been clear and can understand and help me ... thanks ..!
It is not clear what exactly you are trying to achieve here but for sending data using ajax to a specific route following should suffice :
AJAX Call
function postLabel()
{
var value = $('#preciototalEmpresarialSiga').text();
$.ajax({
url : "{{url('/prueba')}}",
type : "GET",
data : { 'price' : value },
success:function(data){//200 response comes here
//Do what you want to do with processed data
},
error:function(e){
//Error handling
}
})
}
And in your Controller
public function index()
{
if( !Input::has('price') )//Check if input exist
return response('Bad request', 400);
$price = Input::get('price');
//Also you should validate your data here
//Process data
return $price;
}
Related
I'm very stuck here and I'm not sure the best course of action
Currently I have a blade and vue component that make up a dashboard. I have one route that loads the view for the dashboard, and another route (which is called inside the dashboard upon an event) which gets details for an ID.
This works perfectly right now.
The issue is that I want to be able to hit that 2nd route externally (like site.com/status/12345) and load the data for that id (12345) by default.
Right now I hit site.com/status and on page creation it calls fetchEntries to get data, I render a table based on that data with links, so if you click a link it passes the ID for that clicked entry into status/{id} to get details for that specific clicked entry. THen it loads the returned data into a details screen.
All I want to do here is make it so that if I call status/12345 it hits that route, and my controller details method. I know I can call that function on page creation as well, but I want to know how to handle the difference between site.com/status and site.com/status/12345.
What should I do here, and how do I go about it?
Here's what I have right now:
controller.php
public function index() {
return view('Entry.status');
}
public function details($id) {
return json_encode(Entry::getDetails($id));
}
web.php (route)
Route::get('/Entry/status', 'Entry\EntryController#index')
->name('Entry.status');
Route::get('/Entry/status/{id}', 'Entry\EntryController#details')
->name('Entry.status');
status.vue
created() {
this.fetchEntries();
},
fetchEntries() {
axios.get('/Entry/dashboard')
.then(response => {
this.events = response.data;
})
.catch(function(error) {
console.log(error)
})
.finally(function() {
})
},
getDetails(event) {
this.detailId = event.taskt_id;
axios.get('/Entry/status/' + this.detailId)
.then(response => {
...
}
if I understand your question correctly. I think you can do something like this.
public function details($id) {
if (request()->wantsJson()) {
return json_encode(Entry::getDetails($id));
}
return view('Entry.detail');
}
Is it what you want?
I asked a question about this earlier but I'm still stuck and having some issues.
Right now, if I go to site.com/status/12345 then I will get my status as I would expect. The route with the id (12345) hits a controller method which passes that id in and returns the json version of it in the view, where I'm using it as a prop and calling a function with it on page load.
However, if I just go to site.com/status (which should hit my index method, returning only the base view) then I get 'undefined variable entryKey'.
Obviously, I'm adding my entryKey value from the controller into my blade to be passed in as a prop, which seems to be working if there is a URL segment for it, but now I can't just get my base view to return with site.com/status
How can I fix this so that site.com/status hits my index method and site.com/status/12345 hits the detail method and passes the id in as a prop?
web.php (routes)
Route::get('/entry/status', 'entry\entryController#index')
->name('entry.status');
Route::get('/entry/status/{id}', 'entry\entryController#details')
->name('entry.status');
controller.php
public function index() {
return view('entry.status');
}
public function details($id) {
if (request()->wantsJson()) {
return json_encode(entry::getDetails($id));
}
$entryID = json_encode($id);
return view('entry.status')
->with('entryKey', $entryID);
}
status.blade.php
<entry-dashboard :entry-key="{{$entryKey}}"></entry-dashboard>
detail.vue
props: [
'entryKey'
],
created() {
this.setDetailWindow(this.entryKey);
},
setDetailWindow(event) {
this.detailId = event.taskt_id;
axios.get('/entry/status/' + this.detailId)
.then(response => {
...
...
}
In the below snippet, I have an html form in my example page with the form's action attribute set to exampleController URI in my Routes as follows:
htmlPage.blade.php
<form action="/exampleController" method="POST">
{{csrf_field()}}
// rest of form stuff goes here
</form>
#if(isset($result))
<table>
<td>{{ $result }}</td>
</table>
#endif
Web.php
Route::get('/gotoform', 'PageController#showform');
Route::post('/exampleController', 'PageController#processData');
Controller.php
class PageController extends Controller
{
public function showform()
{
return view('htmlPage');
}
}
public function processData(Request $request)
{
$result = $request['htmlInputData'];
return view('htmlPage')->with(compact('result');
}
}
Everything is fine from here and data is being processed and displayed correctly on the page but I was wondering why the URL address the controller returned for the $result variable is mydomain.com/exampleController and not mydomain.com/htmlPage which is the html page responsible for displaying the results of the operation.
Also, since the resulting URL address is mydomain.com/exampleController (which is not what I expected), its returning the following error when manually refreshed:
MethodNotAllowedHttpException in RouteCollection.php line 233:
Can somebody please enlighten me what did I missed?
thanks in advance.
EDIT:
by changing to Route::any the MethodNotAllowedHttpException error has gone away but still the URL returned by the controller is not right.
This happens because you return a View (view('htmlPage')).
if you want to access this url mydomain.com/htmlPage you need to redirect not load a View.
Use this return redirect(url('/htmlPage'))->with(compact('result');
In order to load this successfully you need to have another route :
Route::get('/htmlPage', 'PageController#loadhtmlPage');
and also to load the view in this url you have to do this in your controller:
public function loadhtmlPage(Request $request)
{
return view('htmlPage');
}
I have a bootstrap form where after filling data its successfully get inserted to database .Now i want to show detail view of form with filled data but it taking me back to create view after submit with data added to database. i guess i have problem with site url. for better understanding hereby i am attaching my code.
my create view file code is :
<form class="form-horizontal" id="job" action="<?php echo site_url('admission/add_students')?>" method="POST" name="job">
where as controllers(admission):
function add_students() {
$this->load->model('admission_detail_model');
$data=array(
'student_id'=>'La-0002'.$this->input->post('student_id'),
'father_name'=>$this->input->post('father_name'),
'mother_name'=>$this->input->post('mother_name'),
'fname'=>$this->input->post('first_name'),
'lname'=>$this->input->post('Last_name'),
'place_of_birth'=>$this->input->post('place_birth'),
'mother_tounge'=>$this->input->post('mother_tounge'),
'd_o_b'=>$this->input->post('DOB'),
'nationality'=>$this->input->post('nationality'),
'religion'=>$this->input->post('religion'),
'sc_st_obc'=>$this->input->post('sc_st_obc'),
'caste'=>$this->input->post('caste'),
'address'=>$this->input->post('address'),
'admitting_student'=>$this->input->post('Admit_std'),
'father_edu_qual'=>$this->input->post('father_q'),
'mother_edu_qual'=>$this->input->post('mother_q'),
'annual_income'=>$this->input->post('annual_income'),
'father_occupation'=>$this->input->post('father_occupation')
);
$this->admission_detail_model->add_students($data);
$this->index();
}
Model:
class Admission_detail_model extends CI_Model {
function add_students($data) {
$this->db->insert('students',$data);
return; }
Everything working fine i just want to add detail view after submit form not another create form View. For detail view i have controller defined in my base controller(admission) :
public function detailed_admission()
{
$this->load->helper('url');
$this->load->view('Header');
$this->load->view('side_menu');
$this->load->view('admission/detailted_view');
$this->load->view('footer');
}
when i try to replace site url in create view file
"<?php echo site_url('admission/detailted_view')?>"`
it does not enter data in database redirect directly to this view without any data.
This is my first question so if i had made any mistake please avoid it.
Thankyou for helping
Check my comments in the code.
function add_students() {
$this->load->model('admission_detail_model');
$data=array(
'student_id'=>'La-0002'.$this->input->post('student_id'),
'father_name'=>$this->input->post('father_name'),
'mother_name'=>$this->input->post('mother_name'),
'fname'=>$this->input->post('first_name'),
'lname'=>$this->input->post('Last_name'),
'place_of_birth'=>$this->input->post('place_birth'),
'mother_tounge'=>$this->input->post('mother_tounge'),
'd_o_b'=>$this->input->post('DOB'),
'nationality'=>$this->input->post('nationality'),
'religion'=>$this->input->post('religion'),
'sc_st_obc'=>$this->input->post('sc_st_obc'),
'caste'=>$this->input->post('caste'),
'address'=>$this->input->post('address'),
'admitting_student'=>$this->input->post('Admit_std'),
'father_edu_qual'=>$this->input->post('father_q'),
'mother_edu_qual'=>$this->input->post('mother_q'),
'annual_income'=>$this->input->post('annual_income'),
'father_occupation'=>$this->input->post('father_occupation')
);
//To insert the record in the database
$this->admission_detail_model->add_students($data); after
//open the detail view page
// $this->index(); why did you call index function ??
$this->detailed_admission(); //call detailed function just after the form submission or you can perform redirect('admission/detailed_admission')
}
I have created a fully custom view, I want this view to only show certain fields in an editview format so I can update records. But this view is to be different from the normal editview. How do I add a custom metadata file to this view that will allow me to define the form and fields that I need? The view is tied to a custom button and currently just shows "works". This is working so far just need to understand how to define the layout.
the controller:
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class CustomCasesController extends SugarController {
function action_resolve_Case() {
$this->view = 'resolve_case';
}
}
The view :
if (!defined('sugarEntry') || !sugarEntry)
die('Not A Valid Entry Point');
require_once('include/MVC/View/SugarView.php');
class CasesViewresolve_case extends SugarView {
public function CasesViewresolve_case() {
parent::SugarView();
}
function preDisplay() {
parent::preDisplay();
}
public function display() {
// include ('test.php');
echo "works";
}
}
Old, but still may help someone...
You can :
work inside the display function. Everything you do or echo here will be shown inside the main Sugar app screen (navbar, footer, etc) so it will look native.
Work directly inside the controller and echo everything out.
Build your form with the fields you want to edit and have as action a function in controller where you can use the bean->save() method for the POST results.
<form name="whatever" method="POST" action="index.php?module=Contacts&action=yourcustomaction">
ex:
`$contact = new Contact();
$contact->retrieve("{$_REQUEST['contactId']}");
//be sure to send the id in the button/link to the view
$contact->first_name =$_POST['first_name'];
$contact->last_name =$_POST['last_name'];
.....
$contact->save();`
Not very elegant but the only other option I know of is working with smarty templates which I'm not familiar with.
If anybody has a better solution, please post it.