3

Hi and thanks in advance,

I am using the following function in my main.js to send a contact us email. Upon success I am simply redirecting back to home.

How do I send the client a simple alert("Email Sent: Success"); to the client browser.

from main.js

app.use(bodyParser.json());

app.get('/sendMail', function(req, res){

 res.sendFile("index.html"); // html file is within public directory
});


    app.post('/sendMail',function(req,res){
   var username = req.body.name;
   var subject = req.body.subject;
   var fromEmail = req.body.email;
   var toEmail = '[email protected]';
   var bodyMessage = req.body.message;

  //  var htmlData = 'Hello:  ' + toEmail;

var mg = new Mailgun('key-0000000000000000000');
mg.sendText(fromEmail, toEmail,
  'CS: '+subject,
  bodyMessage,
  fromEmail, {},
  function(err) {
    if (err)res.send('NOT SENT: STRUGGLE EVERYDAY');
    else res.redirect('/');
    // res.json({success: true});
    // else     res.send('Success: EMAIL SENT From: '+fromEmail+'  -  To: '+toEmail);
}); 

});

from my index.html page button to send..

<!-- START CONTACT FORM -->

            <form method="POST" action="sendMail" accept-charset="UTF-8" class="contact-form" id="jvalidate" novalidate="novalidate"> 

                <div class="row">
                    <div class="col-sm-12 col-md-4">
                        <div class="field-holder">
                            <input class="form-control input-lg field" placeholder="John Snow" name="name" type="text">
                        </div>
                    </div>
                    <div class="col-sm-12 col-md-4">
                        <div class="field-holder">
                            <input class="form-control input-lg field" placeholder="[email protected]" name="email" type="email">
                        </div>
                    </div>
                    <div class="col-sm-12 col-md-4">
                        <div class="field-holder">
                            <input class="form-control input-lg field" placeholder="Subject" name="subject" type="text">
                        </div>
                    </div>
                    <div class="col-md-12 col-xs-12">
                        <div class="field-holder">
                            <textarea class="form-control" placeholder="Some text" name="message" cols="50" rows="10"></textarea>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-12">  
                            <div class="alert alert-success" role="alert" style="display: none;">
                                Message has been successfully sent.
                            </div>
                            <div class="alert alert-danger" role="alert" style="display: none;">
                                <div class="alert-text">Server error</div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <button class="btn btn-primary btn-contact" type="submit" value="SEND MESSAGE">SEND MESSAGE</button>
                    </div>
                </div>
            </form>
            <!-- END CONTACT FORM -->

If code does not allow it, are there any modules I can install for this ?

New to node and js. Thanks for the help.

4
  • 1
    You can't. Node runs server side. You can however send a response, for which the client code responds with an alert. Commented Oct 29, 2016 at 17:34
  • 1
    res.send('<script>alert("Hello")</script>') <- tada Commented Oct 29, 2016 at 17:35
  • 1
    @adeneo That's a really bad recommendation. Don't unnecessarily tie the server-side components to the client-side. Commented Oct 29, 2016 at 18:11
  • @Brad - oh, it's just a joke. The OP is doing a regular form submit, so I guess just outputting the appropriate response in the sendMail route is the way to go. Commented Oct 29, 2016 at 18:52

1 Answer 1

4
res.json({success: true});

At the front end, you get response in using promise(JavaScript,jQuery,AngularJs..Whatever) and check for success property for your further businedd logic.

Sign up to request clarification or add additional context in comments.

6 Comments

I was toying/trying that but I couldn't quite get it to work. my index.html page is using a standard button. I will update my question if that helps.
not html will not help.Update Q with JS sending sendMail request and relative part
@RIYAJKHAN No need for the JSON payload. A simple 204 No Content will do. The client should be checking the response status code, and the promise will resolve/reject accordingly.
@RIYAJKHAN updated the questions with the full function. not sure what a promise function is ( I assume it's like a callback ?). A brief example would be appreciated. thanks.
@Brad but API should be written in that way.HTTP Status can do this things.I am answering his requirement.I will prefer your solution.Most robust and stable
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.