refresh page with joomla component - model-view-controller

I have a simple form in my tmpl/default.php:
<form id='AddForm' action="<?php echo JRoute::_('index.php?option=com_mycomponent&task=addcam'); ?>" >
<p>
<label for="CamName">Name:
</label>
<input type="text" id="CamName" name="cam_name" />
</p>
<button type='submit' class='submit_cam' name='addcam' value='Add'>Add</button>
<button type='reset' class='cancel_changes' name='cancel_changes' value='Cancel'>Cancel</button>
</form>
In my controller.php file I'm trying to process the values:
function addcam()
{
$add_name=JRequest::getString('cam_name');
$model = &$this->getModel();
$model->AddWebcam($add_name); //send to model to add to DB
}
In my model I just return the result of the query. With this implementation I just get routed to an empty page. I'd like to have it refresh the current page. Typically you do this with action="" but in my case I need it to route to the function called addcam in the controller. Or is there a better way to do this?

A common technique in Joomla when directing to the task is to have that function do a full redirect to a view at the end. This prevents a page refresh from trying to resubmit the data and leads to a cleaner url for the client. To do this, try the following:
function addcam()
{
$add_name=JRequest::getString('cam_name');
$model = &$this->getModel();
$model->AddWebcam($add_name); //send to model to add to DB
JFactory::getApplication()->redirect(JRoute::_(index.php?option=com_mycomponent&view=whatever));
}
Obviously, update the JRoute bit to the url you actually need. You can also include a message if you would like (like "Saved!"): http://docs.joomla.org/JApplication::redirect/1.6

Related

Code Ingiter POST data empty

I'm just have a trouble when POST a multipart form with codeigniter. The form is contain text field and 8 field of file upload. the problem is, the file is uploaded successfully with no error, but the field of text is empty in my controller.
I tried to print_r the POST data and get an empty array. But sometimes when I'm using google chrome, It's not got any problem.
Any solution?
<form name="form" action="some action page.php" method="post" enctype="multipart/form-data">
<input type="text" name="inputfieldvalue" value="Input Field">
<input type="file" name="uploadedfile" id="uploadedfile">
<input type="submit" value="Submit" >
</form>
// suppose your form is like this as above then to check textfield value
<?php echo $_POST['inputfieldvalue']; ?>
// pass the name attribute whatever you have given to $_POST[your name field value]. like this
Complete example should be working.
your form target action to function my_form in My_controller controller. Lest try input with name WTF
<form name="form" action="my_controller/my_form" method="post" enctype="multipart/form-data">
<input type="text" name="WTF" value="Input Field">
<input type="submit" value="Submit" >
</form>
your cotroller action in My_controller.php file (CI Controller folder) should look le this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class My_controller extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index(){
/* do something for page my_controller if you want */
} // end of index
function my_form() { // = my_controller/my_form
$here_is_it = $this->input->post('WTF'); // call value of input with name WTF
/* test it */
print_r($here_is_it);
}
Extra, to make sure everything it's okay. You can Enable auto load feature in Codeigniter. follow this tutorial
I Just check it for a while and the problem is not in the php or my html form code. But the problem is in my php.ini config. By increase upload max file size fix the problem. I just confused before because the form sometime work well and sometimes give an empty value.
Thanks in advance

How to make a pretty simple Codeigniter url route?

I've looked some websites have a simple route in their login/registration form. For instance, their form redirect to action="/function/method". Recenttly I want to implement that route in codeigniter (I use codeigniter 3), but my form still break. I don't know how to.
Below is my simple controller:
/*
* i.e Folder: Login
* i.e Controller name: Login
* i.e steps: login/login/verify
*/
class Login extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function index()
{
// Load login view
}
public function verify()
{
// Processing data from login form
}
}
And the login form is:
<form action="<?php echo base_url('login/login/verify'); ?>" method="post" class="form-horizontal">
<input type="text" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<button class="btn btn-login" type="submit">Sign in</button></p>
</form>
As we can see the action is redirect to base_url()/login/login/verify or if we print this will make http://localhost/domain/login/login/verify. I think it's seem too long. What I want to is to simplify that route, for instance, action="/login/verify". I try to make route $route['login/(:any)'] = "login/login/$1" and it's not working. A little trick maybe to rename login folder i.e user, so that will be user/login/verify. But it's not what I want to.
Any idea or direction?
The issue is a you're attempting to call a method that doesn't exit.
Remember that Codeigniter's URL Scheme is:
http://localhost/codeigniter_installation/controller/method/params/..../
From your question, you appear to be calling:
/login/login/verify
login - controller
login - method
verify - param
This would be listed as: function login($action = "verify") { do_stuff() } in your controller.
When you should just be calling: /login/verify

