CodeIgniter Forums
Trying to not go to a different page after form submit... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Trying to not go to a different page after form submit... (/thread-761.html)



Trying to not go to a different page after form submit... - alexandervj - 01-14-2015

Hello,
I have a form you can see at www.trilumina-temp.com and then click on the "Download Now!" link under the slider where it says "Download a Product Sheet" ..its not really a download though. I would like to not go to a different page after you submit the form, and just to show text or show a hidden div in the modal after the form is submitted saying "Thank you" or something like that. Is there a way I can do this? Thanks


RE: Trying to not go to a different page after form submit... - dmyers - 01-14-2015

For this I would recommend submitting the form with Javascript VIA ajax (jQuery is a nice javascript lib for this) against a different controller method. That controller method can then return JSON which you can test in your Javascript for a error or link or something. Which you can then display in your <div> for example. (Thank you here is your link etc...)

I have been using this method for years to post all of my forms to have the model validation done by the model only returning the responds to the end user. If it's all good then the form is submitted "for real". Of course the controller I runs it again against the same model (just for security because it already was checked it "should" be good). That keeps me from writing form validation in Javascript AND again in PHP. I only need to write it once in PHP (in the model). This also saves me from the form post -> validate -> fail -> form redisplay -> form post -> validate -> fail.... loop. The user never posts the form until it's actually "good".

Hope that helps?


RE: Trying to not go to a different page after form submit... - alexandervj - 01-14-2015

That helps very much. I'm not too familiar with jquery or json - do you know of any references or tutorials that can show how to do something like this?


RE: Trying to not go to a different page after form submit... - Hobbes - 01-14-2015

check out jquery api documentation for ajax here: http://api.jquery.com/category/ajax/


RE: Trying to not go to a different page after form submit... - alexandervj - 01-14-2015

I found this ...
http://www.sanwebe.com/2011/12/making-simple-jquery-ajax-contact-form
I'm gonna go through this but I'm a little unsure what I need to change to make it work with codeigniter instead of just plain php. Any help is appreciated


RE: Trying to not go to a different page after form submit... - dmyers - 01-17-2015

(01-14-2015, 09:43 AM)alexandervj Wrote: I found this ...
http://www.sanwebe.com/2011/12/making-simple-jquery-ajax-contact-form
I'm gonna go through this but I'm a little unsure what I need to change to make it work with codeigniter instead of just plain php. Any help is appreciated

Sure, Let me try to explain some to you ok?

Of course this line handles the click on the html element with the id="submit_btn"

$("#submit_btn").click...

It then checks that a few required fields are filled in setting "proceed"

Finally if proceed is true it sets "post_data" with the post data and sends it to $.post('contact_me.php',

the contact_me.php page (therefore you would need a controller which handles that).

you then need to send json as your reply

something like this.

$data['type'] = 'error'

echo json_encode($data);

it than stays there and let's you post again.

If you like I can put together a demo which does the validation server side like I explained.

zip it up and post a link here?

Would that help? I don't mind.

dmyers


RE: Trying to not go to a different page after form submit... - dmyers - 01-17-2015

Here you go https://github.com/dmyers2004/CI3-Ajax-Post---Model-Validation

This does the posting like I said.
The returned JSON could for example add a <a href="downloads/file765678765.zip">Download</a>
To the page $('#link').html(response.download);


RE: Trying to not go to a different page after form submit... - dmyers - 01-18-2015

Ok, I updated the code to provide a download link you requested.


I also created a tag on the GIT repro to the original validate / post workflow.
https://github.com/dmyers2004/CI3-Ajax-Post---Model-Validation/tree/basic-validation-post

Hope that helps?


RE: Trying to not go to a different page after form submit... - alexandervj - 01-28-2015

Thanks a lot!