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.


Messages In This Thread
Passing values to a controller through jquery ajax post - by El Forum - 07-16-2010, 10:32 AM
Passing values to a controller through jquery ajax post - by El Forum - 07-16-2010, 12:33 PM
Passing values to a controller through jquery ajax post - by El Forum - 07-16-2010, 12:35 PM
Passing values to a controller through jquery ajax post - by El Forum - 07-16-2010, 12:44 PM
Passing values to a controller through jquery ajax post - by El Forum - 07-16-2010, 01:06 PM
Passing values to a controller through jquery ajax post - by El Forum - 07-16-2010, 02:36 PM
Passing values to a controller through jquery ajax post - by El Forum - 07-16-2010, 03:38 PM
Passing values to a controller through jquery ajax post - by El Forum - 07-16-2010, 07:54 PM
Passing values to a controller through jquery ajax post - by El Forum - 07-18-2010, 05:57 AM



Theme © iAndrew 2016 - Forum software by © MyBB