DOMPDF Laravel 5 Interactive Page

I'm using Laravel 5 to generate a form for a warehouse. In Main form you can select which items to get and it should generate a PDF with the items (all info), people to get the items, date and invoice number.
All the information is on a DIV called 'invoice'. How can I send this object to a new view to generates the PDF. I read about 'invoking dompdf via web' to make it more interactive but the documentation is not clear enough or doesn't fit with I'm needing.
This is my current code:
From:
Recibo.js
$('#createPDF').click(function()
{
$htmlData = "data";
$_token = $('[name="_token"]').val();
$.get('recibos/pdf',{ html:$htmlData, _token: $_token })
.done(function(data)
{
console.log('Done PDF!');
});
});
ReciboController.php
public function reciboPDF(Request $request)
{
$data = $request->get('html');
$pdf = \PDF::loadView('create.template.formReciboPDF',compact('data'))->setPaper('letter')->setOrientation('landscape');
return $pdf->stream();
//return view('create.template.formReciboPDF');
}
formReciboPDF.blade.php
<tr>
<td>{{$data}}</td>
<td>Papeleria</td>
<td>Unidad</td>
<td>Grande</td>
<td>La Palma</td>
<td>2342423424234</td>
<td>Bueno</td>
<td>9</td>
</tr>
Instead of trying to capture the HTML can you send the selection parameters to the server and rebuid the page? Then you would just need to feed the generated HTML into dompdf and render. If visual appearance is important you could use hidden checkboxes as the selection mechanism.
If you want to use a shortcut you can capture the HTML using jQuery.contents().
In your second part it looks like you're trying to send your request via AJAX and get back the PDF. While this is technically possible it's more trouble than it's worth. An easier alternative would be to submit a hidden form to a blank window. It would require some minor changes to your HTML/JS but little else, e.g.
$('#createPDF').click(
function() {
$('#html').val($('#invoice').content());
$(this).closest('form').submit();
});
<form method="POST" action="recibos/pdf" target="_blank">
<input type="hidden" name="html" id="html" value="" />
<input type="hidden" name="_token" value="{value from server}" />
<button type="button" id="createPDF">Create PDF</button>
</form>

Codeigniter pass a id to controller from view

I am using codeigniter for this project. I hava a id value in which i pass from controller A to view A. This id value is echo between an anchor tag. When this anchor tag is clicked on, it redirects to another controller B with the id value and processed this id value within controller B. Is there any other way of doing this other than using the uri class? Want to keep the url clean.
I thought of a way of appending hidden input elements when I shift from controller A to view A to controller B, but i realised it can be very messy.
Any clean ways of doing this? Thanks in advance guys!
New to using Stackoverflow See if you could understand my layout:
METHOD 1
Use the URI Class except you have good reasons not to.
From CONTROLLER_A
$data["id"] = ("ID NUMBER");
$this->load->view("VIEW_A", $data);
ANCHOR IN VIEW A
link
IN CONTROLLER_B
$id = $this->uri->segment(3);
.
METHOD 2
USE FORM POST if you want to keep things hidden:
$data["id"] = ("ID NUMBER");
$this->load->view("VIEW A", $data);
ANCHOR IN VIEW A
<form name="myform" id="myform" action="<?php echo base_url() ?>/controllerB/controllerfunction/" method="post">
<input type="hidden" name="id" id="id" value="<?php echo $id ?>" />
<input type="submit" value="See more" />
</form>
You could also use javascript to submit the form here via link if you which:
See more
Tips:You could also use css to hide submit button in the form by setting opacity to 0;
If link is within the form, you could use javascript:this.submit();
OR JQUERY
See more
$('#link').click(function() {
$('#myform').submit();
});
IN CONTROLLERB
$id = $this->input->post("id");
Well hidden inputs will work, and there's may another workaround that once you redirected to the method and use your id value i.e. get data from database, redirect again to another controller method with the clean URL you need , hope this is helpful

