I have a codeigniter application that is effectively just a one-page form that takes in info and sends an e-mail. I created a method that is intended to handle form submission and send the e-mail called "submit", but for some reason this method appears not to do anything at all.
Here's my controller code:
class Main extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index($page = "home") {
// Loading the form view.
$this->load->view($page);
}
// This is the method that doesn't do anything.
public function submit() {
// I would be processing post data from the form here,
// but right now I'm just trying to load
// a view in this method to see if it's working
$this->load->view("form_submit");
}
My view is using the CodeIgniter form_open function to create the form, and looks as follows:
echo form_open("main/submit/");
This, to my understanding, is how one is supposed to use the form_open function to provide an action to the form.
The form opening tag looks like this when the view is accesssed.
<form action="http://localhost:81/project1/main/submit" method="post" accept-charset="utf-8">
The action and method in the form tag appears to be correctly set, but when clicking on submit on the form the home view just refreshes. Additionally, if I try to directly navigate to "home/submit/" it just loads the home view again. I'm starting to go a little crazy on this and I'm sure I'm missing something really simple.
Related
I've got two buttons on my page, which each have different behaviours when clicked. Currently I've got my routes and controllers set up like this...
routes/web.php
Route::post('/users/button1clicked', 'UsersController#button1clicked');
Route::post('/users/button2clicked', 'UsersController#button2clicked');
app/http/controllers/UsersController.php
class UsersController extends Controller
{
public function button1clicked(Request $request){
//Do something
}
public function button2clicked(Request $request){
//Do something else
}
}
It works ... but I don't think I'm following the correct convention for my controller, because controllers should just have the standard actions (index, create, store, show, edit, update, destroy).
What would be a better way for me to structure this code?
What does each button do? For example, if the button submits a form to 'save' an entity (say a 'Post' in a Blog app) then the form action can direct to the PostController#store. If the button click is intended to show a html form to create an Post, then the it can lead to PostController#show. The table below from the Laravel docs will help you.
Route definitions conventions
Please also see https://laravel.com/docs/7.x/controllers#restful-naming-resource-route-parameters.
If you are using ajax or axios to make async calls then you can call a function using button events (on-click for example) and that function can make a post (or any other async call to the relevant controller method ( PostController#store). Please let me know if you'd like an example.
I am working on a web application with a form submit function. People fill in their info and submit the form. But I have the problem that people can resubmit the form when filled in with F5(Page refresh). Now i have used header() before to fix this.
I don't really like header(), because some users need to submit the form with different data... And header() does not work properly sometimes for me.
I am programming in PHP with the symfony framework, Maybe the framework has some kind of function for this, I can not really find out.
I hope people know good working alternative ways of header(). To achieve this.
You might do something like this with use of redirectToRoute() method from your Controller:
class DefaultController extends Controller
{
public function someAction(Request $request): Response
{
# create form and handle request
$form = $this->createForm(YourFormType::class, $data);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
/* some logic here */
# generate flash message
$this->addFlash('success', 'Your flash message.');
# refresh page or redirect to other page
return $this->redirectToRoute('your_route_name');
}
/* some other logic */
}
}
More examples on Symfony Docs.
You can redirect User to the same page using "RedirectResponse" after submitting data, then if User click F5 it will not send another POST Request. You can also use Ajax call for submitting data.
For example I have edit profile page which have a form for editing the summary. the file's name is Index.tpl.
In form I have a text field, I have added the saveSummary() in the controller i.e. controller.php. How can I invoke the given function on clicking on submit button of form.
Answering directly to your question, you should submit the form to /save-summary/ action if it's called saveSummary() in your controller. Of course don't forget to include prefix of the route.
Generally your approach is not correct because you're trying to use different actions for displaying content and processing the form – you can easily do both operations in one action. Check for isPost() and getPost() in other controllers – this methods are used to divide parts of action responsible for simply getting and displaying content and for processing form data.
You can access controller functions by using action handle at the end of function name ex -
class Mymodule_MytestController extends Core_Controller_Action_Standard{
public function saveSummaryAction(){
.......
}
}
I have a CakePHP Action that is not rendering it's view, however the action is being executed.
I have the controller VoteTagsController with the action alltags()
App::uses('AppController', 'Controller');
class VoteTagsController extends AppController {
public function index(){
$this->set('allVoteTags', $this->VoteTag->find('all'));
}
public function alltags(){
echo "Running";
echo "Test";
}
}
The view exsists also in /View/VoteTags/alltags.ctp
I am attempting to load the action from http://test_site.dev/votetags/alltags
I've tried everything but cannot get this view to render I just get a white page, no errors.
Changing the name of the controller however and refreshing sends me to an Error Not Found page which indicates the action is being fired but not rendered.
Any ideas?
Based on conventions your URL should be test_site.dev/vote_tags/alltags
If you wish to change this you can add to your app/Config/routes.php file the following line
Router::connect('/votetags/alltags', array('controller' => 'vote_tags'))
Or whatever other url template you might like.
For more information read http://book.cakephp.org/2.0/en/development/routing.html
I'm developing a small app. using php and Zend Framework. I have a form, and I use it's controller to view the form. Now I need to manipulate the form data and redirect the user accordingly.
here is my controller
<?php
class NewuserController extends Zend_Controller_Action {
public function init()
{
/* Initialize action controller here */
}
public function newuserAction()
{
$this->view->newuser;
}
public function adduserAction(){
$data = $this->getRequest()->getParams();
$u = new User();
$u->setUserName($data['uName']);
$u->setPrepwd($data['pwd']);
$u->setPrepwd($data['pwd']);
if($u->isEqual()){
$val = $u->addUsers();
if($val)
$this->_helper->redirector('main','main');
}
else
$this->_helper->redirector('newuser','newuser');
}
}
?>
my view is newuser.phtml. In the action attributr of the <form> I have specified Newuser/adduser . But when I submit the form it again displays the newuser.phtml.
Why is this?
Thanks in advance
Charu
You can forward the request instead of redirecting:
$this->_forward('newuser');
I would suggest that you use the redirector helper instead of the foward because if you want the user to be redirected to the main page with foward it will redirect but it won't change the url so if they were to bookmark the page for example it wont work. If you use the redirector it will redirect and change the url to show just your main phtml page. Also if you are in the same controller its not necessary to include the second param which is the controller. Check this out for more help
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.redirector