I am using form helper library to load form in ci views. the script i am using is
echo form_open('');
But when i am inspeting the form or having a look at page source it has set action attribute with base url. I want to set action attribute always blank when using form_open('') method. How can i get this behavior of form_open() method. Usually we set action like this when using ci standard
echo form_open('abc/login');
so is there a way to keep action attribute blank.
First of all load a view in your view page of form open
View.php
// Open Form
<?php $this->load->view('your_directory_path/form_open'); ?>
Secondly add form open code in seperate file so it can be attached anywhere in view files.
form_open.php
<form method="post" id="XYZ_DEMO" name="XYZ_DEMO" enctype="multipart/form-data" class="ABC XYZ">
like this you can set action attribute blank using form_open in codeigniter.
First of load form helper in order to use form_open() method.
$this->load->helper('form');
Or load helper in application/config/autoload.php.
Then set blank form action attribute in view. like this..
<?php echo form_open('', array('id' =>'form_id', 'class'=>'form_class'); ?>
Not sure what you are trying to achieve by wanting the action attribute blank.
If you look into CI's form_open function inside the form_helper:
function form_open($action = '', $attributes = '', $hidden = array())
{
$CI =& get_instance();
if ($attributes == '')
{
$attributes = 'method="post"';
}
// If an action is not a full URL then turn it into one
if ($action && strpos($action, '://') === FALSE)
{
$action = $CI->config->site_url($action);
}
// If no action is provided then set to the current url
$action OR $action = $CI->config->site_url($CI->uri->uri_string());
$form = '<form action="'.$action.'"';
$form .= _attributes_to_string($attributes, TRUE);
$form .= '>';
/* MORE CODE */
}
you can see that if the $action parameter is not set, it will be set to the current url.
// If no action is provided then set to the current url
$action OR $action = $CI->config->site_url($CI->uri->uri_string());
that could be the reason why when you pass empty parameter to form_open('') the action attribute still has value.
wouldn't it hurt if you just use html's way?
<form action="" method="post" id="form_upload_submit">
if you really really know what you want to do, go ahead and edit (which I don't really advise doing so) the form_open function inside the form_helper.php located in system\helpers\form_helper.php
Blank action points to current page so that you can use
echo form_open(base_url());
Related
I have created a ModalDialog in Drupal contain two forms like as follows,
public function signUpForm() {
$response = new AjaxResponse();
// Get the modal form using the form builder.
$modal_form[] = $this->formBuilder->getForm('Drupal\signup_form\Form\SignupForm');
$modal_form[] = $this->formBuilder->getForm('Drupal\signup_form\Form\SigninForm');
// Add an AJAX command to open a modal dialog with the form as the content.
$response->addCommand(new OpenModalDialogCommand('', $modal_form, ['width' => '800','dialogClass' => 'signup-modal']));
return $response;
}
The forms are rendered, But the problem is ,both forms are submitting to the same function. I am unable to change the action of the form.
Is there any options available to change the form action to a custom url?
You need to alter these forms so that you can add your own submit handlers (and remove the unwanted one), and eventually from there you will be able to set the appropriate redirect, or AjaxResponse.
For example, for each form, implement hook_form_alter from a custom module :
function MODULE_signup_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['actions']['submit']['#attributes']['class'][] = 'use-ajax-submit';
$form['actions']['submit']['#submit'][] = 'MODULE_signup_form_ajax_submit';
}
Then add the appropriate submit handlers, eg. :
function MODULE_signup_form_ajax_submit(array $form, FormStateInterface &$form_state) {
$response = new AjaxResponse();
$form-class = '.signup-form'; # using your own selector
$selector = '#drupal-modal' . ' ' . $form-class;
$message = '<div>submitted</div>';
$response->addCommand(new ReplaceCommand($selector, $message));
$form_state->setResponse($response);
}
I assumed you want to update the modal content so that the second form can still be submitted, so here I added the 'use-ajax-submit' class (cf. Drupal.behaviors.AJAX) to the appropriate $form action attributes so that the submit response can be rendered in the modal as well. Also used the ReplaceCommand to replace the submitted form content with a simple message.
If instead the first submitted form should trigger a redirection, then don't use setResponse() (that actually cancels redirect), and use setRedirect() if the base action url is not the one that you want.
I have a template design for each page and it is working without any issues, now i have a problem with form validation and template.
my_controller //inside my controller
function __construct()
{
parent::__construct();
$this->load->library('template');
}
public function page1(){
$this->template->write('title', 'COMPANY');
$this->template->write_view('content', 'company');
$this->template->render();
}
public function validation(){
//for page1 form validation
//form validation here
if ($this->form_validation->run() == FALSE){
// here i need to call my page1 again to update the form error
// option1: $this->load->view('page1'); // this is just loading page1, not loading with my template, template file has header, footer, js and css file includes
// option2: $this->template->load('template', 'page1'); // it is loading page withing page
// option3: $this->template->write_view('content', 'page1');
// $this->template->render(); // this is same as option2
} else {
$this->load->view('formsuccess');
}
}
So my question is how do i can call same view (within one controller) for multiple time when it is already loaded with template?
EDITED for more clarity
My template file is just single file ( not separated like header, footer) will have all header information and footer information with common style sheet and script files.
I am having
<?php $content ?>
variable name inside the template to pass my content from controller.
for example see my page1 function(),
$this->template->write('title', 'COMPANY'); // to write a value on variable
$this->template->write_view('content', 'page1'); //view page to write on template
$this->template->render(); // final template render
This page is having form validation, if my form validation is FALSE, how do i can reload the template page with validation error?
I have tried so many way, please check my option1, 2, 3, i never got a solution to solve this, now please tell me how do i can solve this?
If I understand correctly, you want to display the validation errors?
Inside your View file, put the following:
Short IF-Statement Form:
<?php if ( validation_errors() ) :?>
<div>
<?php echo $validation_errors();?>
</div>
<?php endif;?>
Regular If-Statement Form:
<?php if ( validation_errors() ) {?>
<div>
<?php echo $validation_errors();?>
</div>
<?php }?>
Validation Errors Doc
I have created a custom component with form to update prices of four product to be displayed on frontend.
My main controller code is here:
public function display($cachable = false, $urlparams = false) {
require_once JPATH_COMPONENT.'/helpers/calculator.php';
$view = JFactory::getApplication()->input->getCmd('view', 'pricetable');
$layout = JFactory::getApplication()->input->getCmd('layout', 'edit');
JFactory::getApplication()->input->set( 'layout', $layout );
JFactory::getApplication()->input->set('view', $view);
JFactory::getApplication()->input->set('id', 1);
parent::display($cachable, $urlparams);
return $this;
}
id is set to 1 so it loads only first row from database.
code for pricetable container is:
function __construct() {
$this->view_list = 'pricetable';
parent::__construct();
}
Now in admin backend the form is loaded as desired with the first row of data.
When I try to save the form it is redirected to administrator/index.php?option=com_calculator&view=pricetable and error is:
Error: You are not permitted to use that link to directly access that
page (#1).
my form action is:
<?php echo JRoute::_('index.php?option=com_calculator&task=pricetable.edit&id='.(int) $this->item->id); ?>
Please suggest where I am doing wrong. It is third day I'm scratching my head. :(
You can do updating actions (or calling them) inside your code whenever it is.
New instance or update new - just add one more if in code and hidden input on form. For example:
<input type="hidden" name="task" value="update" />
This has to do with routing. So for getting parameters via url, you basically pass the data to the url following the route format you set.
This is working with links. I created the route, passed the data into the url, and used the request method to get the parameter for use in the controller. like URL::site("site/$color/$size")
What if I am constructing the url by form submission? For example, if I want to create a basic search query.
How do I get my form submission to look like this search/orange/large and not like this search.php?color=orange&size=large when I submit a form via get method.
By definition, the GET method puts the submitted information as URL parameters. If you specifically want to end up with a URL like site/$color/$size, you can use the POST-REDIRECT-GET pattern.
A partial example, from a controller on one of my sites (there is a submit button on the page named clear_cache_button):
public function action_index()
{
$session = Session::instance();
$is_post = (Request::current()->post('submit_button') !== NULL);
$is_clear_cache = (Request::current()->post('clear_cache_button') !== NULL);
$p = Database::instance()->table_prefix();
$people = DB::query(Database::SELECT, "
SELECT *
FROM `".$p."Tabe`;
")->cached(600, $is_clear_cache)->execute()->as_array('RegID');
if ($is_clear_cache)
{
HTTP::redirect(Request::current()->uri());
}
...
...
...
}
You can use Route filters (v3.3) or callbacks (3.1, 3.2) and set route params manually.
You can do it this way...
public function action_index()
{
// this will only be executed if you submmitted a form in your page
if(Arr::get($_POST,'search')){
$errors = '';
$data = Arr::extract($_POST,array('color','size'));
// you can now access data through the $data array:
// $data['color'], $data['size']
// perform validations here
if($data['color']=='') $error = 'Color is required';
elseif($data['size']=='') $error = 'Size is required';
if($error==''){
$this->request->redirect('search/'.$data['color'].'/'.$data['size']);
}
}
// load your search page view here
echo 'this is the search page';
}
Hope this helps you out.
I am trying to get a HTML/PHP form to submit properly. Some details:
base url = http://localhost/directory
page = page/add
complete address = http://localhost/directory/page/add
Using htaccess to rewrite urls so http://localhost/directory/page/add is actually http://localhost/directory/index.php?q=page/add
My HTML POST action is "page/add" so that the front controller knows which function to fire to sanitize and submit the data (it acts as a 'form id').
The page loads fine at http://localhost/directory/page/add but when I click on the submit button, the URL gets mangled to page/page/add. And every time I press "submit" I get another "page" added to the url. So 5 clicks will get "page/page/page/page/page/page/add"
I can't seem to find why I am getting that "extra" "page".
The actual PHP error (page/page/add doesn't exist in $routes since it isn't a valid route):
Notice: Undefined index: page/page/add in C:\xampp\htdocs\script\includes\common.inc on line 92
Here is the function at line 92:
function route_path($path = NULL) {
$routes = get_routes(); //Returns array: approved "urls => function callbacks"
if($path === NULL) {
$path = get_path(); //Returns $_GET['q'] with trim and strip_tags
}
$function = $routes[$path]; <<<<<----This is LINE 92
if(isset($function)) {
$form_name = str_replace('/', '_', $path); // page/add = function page_add()
}
if(function_exists($function)) {
call_user_func($function, $form_name);
}
else {
//TODO: Redirect to Login screen.
}
}
The basic HTML is:
<form action="page/add" method="post" />
//Form elements
<input type="submit" value="Submit" />
</form>
Thanks for the help.
UPDATE: What I did was add the <base> tag to my HTML templates. This allows me to keep the action as page/add (since it is also a route in my simple router/dispatcher).
By using a relative path, you're telling the form to submit at the existing path plus your action. So if you are at http://example.com/page/add, the form uses http://example.com/page/ as a base and adds the action page/add resulting in a POST to http://example.com/page/page/add.
You can still use a relative path, just change the action accordingly:
<form action="add" method="post" />