![]() |
Passing values to a controller through jquery ajax post - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Passing values to a controller through jquery ajax post (/showthread.php?tid=32224) |
Passing values to a controller through jquery ajax post - El Forum - 07-16-2010 [eluser]vintage75[/eluser] Hi, I have created a form on my website for sending emails. Now what I'd like to do is check for errors using javascript and send the email using ajax so the page isn't reloaded. The error checking is fine but I have run into a problem when trying to pass the inputted data to a controller method so that it can actually be sent. Here is part of the jquery code I'm using to post the values to the controller Code: if (hasError == false) Now this runs fine and returns true. It successfully runs the send_email method in the lounge controller. Here is my send email method: Code: function send_email() The problem is that $this->input->post('email') is empty. I tried printing the whole $_POST array and it's all empty. Somehow the values are not making it to the controller. Did I take a wrong approach to doing this? Or maybe I am missing something? Any help would be appreciated. Passing values to a controller through jquery ajax post - El Forum - 07-16-2010 [eluser]pickupman[/eluser] Not being able to see what element you have attached your submit/click handler to, but you may not properly getting the value form your form fields. What happens if you use: Code: alert('Email: ' + emailFromVal + ' Message: ' + messageToVal); //add this like to check if values are properly set Or in Firefox, use firebug, check your console and see what data is being sent from the request. Passing values to a controller through jquery ajax post - El Forum - 07-16-2010 [eluser]vintage75[/eluser] I have checked for that and everything seems to be working fine. The values are alerted as expected. The javascript actually checks for empty fields so there is no way I could pass empty variables. So basically, the way I am currently doing things should work? Passing values to a controller through jquery ajax post - El Forum - 07-16-2010 [eluser]pickupman[/eluser] Here is something to take a look at for a [url="http://www.bradino.com/php/empty-post-array/"]empty $_POST array[/url]. You should be able to see your http headers using firebug. Passing values to a controller through jquery ajax post - El Forum - 07-16-2010 [eluser]vintage75[/eluser] I tried putting this code at the top of my send_email function: Code: if(empty($_SERVER['CONTENT_TYPE'])){ No luck though, same result. It doesn't seem like it's a data type issue to me. I just tried using the same jquery function, just changed the post to location (js/send.php) to a new php file I made for test purposes and it works perfectly. So somehow when posting to the controller, the values are getting lost. So it's a CodeIgniter problem. Code: if (hasError == false){ Here is the send.php file Code: <?php Passing values to a controller through jquery ajax post - El Forum - 07-16-2010 [eluser]pickupman[/eluser] Good idea for debugging purposes. Have you extended the Input class at all, or using any other 3rd party library that is unsetting _POST? Passing values to a controller through jquery ajax post - El Forum - 07-16-2010 [eluser]vintage75[/eluser] I think I found the problem. A library that I was loading was somehow resetting the $_POST array like you said. I didn't make this library as I'm not working on this project alone. I'll have to talk to my colleague about it. The email is being sent but for some unknown reason $this->email->send() is returning false. Echoing $this->email->print_debugger(); returns: Code: Unable to send email using PHP mail(). Your server might not be configured to send mail using this method. Any idea how to fix this? Thank you for the help so far ![]() Passing values to a controller through jquery ajax post - El Forum - 07-16-2010 [eluser]pickupman[/eluser] What's your server environment, and what mail server are you running? Can you send mail using standard php mail() without any error messages? Passing values to a controller through jquery ajax post - El Forum - 07-18-2010 [eluser]vintage75[/eluser] I tried using the php mail function and I was surprised to see that once again, errors where coming up saying that the values in POST are undefined. The weird thing is, I receive the message anyway. I actually receive the message twice. But one is an empty message and the other one is fine. I'm really confused now. I'm not sure if this is a server issue because I tried doing this on a different server (same host) and it worked fine. Or it could possibly still be some conflict within the project. I haven't tried moving the whole project to a different server to test. I'll keep that as a last resort as I'd have to move the database as well and it's not a small one. |