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" />
Related
I have this input element in index.php
<input type="submit" name="OUs" value="Get all OU's">
This is where I fire the function in a PHP codeblock in index.php:
require_once('./OU.php');
$ou = new \Google\OU\OU();
if(isset($_POST['OUs'])) {
echo $ou->getOUs($response['access_token']);
}
I try to run a function in ou.php
namespace Google\OU;
define("APIURL_DIRECTORY","https://www.googleapis.com/admin/directory/v1/customer/"); // For Google
Directory actions
class OU
{
// Get OU's
function getOUs($token){
$url = "https://www.googleapis.com/admin/directory/v1/customer/my_customer/orgunits?orgUnitPath=/&type=all";
$method = "GET";
echo exeCurl($url,$method);
}
What is the reason that nothing happens when I click on that submit button? It should execute the Curl Call, shouldnt it?
I cant really figure it out. Thanks in advance.
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());
I have a html form that has this markup.
<form id="login-form" action="/post/login">
<input name="username" type="text">
<input name="password" type="password">
</form>
I want to be able to assert this form action.
I try with this inside the test method, note I extended \PHPUnit_Extensions_Selenium2TestCase
$form = $this->byId('login-form');
$this->assertEqual('/post/login', $form->attribute('action'));
It seems like action always null.
Does anyone know how to test the form action attribute?
Thank you.
Unfortunately, $form->attribute('action') returns action with base url (http://localhost/post/login).
I did not find a way to get action without base and did not find how to get base url. There is my solution:
function testForm(){
$this->url('/test.html');
$form = $this->byId('login-form');
$this->assertEquals('/post/login', $this->getRelativeFormAction($form));
}
function getRelativeFormAction($form){
$action = $form->attribute('action');
$action = str_replace($this->getBaseUrl(), '', $action);
return $action;
}
function getBaseUrl(){
$urlComponents = parse_url($this->url());
$url = "{$urlComponents['scheme']}://{$urlComponents['host']}";
return $url;
}
There is successful full test code.
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" />
I have made a simple plugin with a form but it won't post when I'm not logged in.
Here is the file class.tx_gctest_pi1.php, created with Kickstarter.
require_once(PATH_tslib.'class.tslib_pibase.php');
class tx_gctest_pi1 extends tslib_pibase {
var $prefixId = 'tx_gctest_pi1'; // Same as class name
var $scriptRelPath = 'pi1/class.tx_gctest_pi1.php'; // Path to this script relative to the extension dir.
var $extKey = 'gc_test'; // The extension key.
var $pi_checkCHash = true;
function main($content, $conf) {
$this->conf = $conf;
$this->pi_setPiVarDefaults();
$this->pi_loadLL();
if($_POST) {
echo 'test';
}
$content='
<strong>This is a few paragraphs:</strong><br />
<p>This is line 1</p>
<p>This is line 2</p>
<h3>This is a form:</h3>
<form action="'.$this->pi_getPageLink($GLOBALS['TSFE']->id).'" method="POST">
<input type="text" name="'.$this->prefixId.'[input_field]" value="'.htmlspecialchars($this->piVars['input_field']).'">
<input type="submit" name="'.$this->prefixId.'[submit_button]" value="'.htmlspecialchars($this->pi_getLL('submit_button_label')).'">
</form>
<br />
<p>You can click here to '.$this->pi_linkToPage('get to this page again',$GLOBALS['TSFE']->id).'</p>
';
return $this->pi_wrapInBaseClass($content);
}
}
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/gc_test/pi1/class.tx_gctest_pi1.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/gc_test/pi1/class.tx_gctest_pi1.php']);
}
?>
This will output test when logged in and nothing when not logged in.
The page is reloaded but no post is sent
I think this has not much to do with logged in / logged out. TYPO3 caches content unless you tell it to not cache.
echo var_dump print_r debug are methods that directly display things through php. TYPO3 doesn't catch them. If you want to have something displayed, add it to e.g. $content and return $content at the end of main(). The return value of main() gets cached.
You can try this by clearing your cache in backend and refresh the page. echo etc is displayed. after a new reload, it is gone.
So, how to solve this? There are a few possibilities
make the page that contains your plugin uncached
make the plugin itself uncached
I would suggest you find out what you really want to do and then write your code that is is using caching.