Zend Framework fancybox confirmation dialog with ajax and posted values

I created a confirmation page for deleting an item. This works perfectly.
Now I want to appear the confirmation page in fancybox, still passing all the variables and delete the data or close the dialog.
It's important that the process still works as it does now (delete->confirmationpage->redirect/deletion) so that users with javascript disabled can perform these actions without a problem.
Now have I been reading about zend framework,ajax, json and more, but the more I read, the less I understand.
So in a nutshell, my question:
I want to pass a variable to the fancybox and if 'yes' perform the delete action or on 'no' close the dialog. This within the zend framework and jquery.
Any advice or tips are greatly appreciated!
You need to use content switching in your ajax which will then render your action appropriately, for example:
function confirmDeleteAction() {
if($this->_request->isXmlHttpRequest()) {
//This is ajax so we want to disable the layout
$this->_helper->layout->disableLayout();
//Think about whether you want to use a different view here using: viewRenderer('yourview')
}
//The normal code can go here
}
function deleteAction() {
//You probably don't want to show anything here, just do the delete logic and redirect therefore you don't need to worry where it's coming from
}
And in your fancybox, have a view that has a form and two buttons, the form should point to your delete action with the id of whatever your deleting as a get param. Set some javascript up that says something like (and this is mootools code, but you can convert it easily):
$('confirmButton').addEvent('click', function(e) {
e.preventDefault();
this.getParent('form').submit();
}
$('cancelButton').addEvent('click', function(e) {
e.preventDefault();
$('fancyBox').destroy(); //I'm not sure it has an id of fancyBox, never used it
}
Came across the question today and figured I could give it a fresh look. For Zend Framework the solution is really simple:
This is how the action looks:
public function deleteAction()
{
$this->_helper->layout()->disableLayout();
$this->view->news = $this->newsService->GetNews($this->_getParam('id'));
if($this->getRequest()->isPost())
{
if($this->getRequest()->getPost('delete') == 'Yes')
{
$this->newsService->DeleteNews($this->_getParam('id'), $this->view->user->username, $this->view->translate('deleted: ').'<strong>'.$this->view->pages[0]['title'].'</strong>');
$this->_helper->flashMessenger(array('message' => $this->view->translate('The page is deleted'), 'status' => 'success'));
$this->_helper->redirectToIndex();
}
elseif($this->getRequest()->getPost('delete') == 'No')
{
$this->_helper->flashMessenger(array('message' => $this->view->translate('The page is <u>not</u> deleted'), 'status' => 'notice'));
$this->_helper->redirectToIndex();
}
}
}
The delete.phtml
<div>
<h2><?php echo $this->translate('Delete news'); ?></h2>
<?php
foreach($this->news as $news)
{
?>
<p>
<?php echo $this->translate('You are now deleting <strong>\'').$news['title'].$this->translate('\'</strong>. Are you sure about this?'); ?>
</p>
<p>
<?php echo $this->translate('<strong>Note! </strong>This action is inreversable, even for us!'); ?>
</p>
<form action="<?php echo $this->baseUrl(false).'/news/index/delete/'.$news['id']; ?>" method="post">
<?php
}
?>
<input class="submit deleteYes" type="submit" name="delete" value="<?php echo $this->translate('Yes'); ?>" />
<input class="submit deleteNo" type="submit" name="delete" value="<?php echo $this->translate('No'); ?>" />
</form>
And this is how the link to delete a file looks (within a foreach loop with my database results)
<a class="deleteConfirmation" href="<?php echo $this->baseUrl(false).'/news/index/delete/'.$news->id; ?>">delete</a>
This works like you would expect; when you click on delete the user goes to the delete confirmation page and redirects the user back to the index after the form is submitted. But I wanted the confirmation in a dialog (in my case I use fancybox). To achieve this, add the following jquery to your index:
$('.deleteConfirmation').fancybox({
// Normal fancybox parameters
ajax : {
type : "POST"
}
});

Resources