Adrian Enriquez
Adrian Enriquez

Reputation: 8413

http post not working on my code, but working on postman(rest api client)

Here's the code:

$http({
            method  : 'POST',
            url     : 'mailer.php',
            data    : {fullname: "a", email: "[email protected]", confirmEmail: "[email protected]", phone: "09", website: "a.c"} ,  // pass in data as strings
        })  

        .success(function(data, status, headers, config) {
            console.log(data);
            console.log(status);
            console.log(headers);
            console.log(config);

            if (!data.success) {
                // if not successful, bind errors to error variables
                console.log('sending email error');
            } else {
                // if successful, bind success message to message
               // $scope.message = data.message;
               console.log('email sent');
            }
        })

I'm new in angular and php so please take it easy on me. :)

PHP Code:

$mailSend = null;

if (isset($_POST['email'])) {
    $fullname = (isset($_POST['fullname'])) ? $_POST['fullname'] : '';
    $email = (isset($_POST['email'])) ? $_POST['email'] : '';
    $confirmEmail = (isset($_POST['confirmEmail'])) ? $_POST['confirmEmail'] : '';
    $phone = (isset($_POST['phone'])) ? $_POST['phone'] : '';
    $website = (isset($_POST['website'])) ? $_POST['website'] : '';

    if (strlen($fullname) > 0 && strlen($email) > 0) {
        $to = '[email protected]';

        $subject = 'Pricing Request';

        $header  = "From: " . strip_tags($email) . "\r\n";
        $header .= "Reply-To: ". strip_tags($email) . "\r\n";
        $header .= "MIME-Version: 1.0\r\n";
        $header .= "Content-Type: text/html; charset=UTF-8";

        $content = '<strong>Full Name: </strong>' . $fullname;
        $content .= '<br/><strong>E-mail: </strong>' . $email;
        $content .= '<br/><strong>Phone: </strong>' . $phone;
        $content .= '<br/><strong>Website: </strong>' . $website;


        $mailSend = (mail($to, $subject, $content, $header));
    }
}

Console Ouput:

200
sending email error 

Upvotes: 0

Views: 1206

Answers (1)

Oliboy50
Oliboy50

Reputation: 2721

You never return an object with success property... that's why data.success is false (cause it doesn't exist at all) :

if (!data.success) {
    // if not successful, bind errors to error variables
    console.log('sending email error');
}

But the request seems OK, you even get a 200 response status which mean "It Worked!".

Upvotes: 1

Related Questions