AJAX contact form in CodeIgniter
Posted
by
Ross
on Stack Overflow
See other posts from Stack Overflow
or by Ross
Published on 2011-01-05T12:49:41Z
Indexed on
2011/01/05
12:53 UTC
Read the original article
Hit count: 286
Few questions:
I'm using CI and JQuery AJAX.
In my code below, I assemble dataString
, which by default, is appended to the URL as a query string.
I've changed the AJAX "type" to POST, so my question is - how do I access dataString
in my CI app?
It would seem I still have to use
$name=$this->input->post('name')
Which to me, makes setting dataString
redundant?
--
I've tried searching but can't really find anything concrete.
Would it be possible to still make use of CIs validation library and AJAX?
if($this->form_validation->run() == FALSE)
{
// what can i return so that my CI app shows errors?
}
Normally you would reload the contact form or redirect the user. In an ideal world I would like the error messages to be shown to the user.
Jquery:
$(document).ready(function($){
$("#submit_btn").click(function(){
var name = $("input#name").val();
var company = $("input#company").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
var message = $("textarea#message").val();
var dataString = 'name=' + name + '&message=' + message + '&return_email=' + email + '&return_phone=' +
phone + '&company=' + company;
var response = $.ajax({
type: "POST",
url: "newsite/contact_ajax/",
data: dataString
}).responseText;
//$('#contact').hide();
//$('#contact').html('<h5>Form submitted! Thank you!</h5><h4>We will be in touch with you soon.</h4>');
//$('#contact').fadeIn('slow');
return false;
});
});
hope i've been clear enough - if anyone has a decent example of a CI contact form that would be great. there's mixed stuff on the internet but nothing that hits all the boxes.
thanks
© Stack Overflow or respective owner