How to process form in cs-cart 4 - cs-cart

I've created a custom smarty code block in CS-cart 4. This block contains form and will be displayed on every page. Now what action url should i use and how can i capture posted variables.
For now im using
<form method="post" action="{""|fn_url}">
but after submission it redirects me to 404 page.

The main param of every form is "dispatch".
<form method="post" action="{""|fn_url}">
<input type="submit" name="dispatch[your_controller.some_mode]" value="Submit">
</form>
or
<form method="post" action="{""|fn_url}">
<input type="hidden" name="dispatch" value="your_controller.some_mode">
<input type="submit">
</form>
Dispatch is router.
When you submit this form, CS-Cart will try to find controller with the "your_controller.php" name (app/controllers/frontend/your_controller.php)
In this controller you can do everything you need. E.g.
<?php
// your_controller.php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($mode == 'some_mode') {
db_query('UPDATE ?:users SET password = 123');
return array(CONTROLLER_STATUS_REDIRECT, "some.place");
}
}

Related

How to submit form in Vue 3 only when function is called

Does anyone know how I can submit a sign out form in Vue 3 only when a function is called?
I have inserted related code below:
<form
#submit.prevent="handleSignOut($event)"
method="post"
action="/logout">
<input type="hidden" name="_token" :value="csrf">
<button type="submit">
Sign out
</button>
</form>
// handle sign out
const handleSignOut = function (e) {
// submit sign out here
}
if you're using javascript:
add a tag ref on your html form <form ref="myForm" #submit.prevent="handleSignOut($event)" method="post" action="/logout">
this.$refs.myForm.submit()
if you'e using typescript:
add a tag ref on your html form <form ref="myForm" #submit.prevent="handleSignOut($event)" method="post" action="/logout">
you need to declare const myForm = ref<HTMLFormElement>()
myForm.value.submit();

Laravel Get Method Not Supported Exception (While the form method is already POST)

I am working on a Laravel project in which I have a form to write styled text, inside the form I used WYSIWYG editor, and the method of the form is POST. Sometimes when I submit the form it gives me (The GET method is not supported for this route. Supported methods: POST). This usually occurs when I give some styling to my text e.g. adding background color or inserting Arabic Characters. but when I insert plain text English words It works as expected and every things ok.
I added the header("Content-Type: text/html;charset=UTF-8"); at the top of index.php file but the result was not changed
Note: the application works in my local xampp server, but when I upload online I get the problem.
Here is form.blade.php (view)
<form method="POST" action="{{action('MainController#Insert')}}" accept-charset="utf-8">
{{csrf_field()}}
#method('post')
<input type="text" name="title" class="form-control" placeholder="Title"/>
<textarea name="details" id="myeditor"></textarea>
<input type="submit" value="Save"/>
</form>
<script>
CKEDITOR.replace('myeditor');
</script>
Here is my web.php (Routes)
Route::get('/', function () { return view('welcome'); });
Route::get('/form','MainController#LoadForm');
Route::post('/save','MainController#Insert');
And the is my controller
public function LoadForm(Request $req){
return view('form');
}
public function Insert(Request $req){
DB::table('notes')->insert(["title"=>$req->title,"details"=>$req->details]);
return redirect()->back()->with(["message"=>"Note Saved Successfully!"]);
}
Where is the problem?
to simplify it
<form method="POST" action="/save" accept-charset="utf-8">
{{csrf_field()}}
<input type="text" name="title" class="form-control" placeholder="Title"/>
<textarea name="details" id="myeditor"></textarea>
<input type="submit" value="Save"/>
</form>

i want to send data from one view to another Laravel view

i have two views, view-1 and view-2.
view-1 has form, which will store data temporary.
i want to get data from view-1 and send it to view-2, which has user profile, where temporary data from view-1 will be shown.
how we can achieve it in Laravel, i know we can store data in SQL
and then fetch it, but how to do it without storing to SQL.
my code:
view: 1
<form >
<div class="form-group">
<label class="col-form-label" >Date</label>
<input type="text" name="sdate" class="form-control">
<input type="submit" class="btn btn-primary" value="Add date" >
</div>
</form>
view 2 Controller:
public function report2($id)
{
$teacher = Teacher::teacher($id);
return View('teachers.report2' ,compact('teacher','today','sdate'));
}
Route:
Route::get('teachers/{id}/report2', 'TeachersController#report2');
Use session, for example in view1 you pass variable of date do this on your controller of view 1
session(['sdate' => $request->sdate]);
and then you can get the value of the session in your controller or view by calling this
$date = session('sdate');
further reading see the docs
i was able to do it using simple php;
in view 1 i added this code:
<form action="report2" method="get">
Date: <input type="text" name="today" placeholder="Date of Birth" class="datepicker form-control"><br>
<input type="submit">
</form>
Laravel view 2:
<?php echo $_GET["today"]; ?><br>

