We (the entire team) have been pulling hair over this problem for the last 2 days. For some strange reason our Zend Framework 1.11.2 will not let us post plain form into the controller unless we create a Zend_Form class.
HTML in view file (no javascript, nothing):
<html><body>
<form action="/index/login/" method="post">
Email: <input type="text" name="email"/><br />
Password: <input type="password" name="password" />
<p><input type=submit name="ac" class="btn btn-success" value="Login"></p>
</form></body></html>
Index Controller:
public function loginAction()
{
$request = $this->getRequest();
if ($request->getParam('email')) {
Zend_Debug::dump($request);
}
}
$request->getParams() is empty!
But if we create a Zend_Form or pass the fields in as GET then $request->getParams() is filled with data.
I just don't get it. Is there something in Zend that you have to turn off to use plain form? We think we've tried everything, accessing global $_POST and $_REQUEST variables, and calling $request->getPost(). All empty unless we create a Zend_Form class and instantiate it inside the controller.
Looks like an problem with your Form Action. Please use "baseUrl" View Helper or URL View Helper to create correct action url:
<?php
// correct action url
$actionURL = $this->url(array(
'controller' => 'index',
'action' => 'login',
'module' => 'default',
));
?>
<html>
<body>
<form action="<?php echo $actionURL; ?>" method="post">
Email: <input type="text" name="email"/><br />
Password: <input type="password" name="password" />
<p><input type=submit name="ac" class="btn btn-success" value="Login"></p>
</form>
</body>
...pass the fields in as GET then $request->getParams() is filled with data.
Since you are crafting a form post, you will need to use the getPost() method.
public function loginAction()
{
if ($email = $this->_request->getPost('email')) {
Zend_Debug::dump($email);
}
}
First use the latest version
Zend Framework 1.11.11
Then try this code inside loginAction
if($this->getRequest()->isPost())
{
print_r($this->_getAllParams());
}
Related
I searched in internet for information about my problem but I found nothing. I have to send a form to php file. The problem is, I use a framework called zend and I don't understand how to send my variable to an action function from my controller. What can I put in my form action the name of my controller or the name of my function?
My form :
<form name=="downloadFile" id="downloadFile" method="POST" action=="ajaxdownloadfile()">
<input type="hidden" name="id" value="<?php $elem['evt_id']?>" />
<input type="hidden" name="nomfic" value="<?php $elem['evt_nomfic']?>" />
<input type="hidden" name="statut" value="<?php $elem['evt_statut']?>" />
<img class='js-img-download' type="submit" src='/img/download.png'>
</form>
My function action in my controller :
public function ajaxdownloadfileAction() {
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$this->view->lib = $this->_labelsFile;
$connection = ssh2_connect($this->_configFile->ftp->hostname, $this->_configFile->ftp->port);
if ($connection) {
$login = ssh2_auth_password($connection, $this->_configFile->ftp->login, $this->_configFile->ftp->password);
if ($login) {
$content = true;
if ($content) {
$id = $this->_getParam('id');
var_dump($id);
$nomfic = $this->_getParam('nomfic');
var_dump($nomfic);
$statut = $this->_getParam('statut');
echo $statut;
In ZF1 ( it is not specified but this code comes from a ZF 1.x application ) there is a standard routing where a controller/action match the www.mydomain.com/{controller}/{action}, for example www.mydomain.com/index/index.
So edit you form action
action="/yourcontroller/ajaxdownloadfile"
No double "=", no Round brackets.
IF you want to use your controller/action with ajax, just remove the 'action' attribute and create a js function that listen to the submit form event.
There are a lots of tutorial on the web
Imagine I have the following in my routes.php file:
Route::get('/test/{system}/{link}', function($system,$link)
{
return "test ok";
});
Then, I wanna access this route with a form from my view:
<form action="/" method="get">
<input type="text" name="system">
<input type="text" name="link">
<button type="submit">Go!</button>
<form>
What would be the best way to do that? I know I can do something like:
Route::get('/',function(){
$input = Request::only('link','system');
$url = 'test/'.$input['system'].'/'.$input['link'];
return redirect($url);
});
But I'm not sure if it's the correct way to achieve it.
Thanks in advance!
You could have js change the form action and then submit it. Your way is ok, but make sure to put it at the end of the routes file. I would actually have the form submit to some specific route (instead of / maybe /redirect-to or something) and from there redirect.
You can do this. I'm not sure if this is the best process.
In routes.php, do this:
Route::get('/form', function(){
return View::make('formview');
});
Route::get('/formaction', function(){
$link = Request::input('link');
$system = Request::input('system');
return "$link";
});
And in the view, as usual, the following:
<form method="get" action="formaction">
System: <input type="text" name="system" />
Link: <input type="text" name="link" />
<input type="submit" value="ok" />
</form>
The Request of Laravel does not depend on the verb used for the request.
I haven't used CodeIgniter in nearly a year and I seem to have forgotten a lot of the fundamentals.
I am trying to retrieve the post variables from a form and pass them into a model which inserts them into mysql.
The controller function my form submits to looks like this:
public function validation() {
$this->load->helper("form");
$this->load->model("contact_form");
$data = array(
"name" => $this->input->post("name"),
... etc. etc. ....
);
if ($this->contact_form->new_form($data)) {
$this->load->view("header");
$this->load->view("submitted");
} else echo "Sorry, there was a problem adding the form to the database.";
}
The form in the view is structured like so:
<? echo form_open("form/validation");?>
<div id="one" style="display: block;">
<h1>A Heading</h1>
<p>Some Text</p>
<p class="bold">Name: <input type="text" name="name" class="single" value="<?php echo set_value('name'); ?>"></p>
<p class="bold">Email: <input type="text" name="email" class="single" value="<?php echo set_value('email'); ?>"></p>
<p class="bold">And then some radio buttons</p>
<p> yes <input type="radio" name="registered" value="yes"> no <input type="radio" name="registered" value="no"></p>
<p class="bold">And a textarea...</p>
<textarea name="description" class="fill" value="<?php echo set_value('description'); ?>"></textarea>
next
</div>
<div id="two" style="display:none;">
<h1>Another Heading...</h1>
<p class="bold">And some more textareas</p>
<textarea name="audience" class="fill"></textarea>
... There are four divs in total with further textarea fields ...
<p class="bold"><input type="submit" name="submit" value="submit" class="center"></p>
back
</div>
<? echo form_close();?>
And finally my model is very simple:
class contact_form extends CI_Model {
public function new_form($data) {
$query = $this->db->insert("contact", $data);
if ($query) {
return true;
} else return false;
}
}
The form processes without any errors, but the data just appears as 0's in MySQL. If at any point I attempt to output the value of $_POST it returns BOOL (false), or with $this->input->post('something'); it returns NULL.
You will notice that no actual validation takes place. Initially I was using $this->form_validation->run() and getting the same results. I thought perhaps I was having trouble with the validation so I stripped it out and now I'm fairly certain my problem is that I'm not passing the $_POST variables correctly.
Can anyone explain why I am failing so hard?
I have now resolved this problem.
For some reason <? echo form_open("form/validation");?> was implementing GET and not POST. Replacing that line with <form method="post" accept-charset="utf-8" action="form/validation"/> resolved the issue.
According to the CodeIgniter documentation, by default, form_open should use POST - I have no idea why in my case it decided to use GET.
I am having a problem with Kohana 3.3. I cannot get the $_POST values using $this->request->post() on my Controller. I dont know what I did wrong on my code. Hope you can help me out here. BTW, I was able to use Twig on all my templates using Kohana 3.3 but I wasn't able to process the data from my forms. Thank you. :-)
Here is my code:
Controller:
class Controller_Setup extends Controller{
public function action_item_group(){
if (HTTP_Request::POST == $this->request->method()){
// Post has no data
print_r($this->request->post());
}
$this->response->body( Twig::factory('setup/sample_form') );
}
}
View
<form class="form-horizontal" action="item_group" method="post" name="setup_form">
<input type="text" value="">
<button type="submit">Save</button>
</form>
You need to set name or id attributes to your HTML elements. Try this code and see if it's working now:
<form class="form-horizontal" action="item_group" method="post" name="setup_form">
<input name="group_name" type="text" value="">
<button type="submit">Save</button>
</form>
I am new to PHP Codeigniter framework. I am designing a page in which I am using a link. On clicking the link it is calling a jquery function, which submits form through jquery. I have used codeigniter form validation methods for server side validation and for the timebeing I have disabled the client side validation.
The problem is that in this process when the form is submitted through jquery, the codeigniter form validation method is not working.
But if I am using a submit button to submit the form then the codeigniter form validation method works perfectly.
Please advise me what to do if I need to submit the form through jquery and use the codeigniter form validation method.
Please find the code below:
Login Form:
<?php echo validation_errors(); ?>
<form name="login-form" id="login-form" method="post" action="<?php echo base_url();?>index.php/login/login_form" >
<H2>Login</H2>
<div id="login-box-name">
Email:
</div>
<div id="login-box-field">
<input name="user-name" id="user-name" class="form-login" title="Please Enter Correct User Name" value="" size="30" maxlength="2048" />
</div>
<div id="login-box-name">
Password:
</div>
<div id="login-box-field">
<input name="password" id="password" type="password" class="form-login" title="Please Enter Correct Password" value="" size="30" maxlength="2048" />
</div>
<br />
<span class="login-box-options">
<input type="checkbox" name="1" value="1" title="Want this computer to remember you!!"> Remember Me Forgot password?
</span>
<br />
<br />
<a href="" id="login-submit">
<img src="<?php echo base_url();?>assets/images/login-btn.png" width="110" height="40" style="margin-left:90px;" />
</a>
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
jquery function to submit the form on clicking the "link":
$("#login-submit").click(function()
{
$('#login-form').submit();
return false;
});
Controller function:
public function login_form()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$data['title'] = 'Log In';
$data['errorMessage'] = '';
$this->form_validation->set_rules('user-name', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('login', $data);
$this->load->view('templates/footer');
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('templates/menu');
$this->load->view('index', $data);
$this->load->view('templates/footer');
}
}
Here if I click on the "Submit button of the form, then the codeigniter validation works for user-name and password fields. But if I click the link with id="login-submit", then it calls the jquery function and the form get submitted. But the codeigniter validation does not work for user-name and password fields this time.
I need to submit the form through the link and the codeigniter validation function should work for this.
Thanks in Advance.....
Use .preventDefault() instead of just returning false on the anchor click event.
This has happened to me before and it seems to me that there is a conflict somewhere using .submit() inside a click event and returning false to stop the anchor's default behavior.
The code above is working fine. Actually I made a silly mistake in my code.
I used following code for jQuery and it is working now.
$("#login-submit").click(function(e) {
e.preventDefault();
$('#login-form').submit();
});
Thanks
it happens because it calls the function all the time when clicking on the button. you need to stop that.
$("#login-submit").click(function(e){
e.preventDefault();
$('#login-form').submit();
});