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
Related
I am trying to create a data-entry,with controller which inputs the data, displays it and confirm for any edit then uses a method to submit it to the model.
The problem is that in the load the model in the function post_data() the value of $this->input->post() return an empty array.
I am entering the data returning to the get_data function and then displaying it in the data.php in the view.
using
data.php in view post the data to the post_data method.
<form id='form' action="<?php echo base_url("welcome/post_data"); ?>" method="POST" style="display:inline;">
<input type="text" name="xyz" value="<?php echo $this->input->post("xyz") ?>" />
the controller is
protected $arr;
public function index(){
$this->load->view('index/index');
// $this->load->library('Controllerlist');
// print_r($this->controllerlist->getControllers());
}
public function get_data(){
echo "matoercod";
$this->load->view("index/data");
}
public function post_data(){
$this->load->model("form1","form",TRUE);
print_r($this->input->post());
$blue=$this->form->insert_data($this->arr);
print_r($blue);
if($blue){
echo "Successfully added to database";
}
}}
Why does print_r() method return an empty array?
$this->input->post() in the post_data method return empty array.
if Iam right $this->input->post() should is global to all the method in Controller CI class.
Your input tag in form doesn't have a name attribute.
<form id='form' action="<?php echo base_url("welcome/post_data"); ?>" method="POST" style="display:inline;">
<input name="xyz" type="text" value="<? php echo $this->input->post("xyz") ?>" />
</form>
Edit:
Also the input tag is closed in a wrong way (closing before the value attribute).
<input type="text name="xyz" value="<? php echo $this->input->post("xyz") ?>" />
If you are loading the view data.php in get_data:
public function get_data(){
echo "matoercod";
$this->load->view("index/data");
}
Then how are you accessing the get_data url? If you are just typing it in the browser, then you are not POSTing you are GETing and so $this->input->post is always going to be an empty array.
It's not entirely clear to me how you are calling your method get_data, but you should probably try to alter it so that it more carefully constructs the data you want to be displayed in any view that it loads
public function get_data(){
$view_data = array(
"xyz" => "here is some value" // you could get this value from anywhere
);
$this->load->view("index/data", $view_data);
}
Then in your view data.php you would want to refer to just $xyz instead of $this->input->post("xyz"):
<form id='form' action="<?php echo base_url("welcome/post_data"); ?>" method="POST" style="display:inline;">
<input type="text" name="xyz" value="<?php echo $xyz; ?>" />
Note that second parameter to the view loading function. It's an array and each associative key in the array will be expanded into a variable within your view.
I've been working on php application and I've used a OO programming so far to develop it . I have declared a class like this
<?php
class User
{
public function Add_element()
{
//adds some element
}
public function Delete_Element($element_ID)
{
//delete the element with that ID
}
}
?>
I've created an object of user class (for example $user = new User()) in my index.php file which includes my view.index.php and the view.index file is organized as follow
<html>
<header></header>
<body>
<input type="button" id="ID" value='Delete Element' >
</body>
<html>
I know this question may seem repetitious but I want to know how can I call the $user->Delete_element($ID) method upon clicking the button .
You could do something along these lines:
First of all use a form:
<form method="POST" action="delete.php">
<input type="hidden" name="element_id" value="id">
<input type="button" name="delete" value="Delete element">
</form>
And create the following php script:
if(isset($_POST['element_id']))
{
$element_id = intval($_POST['element_id']);
$user->deleteElement($element_id);
}
OR use ajax, which is a bit more complicated.
Edit:
If you do not want to refresh the page you should use AJAX. Start off by including jQuery, which makes this a lot easier. Now you need something like this:
$("form").submit(function(e){
var data = {};
data.element_id = $(this).find("[name='element_id']").val();
$.POST("delete.php", data).done(function(response){
alert(response);
});
e.preventDefault();
});
edit your html file (change the "input" tag to an html form):
<form action="Delete_Element.php" method="POST">
<input type="button" name="ID" value='123456789' >
</form>
create a new php page called "Delete_Element.php":
<?php
if ( isset($_POST["ID"]) )
{
/* init the "user" object, assuming it is init to: $myUser */
//this line invokes your method, given the posed IP param. .
$myUser->Delete_Element($_POST["ID"]);
}
I've made joomla 2.5 (works in joomla 3 too) component.
To test component I've created a menu item "mycomtest-main" and placed component in that menu item page. So full local testing url is "localhost/joomla/mycomtest-main".
Component lists many items and there is a button when clicked entry form is shown which is a entry form view of my that mvc component and url becomes "localhost/joomla/mycomtest-main?task=edit&id=4", as I used JRoute::_("index.php?...") to keep safe url.
So after above entry form filled and submitted, it is redirected back to default view - localhost/joomla/mycomtest-main but unfortunately url becomes - localhost/joomla/component/mycomtest-main/ instead localhost/joomla/mycomtest-main.
My component entry form view look like below -
<form action="index.php" method="post" name="adminForm" id="adminForm" enctype="multipart/form-data">
<input type="hidden" name="option" id="option" value="<?php echo $_REQUEST['option']; ?>" />
<input type="hidden" name="task" id="task" value="save" />
<input type="hidden" name="id" id="id" value="<?php if($row!=NULL){ echo $row->id; }?>" />
<input type="hidden" name="page" id="page" value="<?php echo JRequest::getVar('page'); ?>" />
.............rest of the html contents along with submit button
</form>
Also in my mvc component's controller.php file I used jroute well this way -
function save()
{
$model = $this->getModel('entry');
if($model->store())
{ $msg = "saved successfully"; $type = 'message'; }
else
{ $msg = 'error found'; $type = 'error';
}
$urlSet=JRoute::_("index.php?option=". $_REQUEST['option']."");
$this->setRedirect($urlSet, $msg, $type);
}
So how I go so that after entry view form submitted I am redirected to menu item page with
correct url below? -
http://localhost/joomla/mycomtest-main/
That is because you did not build the router for the component. you can check the router.php located inside components/com_user or write your own routing follow this one http://docs.joomla.org/Routing
or you can do like this every time you using redirect:
$menu = $app->getMenu();
$items = $menu->getItems('component', 'com_yourcom');
$itemid = JRequest::getint( 'Itemid' );
for ($i = 0, $n = count($items); $i < $n; $i++) {
// Check to see if we have found the resend menu item.
if (!empty($items[$i]->query['view']) && ($items[$i]->query['view'] == 'yourview')) {
$yourviewid = $items[$i]->id;
}
}
$redirect = JRoute::_("index.php?option=com_yourcom&Itemid=".$yourviewid ,false);
$this -> setRedirect($redirect);
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());
}
I want to build an string/text manipulation app that:
An app consists of a form
User inputs text there as a string using
Click a button - which usues $_POST method to manipulate a string using oop method to :
Example method assigned to a button:
public function revers_sentence() {
$this->string = strrev($this->string);
return $this;
}
Then the manipulated string is displayed in the same form.
User can do another manipulation with a converted string
How to assign method to a button to trigger the function and display results in the same form?
Can it be achived in a single php file to send and get a result?
(I want to store my classes in a seperate file)
Any help/idea Appreciated - any wise guy?
edit:
<?php
require_once('mod.php');
$string='';
if (isset($_POST['string']))
$string=$_POST['string'];
if (isset($_POST['DoStuff']))
{
//$string = reverse_1_word($string);
**$string->reverse();**<-------------------------------Fatal error:---------
}
if (isset($_POST['DoOtherStuff']))
{
$string = doOtherStuffWithThisString($string);
}
?>
Fatal error: Call to a member function odwroc() on a non-object on line 14 so how to make every new string an object ?
I would do something like that:
<?php
Class MyString{
private $string='';
function __construct(){
if (isset($_POST['string']))
$this->string=$_POST['string'];
}
function doStuffWithThisString(){
$this->string=$this->string.'!';
}
function doOtherStuffWithThisString(){
$this->string=$this->string.'!!!';
}
function getString(){
return $this->string;
}
}
$myString = new MyString();
if (isset($_POST['DoStuff']))
{
$myString->doStuffWithThisString();
}
if (isset($_POST['DoOtherStuff']))
{
$myString->doOtherStuffWithThisString();
}
?>
<form action="" method="post">
<!-- blank action attribute will post form to the current page-->
<input type="text" value="<?=$string->getString()?>" name="string" />
<!-- <?=$string->getString()?> is the same as <?php echo $string->getString(); ?> -->
<input type="submit" value="Do Stuff" name="DoStuff" />
<input type="submit" value="Do Other Stuff" name="DoOtherStuff" />
</form>
You could have a form like this :
<form action="currentFile.php" method="post">
<input type="text" value="myValue" name="text" />
<input type="submit" value="Reverse !" name="reverse" />
<input type="submit" value="Other manipulation" name="otherManip" />
</form>
When you receive the data, you can do this :
if ( isset($_POST['reverse']) ) {
// User clicked on the reverse button
} elseif ( isset($_POST['otherManip']) ) {
// User clicked on the other button
}
to achive the send and get in a single PHP file you have to set the forms action to the actual site you are on. I do this ba writing <form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post"> FORMFIELDS AND DATA </form>
To work with the returned values you just have to build an if-statement, that checks for the passed values if (isset($_POST['buttonname'])){ //DO SOMETHING}