Welcome Guest, Not a member yet? Register   Sign In
Passing values to a controller through jquery ajax post
#1

[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)
    {
        $("#"+id+" .box").html("<img style='margin:20px 110px;' alt='sending...' src='/images/sending.gif'/>");
        $.post("/lounge/send_email",
               {
                   email: emailFromVal,
                   message: messageToVal              
                },
         function(data){
            
             $("#"+id+" .box").html("<p>"+data+"</p>");
         }
        
        );
        return true;
    } else {
        return 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()
    {
        if ($this->input->post('email'))
        {
            if ($this->input->post('message'))
            {
                $this->email->clear();
                $this->email->message($this->input->post('message'));
                $this->email->from($this->input->post('email'));
                $this->email->to('[email protected]');
                $this->email->reply_to($this->input->post('email'));
                $this->email->subject("Feedback Received");
                if (!$this->email->send())
                {
                    echo "Email could not be sent.";
                }
                else
                {
                    echo "Email sent successfully. Thank you for your feedback!";    
                }
            }
            else
            {
                echo "Email could not be sent. No Message Found.";    
            }
        }
        else
        {
            echo "Email could not be sent. No Email Address Found.";
        }
    }

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.
#2

[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
if (hasError == false)
//... rest of js

Or in Firefox, use firebug, check your console and see what data is being sent from the request.
#3

[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?
#4

[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.
#5

[eluser]vintage75[/eluser]
I tried putting this code at the top of my send_email function:

Code:
if(empty($_SERVER['CONTENT_TYPE'])){
     $type = "application/x-www-form-urlencoded";
     $_SERVER['CONTENT_TYPE'] = $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){
        $("#"+id+" .box").html("<img  110px;' alt='sending...' src='/images/sending.gif'/>");
        $.post("/js/send.php",
               {
                   email: emailFromVal,
                   message: messageToVal              
                },
         function(data){
            
             $("#"+id+" .box").html("<p>"+data+"</p>");
         }
        
        );
        return true;
    } else {
        return false;
    }

Here is the send.php file

Code:
&lt;?php
$emailFrom = $_POST['email'];
$mailFrom = 'Test Test';
$subject = 'Test Mail';
$message = $_POST['message'];
$header =     "From: ".$mailFrom." ".$emailFrom."\r\n".
            "Reply-To: ".$emailFrom;
            
if (mail('<email>', $subject, $message, $header,'-f <email>')){
    echo "Your email has been sent!";
} else {
    echo "There was an error sending this message.";
}
?&gt;
#6

[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?
#7

[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.

User-Agent: CodeIgniter
Date: Fri, 16 Jul 2010 21:37:20 +0000
From: <>
Return-Path: <>
Reply-To: "" <>
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <4c40d1106777c>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
=?utf-8?Q?Feedback?=

Any idea how to fix this?

Thank you for the help so far Smile
#8

[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?
#9

[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.




Theme © iAndrew 2016 - Forum software by © MyBB