I have a componenet in joomla 2.5.
I have several view, in one of then I have a combobox, when I click on it, I want to call a function for that I have this
<form class="product_filter" action="<?php echo JURI::root()?>index.php/com_productos/buscarCategoria" method="POST">
<input type="hidden" class="type" name="type" value="HEALTH_FOOD"/>
<div class="select_wrapper small first">
<?php echo JHTML::_('select.genericlist', $nameCombo,'name','onChange="this.form.submit()"','value','text'); ?>
</div>
the name of my component is com_productos so in the producto.php I have this
class ProductosController extends JController
{
function buscarCategoria(){
$jinput = JFactory::getApplication()->input;
$view = $jinput->getCmd('view', 'productos');
JFactory::getApplication()->input->set('view', $view);
$model = &$this->getModel($view);
$view = &$this->getView($view, 'html');
$view->setModel($model, true);
$view->categoria();
}
but never execute this function.
Any idea
You should pass task (and controller as part of it) in the hidden field like this:
<input type="hidden" name="task" value="productos.buscarCategoria"/>
And your action can be simply index.php.
Finally I solved the problem
<input type="hidden" name="controller" value="field" />
<input type="hidden" name="option" value="com_productos" />
<input type="hidden" name="task" value="buscarCategoria" />
</form>
Related
blade file :
<form action="{{URL::to('/admin/sender')}}"method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
Controller:
public function notificationSender(Request $request)
{
$text= request()->text;
print_r($request->input());
event(new OrderComplete($text));
return view('admin.sender');
}
Route:
Route::post('/sender','HomeController#notificationSender');
the route is a subroute for a group..is there something i'm missing?
Give space between method and route parameters here
if your laravel version is 5 then use this
<form action="{{URL::to('/admin/sender')}}" method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
Or else you can also name the route to make it easy to pass in action parameter of form like this.
Route::post('/sender','HomeController#notificationSender')->name('sender');
Then you can pass it in form like this
<form action="{{route('sender')}}" method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
For GET Method (Laravel 6)
blade file
<form method="GET" action="{{ route('sender') }}" enctype="multipart/form-data" >
#csrf
<input type="text" name="text">
<input type="submit">
</form>
Write Controller
public function notificationSender(Request $request)
{
$text= $request->get('text');
echo "<pre>"; print_r($text);
event(new OrderComplete($text));
return view('admin.sender');
}
Route
Route::get('sender','HomeController#notificationSender');
This question already has answers here:
Laravel form post to controller
(2 answers)
Closed 3 years ago.
I'm trying to set a route to my "MessagesController" so that a i can access it from my form, How do i set a route to the controller and access it from action form ?
This is my Form code
<form action="#" id="ajax-contact" method="GET" enctype="multipart/form-data" >
{{csrf_field()}}
<div class="input-field">
<input type="text" class="form-control" name="name" id="nom" required
placeholder="Nom">
</div>
<div class="input-field">
<input type="email" class="form-control" name="email" id="email" required
placeholder="E-mail">
</div>
<div class="input-field">
<textarea class="form-control" name="message" id="message" required
placeholder="Message"></textarea>
</div>
<button class="btn" type="submit">Soumettre</button>
</form>
What should i write inside my web.php to set a route so i can acces my MessageController.php what should i write inside the action attribute ?
Okay not sure what you want but here is the whole thing
REQUEST(FORM) => ROUTE => CONTROLLER => RESPONSE
So you want to go from FORM to Controller you have to :
Create a controller (in App\Http\Controllers) and a method
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class MyController extends Controller
{
// This method will handle the form
// $request is where the form data will be
public function handleForm(Request $request){
// this means dump all form values
dd($request->all());
}
}
Create a route ( In simple words, this will link between FORM & Controller method ) in web.php
// The request need to be POST to /handle-data
Route::post('handle-data','MyController#handleForm');
finally this should be your form
<form action="/handle-data" id="ajax-contact" method="POST" enctype="multipart/form-data" >
{{csrf_field()}}
<div class="input-field">
<input type="text" class="form-control" name="name" id="nom" required
placeholder="Nom">
</div>
<div class="input-field">
<input type="email" class="form-control" name="email" id="email" required
placeholder="E-mail">
</div>
<div class="input-field">
<textarea class="form-control" name="message" id="message" required
placeholder="Message"></textarea>
</div>
<button class="btn" type="submit">Soumettre</button>
</form>
If you want to make this as an ajax call
$( "#ajax-contact" ).submit(function( event ) {
event.preventDefault();
var data = $(this).serializeArray();
$.post('/handle-data', data ).then(function(response){
console.log(response);
});
});
if you want to test the ajax way, change the dd($request->all()); to return response()->json($request->all());
That should be it :)
Edit routes inside web.php to point to submit method in MessagesController:
Route::post('submit', 'MessagesController#submit')->name('submit');
then set form action to:
<form action="{{ route('submit') }}" id="ajax-contact" method="POST" enctype="multipart/form-data">
You should POST from a form.
I am trying to get to know CodeIgniter. I just couldn’t figure out what the problem with the code below is. However, when I click on the ‘submit’ button, nothing happens. I mean no record is inserted in the database table.
The php in the view is as follows.
<?php echo form_open('site/create'); ?>
<?php echo form_close();?>
<p>
<label for="title">Title</label>
<input type="text" name="title" id="title" />
</p>
<p>
<label for="content">Content</label>
<input type="text" name="content" id="content" />
</p>
<p>
<input type="submit" value="submit" />
</p>
Down goes the controller
<?php
class site extends CI_Controller
{
function index()
{
$this->load->view('options_view');
}
function create()
{
$rec=array(
'title' => $this->input->post('title'),
'content'=>$this->input->post('content')
);
$this->site_model->add_record($rec);
$this->index();
}
}
?>
And the model is as follows.
<?php
class site_model extends CI_Model
{
function get_records()
{
$query=$this->db->get('other');
return $query->result();
}
function add_record($rec)
{
$this->db->insert('other',$rec);
return;
}
function update_record($rec)
{
$this->db->where('id',2);
$this->db->update('other',$rec);
}
function delete_rec()
{
$this->db-delete();
}
}
?>
The form close has to be at the end:
<?php echo form_open('site/create'); ?>
<p>
<label for="title">Title</label>
<input type="text" name="title" id="title" />
</p>
<p>
<label for="content">Content</label>
<input type="text" name="content" id="content" />
</p>
<p>
<input type="submit" value="submit" />
</p>
<?php echo form_close();?>
It's better and easier to use the form library from codeigniter.
I believe you must be familiar with Html already to start using codeIgniter so this should be easy... Like all html forms you need to have an opening tag and a closing tag, what you have done is open and close the form at the top before the other elements. So as expected, the submit button wont do what you need it to do. The solution is to move
<?php echo form_close();?>
to the bottom so your code appears as such
<?php echo form_open('site/create'); ?>
<p>
<label for="title">Title</label>
<input type="text" name="title" id="title" />
</p>
<p>
<label for="content">Content</label>
<input type="text" name="content" id="content" />
</p>
<p>
<input type="submit" value="submit" />
</p>
<?php echo form_close();?>
All the best.
I am using codeigniter. Below is the code for my registration page.
Controller
function register()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|is_unique[users.username]');
$this->form_validation->set_rules('user_email', 'Email', 'trim|required|valid_email|is_unique[users.user_email]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('ans_1', 'Security answer 1', 'required');
$this->form_validation->set_rules('ans_2', 'Security answer 2', 'required');
if($this->form_validation->run() == FALSE) {
//not validated - reload the view and display errors
$this->load->database();
$this->load->model('user');
$data['ques'] = $this->user->get_que();
$this->load->view('register',$data);
}
else {
$this->load->database();
$this->load->model('user');
//create user
$this->user->create_user();
$this->thank();
}
}
View
<!DOCTYPE html>
<html>
<head>
<title> Login</title>
<link rel="stylesheet" href="http://localhost/cinema/assets/css/form.css">
</head>
<body>
<form action="http://localhost/cinema/login/register" method="post" accept-charset="utf-8" class="username register" >
<h3>User Registration form:</h3>
<p><label for="first_name">First Name</label>
<input type="text" name="first_name" /></p>
<p><label for="last_name">Last Name</label>
<input type="text" name="last_name" /></p>
<p><label for="username">Username</label>
<input type="text" name="username" /></p>
<p><label for="user_email">Email</label>
<input type="text" name="user_email" /></p>
<p><label for="password">Password</label>
<input type="password" name="password" /></p>
<br><font size='4'>Select security question 1:</font>
<select name="que_1">
<?php
foreach($ques as $que)
{
echo '<option value="'.$que->que_ID.'">'.$que->que.'</option>';
}
?>
</select><br><br>
<p><label for="ans_1">Answer:</label>
<input type="text" name="ans_1" /></p>
<br><font size='4'>Select security question 2:</font>
<select name="que_2">
<?php
foreach($ques as $que)
{
echo '<option value="'.$que->que_ID.'">'.$que->que.'</option>';
}
?>
</select><br><br>
<p><label for="ans_2">Answer:</label>
<input type="text" name="ans_2" /></p>
<input type="submit" name="btnSubmit" class="styled-button-8" style="float:right; margin-right:300px; margin-top:-10px;" value="Sign Up!"
/>
<font color="red" size="4"><b>
<?php echo validation_errors(); ?></b></font>
</form>
</body>
</html>
<br>
My problem is that in case the user fills invalid data and clicks the signup button, the page is refreshed and the validation errors are displayed but the data entered by the user doesn't remain there.He has to again fill the whole form. I want the user data to be retained and displayed in this case. Thanks in advance .. :)
For this you need set_value()
For input fileds
<input type="text" name="last_name" value='<?php echo set_value('last_name')?>'/>
This will get the value from $_POST and print after failed validation. Also takes 2nd parameter as default value.
<?php echo set_value('last_name','John')?>
Read the Form Helper Class.
You can try like this. You can maintain session to display user fields data while validation check. I added a Sample with session in your form.
Your View :
<?php session_start(); ?>
<form action="http://localhost/cinema/login/register" method="post" accept-charset="utf-8" class="username register" >
<h3>User Registration form:</h3>
<p><label for="first_name">First Name</label>
<input type="text" name="first_name" value="<?php echo $_SESSION['first_name']; ?>" /></p>
<font color="red" size="4"><b>
<?php echo validation_errors(); ?></b></font>
</form>
Your controller:
function register() {
$this->load->library('form_validation');
$firstName = $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
$this->session->set_userdata('first_name', $firstName);
if ($this->form_validation->run() == FALSE) {
//redirect to register page
} else {
//register user details
$this->session->unset_userdata('first_name');
}
Better solution is : use post data
<input type="text" name="last_name" value='<?php echo $this->input->post('last_name')?>'/>
When you use set_value(), you must send that field to validation even it is not required.
But for Post, you don't want to do any additional work.
In your controller put a data array and pass the user entered values to that.Then if there are form validation errors pass those data to the view.
In the view use CI set_value() method to show the previous entered values.
I have the problem of doing the validation of fields myself. Now there are 5-6 fields in the form. So I am checking each one in my controller, and if there is wrong i wish to load the view again and pass the error array to it.
I achieved the above functionality with this:
<html>
<head>
<title>My Form</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
</head>
<body>
<?php
echo $fullname;
?>
<?
echo form_open('/membership/register');
?>
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<h5>Password Confirm</h5>
<input type="text" name="cpassword" value="" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />
<h5>Mobile</h5>
<input type="text" name="mobile" value="" size="15" />
<h5>Home</h5>
<input type="text" name="home" value="" size="15" />
<h5>Full Name</h5>
<input type="text" name="fullname" value="" size="100" />
<br><br>
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
and in controller the code is:
if (preg_match('#[0-9]#',$fullname))
{
$errors['fullname'] = 'wrong name format!';
$this->load->view('register', $errors);
}
Now the real problem I have is if the many fields are wrong. I want to have $errors array passed to view and accessed there for all the values it contains. so I don't have to specify $fullname or $mobile to get the value. How can this be done? as to show the user everything missing in one go
First of all I advise using codeigniter's built in form validation class
Here is how I usually process my validation in the controller:
if ($this->input->post())
{
// process the POST data and store accordingly
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|xss_clean');
// the rest of your form fields validation can be set here
if ($this->form_validation->run() == FALSE)
{
// validation hasnt run or has errors, here just call your view
$this->load->view('same_view_file_with_the_form', $data);
}
else
{
// process the POST data upon correct validation
}
}
In my view file I call each error like this:
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<span class="error-red"><?php echo form_error("username"); ?></span>
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<span class="error-red"><?php echo form_error("password"); ?></span>
<h5>Password Confirm</h5>
<input type="text" name="cpassword" value="" size="50" />
<span class="error-red"><?php echo form_error("cpassword"); ?></span>
Do all your checks in the controller prior to binding errors into the view.
e.g.
$errors = array();
if (preg_match('#[0-9]#',$fullname))
{
$errors['fullname'] = 'wrong name format!';
}
if ( do_something_to_validate(mobile) )
{
$errors['mobile'] = 'invalid mobile';
}
// after checking everything do this
$this->load->view('register', $errors);