How to submit a form using codeigniter without changing the url? - codeigniter

I'm Unable to submit the form within the same page and getting result in the same page.

Put in the action of the your form the current URL.
action="<?php echo current_url();?>"

You probably need
redirect('controllerName')
So once you send data from the form to the controller, that controller does the processing and then redirects back to the index().

Use Ajax and Javascript to submit form without changing the url.

Related

Codeigniter form post url not reaching routes.php

One of my forms with a POST method is doing something weird. When I submit the form it sends the form url to the browser URL box and it doesn't hit the controller. What is causing this? I'm getting this only for one of my form and I've double checked and triple checked my routes.php file, my controller, etc.

Magento Contact form in ajax

I have a Contact form on the footer of my site. The form is loaded from contacts module( i.e contacts->form.phtml). When clicked on submit button it sends the mail but directs me to 'http://www.example.com/contacts/index/' . Instead of directing me to this page is it possible to send mail via ajax and stay on same page? Please help me..I am on top of this for more than a day.
you can do this without ajax , you need to modify `
public function postAction() in
app/code/core/Mage/Contacts/controllers/IndexController.php
and in this function replace
$this->_redirect('*/*/');
with
$this->_redirectReferer();
it will do your work without ajax , from which ever page you will submit form it will redirect you on same page after submit .
thanks and let me know if you have any problems in this.

CakePHP redirect entire page instead of div

When I'm logging in with my CakePHP login function in the Users controller using ajax. The errors will be displayed in an Div above the login form. But when I have a successful login, I want to redirect the page to the homepage. When I'm using $this->redirect('home'); in the Users controller, the Div will only be updated.
How can I make sure that the entire page will be reloaded? I rather fix this problem with PHP than Javascript 'cause I'm using the Writebuffer method with the JSHelper.
Thanks!
This should be solved in client side. Because client browser gets the AJAX response and puts the output inside the DIV.
Although you can embed javascript code inside the AJAX response. For example, if login is incorrect, then put a window.location javascript code inside the output. But this is not a good solution.
Return an HTTP status code within AJAX response. If for example code is 401, then redirect.
if (data == '401') {
document.location.href="http://example.com";
}

Usage of Ajax form

I am an average newbie to CakePHP. While reading cookbook found Ajax form and its submission. But it lacks more details.
What are the main difference between ajax form and a normal form or What are the more specific cases we need to use Ajax form over normal form?
An example will be appreciated
Thanks....
A properly implemented Ajax form is exactly the same as a regular form, except that if JavaScript is enabled a submit handler will be bound to it that will prevent the normal submission of the form and send the data using the XMLHttpRequest object. The response will then be processed by JavaScript in the current page instead of by loading an entirely new page.
This isn't necessarily a CakePHP question, it's understanding what AJAX is.
AJAX in layman's terms is basically submitting data to a website without reloading the current page. If this is the sort of feature you want, you'll need to look into the RequestHandler component and the JsHelper in the CakePHP book for the version you are using.

Appropriate redirection of forms that use AJAX

I have many forms that use AJAX (w/ jQuery) for validation and data submission. When a form is filled out correctly, I use window.location to redirect the page after I get an acceptable response from the PHP script. On the new page, I use a session variable (set after the AJAX calls) to display the appropriate content. Please tell me if this is standard practice or please give me some suggestions.
Thanks!
Is there a reason you would use a $_SESSION variable to store the post-submission content? Standard practice would be to validate the form via AJAX but submit it in the standard way (i.e. via $_GET or $_POST) after validation. This way you don't need to store anything to a session and you'll likely have less to debug as you'll be submitting the form and displaying its results in the most widely-accepted way.
The benefit of AJAX is typically so that you can submit the form without actually having to do the redirect/refresh. You could get the same functionality by simply having your form POST to the destination URL, redirect to the appropriate place from there or send them back to the form displaying any errors that may have occurred. You could use AJAX to validate the form before the submission to save your users a redirection back to the form to fix their errors, but this is really just a convenience for them. Also, you will have to validate any user data on the server side once it has been submitted, as you can't rely on client-side validation, so you might as well forget the AJAX validation.

Resources