Basically, I have my controller function for adding in my case a page but let’s focus on the function:
public function add() {
$this->session->unset_userdata('postID');
if ($this->form_validation->run() == TRUE) {
// Form Validation
}
$this->data['subview'] = 'blah blah';
$this->load->view('blah blah.php', $this->data);
}
Basically when the form is submitted it will still unset the postID in this case, however i want to ensure that if the form is submitted and there are errors that this is missed and it doesn’t redo some of my functions and variables. This is happening for a lot of my content when the form is submitted it re-initiates variables that i want to be ignored.
I also tried the following but it didn’t work either:
if (!$this->form_validation->run()) {
$this->session->unset_userdata('postID');
}
How do i avoid the entire page being redone when the form is validating as it re-performs all the page load content?
Thanks
You can check if the submit button was pushed by using
if($this->input->post('the-name-of-the-submit-button')) {
$this->session->unset_userdata('postID');
}
Basically, $this->form_validation->run() checks if the form was submitted and if it passed validation rules, whereas the above simply checks if the form was submitted at all.
You need to do an ajax submission if you don't want the page to refresh:
http://phpsblog.agustinvillalba.com/sending-forms-ajax-codeigniter/
Alternatively, you can fill in the field values so they don't "RESET", ie <input value="<?php echo $_POST['value'];?>" />, but ajax is a much better solution.
You can return errors in JSON from your codeigniter controller and display them on your page.
If you do any type of request with your browser, the page is always going to refresh, unless you use ajax/javascript.
Related
I have a page that takes form input, it is written using HTML and PHP(codeignter framework), the issue I am facing is when there is a form validation error, the page refresh is not reloading the page like brand new, I mean its not clearing the input from fields and the error message still coming up on page refresh and even after I correct the input and hit submit, when I click browser back button the it is still loading the page with validation error..
Any idea why it happens or how to resolve it. I just need the page to reload when I refresh it.
Here is my view (view1.php)
<html>
<?php
echo validation_errors();
form_open('search_controller/search_fn');
form_input('input1', set_value('input1'));
form_input('input2', set_value('input2'));
form_input('input3', set_value('input3'));
form_submit('submit', 'Submit');
?>
</html>
Controller Functions
function index() {
$this->load->view('view1');
}
function search_fn {
if($this->input->post('submit') {
$this->form_validation->set_rules('input1', 'XXXX',
'trim|some_validation');
$this->form_validation->set_rules('input2', 'XXXX',
'trim|some_validation');
$this->form_validation->set_rules('input3', 'XXXX',
'trim|some_validation');
if($this->form_validation->run() == FALSE) {
//when validation fails
$this->load->view('view1');
} else {
//when all input is validated sucessfully
do some processlisng,
//load some other view
$this->load->view('view2');
}
} else {
//When page is refreshed, this is supposed to redirect
redirect('search_controller/index');
}
}
when the input validation fails, the url comes back as www.something.com/search/search_fn and when I refresh, it basically loading the same page with same error message. I, somehow, need to reset
the $this->input->post('search'), so that it will be true only when the search button is clicked.
Thanks.
Are you using CodeIgniter's redirect() function after processing form? you should always do that
I mean after you are done handling submitted data, instead of return TRUE or simply closing function, to a simple redirect('CONTROLLER_NAME'); even if you want them to be on the same page
As for the fields, i would suggest using autocomplete="off" in your inputs (if you are not using an <input type="text"> above an <input type="password"> because Chrome and next version of Firefox will ignore automplete="off" in that situation)
I have a login controller, which is suppose to redirect to my index page when the user is valid. The redirect works, but at the index page the url is still that of the validation method ex: login/validate_login/. If i click on a link on the index page, and then try and go back in the brower history, the browser points me to the validate method and not the index page.
How do i fix this?
I have tried using redirect with both refresh and location, but both with no luck.
I suspect this is a problem with the ajax call of jQuery mobile, but i'm not sure.
Any help appreciated.
Kind regards
NOTE: I would post an image of the url at the index page, but i'm not allowed to because i'm a new user.
My validate method:
function validate_login($controller='',$method='') {
$this->load->library('form_validation');
$this->load->model('workout_model');
$this->form_validation->set_rules('ex_password','Koden','trim|required|min_length[4]|max_length[4]|callback_pw_check');
if($this->form_validation->run() == FALSE) {
$this->index();
} else {
if($query = $this->workout_model->validate()) {
$data = array(
'is_logged_in' => true,
'user_id' => $query->id,
'current_exercise' => '1'
);
$this->session->set_userdata($data);
if($controller=='' || $method=='') {
redirect("workout");
} else {
redirect($controller."/".$method);
}
} else {
$this->index();
}
}
}
Two things seem to be necessary for the URL to be correctly updated: use redirect(...) instead of method calls AND disable jQuery Mobile ajax call. Two ways of doing that: add and attribute to the link that initially points to your method,
<a href='.../validate_login/...' data-ajax='false'>...</a>
or, disable ajax calls globally by editing the settings (not tested).
There are two ways to handle this and it has nothing to do with AJAX it's the way CI does things. The quickest and easiest way is to change
$this->index();
to
redirect(index);
The way you're doing it you're not actually redirecting, you're calling the index function on the current URL which is validate_login. The problem with doing it this way is if the login fails it will still remain on the validate_login URL for the next try.
The best way to handle it is to have the actual validate_login function called from your index function in the controller rather than the form itself. So send the form back to index, have the index controller check for the form data and if true call validate_login(). That way you're never actually leaving the index page, it just handles whether or not the login form has been submitted. Solving the URL issue. I actually do this with all my pages that submit forms for any kind of validation.
when you submit via HTML form, such as login register .then you have also some kind of dashboard redirection, you need to update your form tag like this.this occurs when I'm using Codeigniter and jquery mobile use data-ajax="false"
<form id="login-box" method="post" action="<?php echo base_url('index.php/auth/login_user'); ?> " data-ajax="false">
I am using the form validation class in codeigniter. All working fine but when it reloads the form with the errors the page is at the top again. (because it's refreshed). Is there a way I can add an #id onto the end of the url so it will load further down the page where the form is?
I tried redirect('form/form#form, 'refresh') which worked but didnt carry through the errors.
The problem is if you redirect the page then the POST array is cleared. Therefore set_value() can't repopulate your form.
There are a couple of ways you can go about this
Overwrite the session library. Inside this library, stick your post array into a session flash_data on redirect. Then inside your set_value functions ( there are 2 of them ), if you can't find anything in the post array, look for your flashdata.
Don't use the set_value function. Instead, inside your action controller put your form data into flashdata, and inside your form, use $this->session->flashdata('form_field_1'), instead of set_value('form_field_1');
As far as the redirection goes, you're doing it right.
Sorry for being lazy and not posting code. If this isn't clear enough let me know and I can post code.
You're just doing a simple form validation right? If so, you should be able to use set_value to repopulate your form after an error.
http://codeigniter.com/user_guide/libraries/form_validation.html#repopulatingform
If you're using CI's form validation, I'm assuming your form goes back to the controller. The controller should then determine where to go.
if ($this->form_validation->run() == FALSE) {
$this->load->view('form#anchor');
} else {
$this->load->view('formsuccess');
}
You should be able to dynamically set the #anchor tag in your controller based on what errors you get during validation.
Whats the best way to handle the following situation in codeigniter:
Home controller has an index action and a submit action.
The submit action is used for a form submission. I want to load the page through the index controller though - including after form submission i.e. with form errors data to repopulate form inputs on error etc.
Whats the best way to do this - without having the index controller handle the main page loading and the form submission.
If you redirect(), you won't be able to use set_value() for re-populating your form fields.
What's easiest is having your index controller handle both the default load behavior, and the submission.
function index()
{
if($this->input->post('foo'))
{ // something was POSTed
$this->load->library('form_validation');
//validation rules
} else
{ // normal view
//
}
$this->load->view('home');
}
Alternatively, you can just set up your index and submit controller, and have them point at the same view, which detects if validation_errors() are set and re-populates form fields accordingly.
Third option (hackish): you could use flashdata to keep submission errors and the submitted form values across a redirect back to index. Something like this would work:
$this->session->set_flashdata('errors', $validation_errors());
Simply point your form at your submit action:
<form method="post" action="/home/submit">
Then, use the submit action to validate your input before redirecting back to the index action.
Say I have create a registration form. Now to add records into a DB, we send the data to another php file by POST method, where we do some validations and add a record. Is it possible to do it in the same file without sending and getting the data by POST/GET? If no, then why?
EDIT: Even sending to the same php file is SENDING and losing resource. I ask this question because I want to avoid the lost of time on sending by GET/POST and getting by the same Get/POST. And if it is not posible, I want to understand why PHP does not allow.
No. You always have to send data from the client to the server, there is no way around that.
If you dont want to reload the entire page the user is on, you could submit the data via AJAX to the php file responsible for processing it and adding the data. That way the user never leaves the page.
yes ofcourse.
just in your form "action" put
$_SERVER['PHP_SELF']
then in the beginning of your PHP page check if the $_POST is set or not
if(isset($_POST))
{
// actions to be taken after form submission
}
ofcourse you can add a hidden input tag for refining checks for the $_POST. eg in your form
<input type="hidden" name="formSubmit" value="yes" />
then your check should be like
if(isset($_POST['formSubmit']))
{
// actions to be taken after form submission
}
It's possible. For example:
<?php
if(true === isset($_POST['submit']) // check if submit-button was clicked
{
// do some validation here...
// If validation successes add record into db here...
}
else // no post data sent so output the form
{
// output the form here...
}
Yes it is possible set
action="same page"
in form tag.
you can access your all form attributes on same page.
Yes it is easy. The form can post back to its self. This is most easily done by not even specifying the value of action in the form tag.
<form method='POST'>
Then at the top of the page before any content is put on the page, include an if statement to check if the form was submitted.
if (isset ($_POST['post'])) { // 'post' is the name of the submit button
$error = false;
// Do validation
From there do validation and act according to the result.
If you have lots of validation to do, perhaps put that in another file and include it.
include "formValidation.php";
If all is well and all tests are passed use
if ($error === false) {
Header ("Location: confirmation.php");
exit;
}
}
If tests fail, stay on the page keeping all the post data, and display an error.
if (isset ($error) && !empty ($error)) {
echo "<div class='error'>$error</div>";
}