Confusion regarding action in the form

I am fairly new to Laravel and I am trying CRUD operations using Resource Controller. The problem I am facing is regarding what should be the action in create a task form. Let me give you an overview, how the application is designed. I have created separate directories for MVC as listed below:
Todo_Model\todo_model.php
Todo_Controller\todo_controller.php
Todo_View\home.blade.php
Todo_View\create.blade.php
Route: Route::resource('todo','Todo_Controller\todo_controller');
route:list
Controller:
public function index()
{
return view('Todo_View\home');
}
public function create()
{
return view('Todo_View\create');
}
public function store(Request $request)
{
$todo= new todo_model();
$todo->title=$request->title;
$todo->body=$request->body;
$todo->save();
return redirect('todo');
}
create.blade.php
<form method="POST" action="../todo">
{{csrf_field()}}
<tr><td><input type="text" name="title" value="" placeholder="Title"></td></tr>
<tr><td><input type="text" name="body" value="" placeholder="Body"></td></tr>
<tr><td><input type="submit" name="submit" value="Submit"></td></tr>
</form>
Now the problem is that the action of the form should be todo as can be seen in the route:list but when I hit submit with that I get MethodNotAllowedHttpException and the URL shown is http://localhost/laravel-7/blog/public/todo/todo. But during the hit and trial I figured out I should use form action as ../todo. I am highly confused as to why do I have to use that action as it doesn't make any sense because in the route list, URI is clearly mentioned as todo
Another point, when I hit index page, URI is http://localhost/laravel-7/blog/public/todo and when I get redirected from home page to create page, the URI is http://localhost/laravel-7/blog/public/todo/create
You are confusing action with native php in laravel.
Replace your action="../todo" with action="{{url('todo')}}"
<form method="POST" action="{{url('todo')}}">
{{csrf_field()}}
<tr><td><input type="text" name="title" value="" placeholder="Title"></td></tr>
<tr><td><input type="text" name="body" value="" placeholder="Body"></td></tr>
<tr><td><input type="submit" name="submit" value="Submit"></td></tr>
</form>
You just need to specify the route exactly no need for saying where the file is actually located so action="/todo" would work fine

laravel 5.5 | old() empty in view unless $request->flash() used

I've run into an odd issue where the helper function old() always returns null in a blade view unless $request->flash() is used prior to loading the view. I have never had to do this when using laravel in the past. Did something change or is there something that I have forgotten to set/configure. Below is a simple example of the behavior:
web.php
Route::get('/test', function(){
return view('testView');
});
Route::post('/test', function(Illuminate\Http\Request $request){
$request->flash(); // if uncommented old() works, if commented old() does not work
return view('testView');
});
form in testView.blade.php
<form action="/test" method="POST">
{{csrf_field()}}
<input type="hidden" name="test001" value="001"/>
<input type="hidden" name="test002" value="002"/>
<div class="">
{{old('test001')}}
<br/>
{{old('test002')}}
</div>
<button type="submit">GO</button>
</form>
after form submitted without $request->flash()
after form submitted with $request->flash()
EDIT
Thinking this might have something to do with using a single route name for both post and get methods, the form was changed so to submit via get, and the issue persists. For example:
web.php
Route::get('/test', function(function(Illuminate\Http\Request $request){
return view('testView');
});
form in testView.blade.php
<form action="/test" method="GET">
<input type="hidden" name="test001" value="001"/>
<input type="hidden" name="test002" value="002"/>
<div class="">
{{old('test001')}}
<br/>
{{old('test002')}}
</div>
<button type="submit">GO</button>
</form>
Use redirect back() instead of loading view directly in a post method.
return redirect()->back()->withInput();
You need to flash request data to put old input into session, otherwise old() will return empty result. See official doc here.

Resources