I post data and redirect to another page with a userfunction and a simple form like the following example:
<?php
class user_test
{
public function getForm() {
$form = '
<form action="#" method="post">
<input type="text" name="test" value="test" />
<input type="submit" name="send" value="send" />
</form>
';
$postArray = t3lib_div::_POST();
if ( isset( $postArray['send'] ) ) {
header( 'Location: index.php?id=2' );
}
return $form;
}
}
this works fine if realURL is deactivated, but if I activate realURL this form leads me always to the homepage after submit :(.
The typoscript part looks as follows:
prefixLocalAnchors = all
simulateStaticDocuments = 0
baseURL = http://test.de
tx_realurl_enable = 1
realURL works for speaking URLs and other forms like powermail forms, but my own forms leads to homepage, hope you can help me :).
UPDATE
If I remove the hash from action attribute: 'action="#"' of this form it is working, is this solution passable?
Removing the action attribute from the form was working.
Related
I am facing problems with yii2 form get method. Here is my form:
<form class="search-form" method="get" action="<?php echo Yii::$app->urlManager->createAbsoluteUrl(['search/index']); ?>" id="search-form">
<div class="row search-box">
<div class="12u search-box-inner">
<input class="search-input" type="text" id="search-query" placeholder="Search" name="search_key" autocomplete="off" >
</div>
</div>
</form>
Here is my SearchController with actionIndex():
public function actionIndex()
{
$request = Yii::$app->request;
$search_key = $request->get('search_key');
return $this->render('index', ['search_key'=>$search_key]);
}
I want to submit to web/index?r=search/index&&search_key='something', but when I submit this form always returns web/index?searchkey='something'.
What need I do?
If you want use a parameter in your SearchController/Index
return $this->render('index', ['search_key'=>$search_key]);
You should declare in function declaration
public function actionIndex($search_key)
in this way you can use the value of $search_key passsed in render call
by your form submit action
<form class="search-form" method="get" action="
<?php echo Yii::$app->urlManager->createAbsoluteUrl(['search/index']); ?>"
id="search-form">
the resulting target is should be
web/index.php?r=search&id=search-form
and for use this get submit in your SearchController/Index
Your actionIndex function should be
public function actionIndex($id)
{
// $id contain the value you assigne in form action
// in you case you should obtain the value 'search-form'
.......
}
First of all, you need to configuring-web-servers correctly.
The url should not include 'web/index'.
Then change your form with ActiveForm
<?php
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(); ?>
Your form content here.
<?php ActiveForm::end(); ?>
OR
Just change url.
<?php echo Url::to(['search/index']); ?>
I wana create a front-end joomla component and this is my first experience.
here is important things:
1-component/controller.php
class TestController extends JControllerLegacy
{
public function display($cachable = false, $urlparams = false)
{
$view= JFactory::getApplication()->input->getCmd('view','items');
JFactory::getApplication()->input->set('view', $view);
parent::display($cachable, $urlparams);
}
}
2: com_test/model/items.php
<?php
defined('_JEXEC') or die();
jimport( 'joomla.application.component.modellist' );
class TestModelItems extends JModelList
{
public function __construct($config = array())
{
if (empty($config['filter_fields']))
$config['filter_fields'] = array('id', 'title', 'catid');
parent::__construct($config);
}
function getListQuery()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select(...)
return $query;
}
}
I can print the query result on default.php on view folder!
but I wana another thing.
I have a form like this in the front page of my site in a custom module:
<form action="" method="post">
<input type="text" name="wordsearch" value="search">
.
.
<input type="submit" />
</form>
Now!
I do not know how can I send this form (with post method) to getListQuery() function in model folder...how can do it?
i wana when sb click submit form, the component filter query sql according to values of form and then show new result to user!
i googled for hourse but no chance to solve. thanks for your help.
You can submit Form from module to component as follows.
Suppose your component name is com_helloworld The in your module form should have the following things.
<form action="" method="post">
<input type="text" name="wordsearch" value="search">
.
.
<input type="hidden" name="option" value="com_helloworld" />
<input type="hidden" name="view" value="yourview" />
<input type="hidden" name="task" value="my_controller_fun" />
<input type="hidden" value="your_controller_file_name" name="controller">
<input type="submit" />
</form>
In this example your controller file should have my_controller_fun method from controller to model you can use regular method. This methods will get all the form data in your controller , then you can pass that to model.
Detailed :
In your controller file.
function my_controller_fun(){
$post_array = $_POST;
$model = $this->getModel('Profile', 'UsersModel');//example for including profile model you can specify your model file name
$model->function_inyourmodel($post_array);//this function should be in model
}
Hope its help..
I just started using CodeIgniter today, and was following the tutorial at the CI website.
When I got to the final step for creating a new news item, I ran into an issue with the form submit. I would expect the URL to be jonhopkins.net/news/create when I press the form's submit button, but it is going to http://jonhopkins.net/news/jonhopkins.net/news/create instead, which is clearly very wrong, and I have no idea why this is happening. Does anyone know how to fix this?
Everything else on my site (the Pages part of the tutorial) works perfectly. This is the only thing that is breaking. All of my code is exactly as it appears in the tutorial, except for a change to what happens when an item is successfully submitted to the database, which currently is never happening.
News Controller
public function create() {
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
} else {
//$success will contain the slug of the created item, or FALSE if database insertion failed
$success = $this->news_model->set_news();
if ($success) {
$this->_doView($success);
} else {
echo "Something went wrong while trying to submit the news item.";
}
}
}
public function view($slug) {
$this->_doView($slug);
}
private function _doView($slug) {
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item'])) {
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
Create View
<h2>Create a news item</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create') ?>
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
And the resulting HTML...
<form action="jonhopkins.net/news/create" method="post" accept-charset="utf-8">
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
News Model I don't think it's ever getting here, but just in case
public function set_news() {
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text'),
'date' => date("Y-m-d H:i:s")
);
if ($this->db->insert('news', $data)) {
return $slug;
} else {
return FALSE;
}
}
Routes
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
The form_open('news/create') method uses the $CI->Config->site_url() method which, in turn, uses the base_url from your config file.
Make sure your $config['base_url'] is set to your domain (with trailing slash) in the application/config/config.php file.
$config['base_url'] = 'http://jonhopkins.net/';
I'm new in Zend Framework, and I'm having a problem.
usually when verifying data in forms we use javascript like bellow:
<html>
<head>
<script>
function verify(){
if( document.form.name.value=="") { alert("Error"); return false}
}
</script>
</head>
<body>
<form onSubmit="verify()" name="form">
<input name="name" />
<input type="submit" />
</form>
</body>
</html>
I'm using zend forms and i don't have a clue how to do it.
<?php
class Application_Form_Login extends Zend_Form {
public function init() {
$login = new Zend_Form_Element_Text('login');
$password = new Zend_Form_Element_Password('password');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel("Login");
$remember = new Zend_Form_Element_Checkbox('remember');
$this->addElements(array($login, $password,$remember ,$submit));
}
}
?>
Any help please.
thanks for any help.
You do it the same way as before, but here is how to tell Zend_Form to add the onsubmit event to your so that the HTML code generated contains the attribute in the <form> tag.
class Application_Form_Login extends Zend_Form {
public function init() {
$this->setAttrib('onsubmit', 'return verify()');
// rest of your form code to add elements
}
}
This adds an attribute onsubmit to the Zend_Form object, such that when the form is rendered your <form> tag has onsubmit="return verify()" in it.
Now you can just put the actual Javascript code to verify the form in the view script itself, or an external javascript file that you can reference in a <script> tag.
Can I use my normal (html) form in Zend Framework ? How can I do that & How can I call action in IndexController file?
of course you can ... just use
<form action="/index/action" methode="POST">
to access post arguments use
$this->getRequest()->getParam('argument')
thats no problem, put your form code inside the view script for the associated action. Maybe:
formAction()
{
// check if post request
if ($this->getRequest()->isPost()) {
// read global $_POST array
$data = $this->getRequest()->getPost();
}
}
the associated view ist than form.phtml
Yes, definitely.. You just have to remove the isValid call in your controller since it won't be performing any validation and also remove the post request check if it will not contain any form. It's like creating a common view with simple links in it.
Yes, I have a module called 'contact', and an action addcontactAction() in the ContactController.php.
So I can use :
/view/scripts/contacts/addcontact.phtml
<form action="" method="post" name="frm_addcontact" />
<input name="cn_fname" type="text" class="textbox" id="cn_fname"/>
<input type="submit" class="button" id="save" value="Save" />
</form>
when this form is submitted, it calls addcontactAction() in the controller.
$cn_fname = $_REQUEST['cn_fname'];
Just to know this is not a good practice to implement, but to solve such problem do the following:
in the view file when you define the form
<form action = "<?php echo $this->url(array('action'=>'ACTIONAME')); ?>" ...>
................
</form>
in the corresponding action name
if($this->_request->isPost()){
foreach ($_POST as $var => $value) {
echo "$var = $value<br>";
}