I have a FancyBox in the home page which contains a registration form.
Validation is working fine.
Currently, I have to manually click on the FancyBox to see the error messages on that form.
Here's the code which reloads the page. But I want it to show the FancyBox as soon as the page loads. It is just a simple validation check.
if ($this->form_validation->run() == FALSE)
{
$this->load->view('valform');
}
else
{
$this->load->view('formsubmit');
}
So what I want is to show that FancyBox with validation errors.
Validation is working, as it is shown in the FancyBox; when the page reloads I have to manually click on that FancyBox to see the validation errors. Is there a way I can store it in a session and use that instead? If yes, how? Or how can I use ajax for this, and get validation errors to show on the FancyBox?
You can see the example below. Try to implement it with ajax and jquery.
public function ajax_()
{
$this->form_validation->set_rules('par','name of something','required');
if($this->form_validation->run()!=TRUE)
{
echo json_encode(array('result'=>'error','msg'=>validation_errors()));
}
else
{
echo json_encode(array('result'=>'success','msg'=>'success'));
}
}
Related
the situation is, I want to show the modal once the form submission is successful.
what i currently do is, after I submit and the form was submitted successfully, I redirect to the same input page. Now the question is, is it possible to set the flash data and pass a jquery script to toggle the modal upon redirection ? here's how i set it in the controller function after the submission was successful.
$js = "<script>$('#mymodal').modal('show');</script>";
$this->session->set_flashdata('js',$js);
then on the view file , I placed a checker at the top so that I will get triggered upon redirection to it.
if($this->session->flashdata('js')){
echo $this->session->flashdata('js');
}
but unfortunately, the modal didn't get triggered to popup at all.. show how ?
You can do it with this way..
From coontroller ... You send a simple flash message like..
$js = "1";
$this->session->set_flashdata('js',$js);
And In View,
<?php
if(isset($this->session->flashdata('js')) && $this->session->flashdata('js')=="1"){
?>
<script>$('#mymodal').modal('show');</script>
<?php } ?>
It will be working fine, Enjoy the code.
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)
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.
I'm using Fancybox 2 to display some forms on my website. The form comes through from an external page into an IFrame to let the user post a message, kind of like twitter does. However I want the user to be re-directed after the form has been posted. So they post the form to a database, the Fancybox window shuts down and then they are redirected to the posts page where they see their newly posted message. Is there a way of doing this succesfully?
You can try this:
<script>
if(data == 1){
//window.location.replace("dashboard.php"); //will open the page in the fancybox
parent.$.fancybox.close(); //will close the fancybox
//parent.window.location.replace( your_url_here ); //your redirecting URL here
parent.window.location.href = 'dashboard.php'; //your redirecting URL here
}
</script>
I would recommend you to use a real form rather than a post action. You are not really using form submission otherwise, but just a POST request.
If you do that, you could use the target="_top" inside the <form tag and submit the results using the submit function of jQuery.
I would like to create popup contact form with validation like i did here http://89.212.111.174/delovtujini.si and click “VPIS V BAZO”.
You will get popup where you can fill contact form. How can i do this with CI? Here on this example i do everything in the same html page. In CI i try to create new controller for contat form but i dno’t know how to open the window. I also try to use http://fancyapps.com/fancybox/ I try. but none solution works.
Can someone explain me how to do? Maybe is better to use https://github.com/EllisLab/CodeIgniter/wiki/Ajax-Framework-For-CodeIgniter
Thx
there are 3 ways to trackle your issue.
1) Use custom inline lightbox like what is done on
http://89.212.111.174/delovtujini.si
First post the form back to the same page like below:
public function sign_up()
{
// Setup form validation
$this->form_validation->set_rules(array(
//...do stuff...
));
// Run form validation
if ($this->form_validation->run())
{
//...do stuff...
redirect('');
}
// Load view
$this->load->view('my_form');
}
In the view when you detect a POST you must have javascript
to "open" the lightbox on page load since it won't be display by default (i.e. when you load the page normally the lightbox is "closed" and it is "opened" only when the button is clicked.)
2) Use a iframe lightbox
create the form on a separate CI controller/view and display in within an iframe when the button is clicked.
when the form is submitted you can then call javascript to close the lightbox.
3) Use ajax
both inline and iframe lightbox can work with an ajax form
the idea the same as using an iframe lightbox. Once the form is submitted via ajax, use javascript to close the lightbox.