lundi 20 avril 2015

MEAN.JS Contact Form

Trying to create a contact form and feedback form for my website. Here is my route and controller I'm using however I need to understand what's going on with my routes and how to capture the input fields from the form implement this inside MEAN.JS:

route.js

app.route('/mail').get(mail.createmail);

app/controller.js

    exports.createmail = function(req, res) {

    var mailOpts, smtpTrans;
    // create reusable transporter object using SMTP transport
    var transporter = nodemailer.createTransport({
        service: 'Gmail',
        auth: {
            user: 'administrator@radleaf.com',
            pass: '34Girls34*goo'
        }
    });

    // NB! No need to recreate the transporter object. You can use
    // the same transporter object for all e-mails

    // setup e-mail data with unicode symbols
    var mailOptions = {
        from: 'Fred Foo ✔ <foo@blurdybloop.com>', // sender address
        to: 'ty@radleaf.com', // list of receivers
        subject: 'Hello ✔', // Subject line
        text: 'Hello world ✔', // plaintext body
        html: '<b>Hello world ✔</b>' // html body
    };

    // send mail with defined transport object
    transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
            console.log(error);
        } else {
            console.log('Message sent: ' + info.response);
        }
    });

 };

Not sure how this work with the html with the view:

<form action="mail">...</form>

Any advantage of jquery .submit() vs jquery .click() if calling .post() in the event?

I have a modal window with address fields in it. I'm trying to understand if I even need a form tag wrapped around my address fields?

When I press the submit/save button, I'm calling .post() to send the address to the controller and if the address is found with Google Maps I update/save the address and send back some data to fill in the map. I also close the modal. If the address isn't found I just show an error message.

Do I really need a ajax.submit here with a form tag wrapped around the fields? What advantage does it provide me for the situtaion I have?

Send datetime field value on submit

I did some research and could not find how to fix this. I have a pdf form with a Date/Time field. On run the field gets the current Date/Time value but when I submit the form the date is not sent to the xml document. How do I get the value in the xml file?

text message disapears after doing javascript validation

I have a form and I'm doing some validation javascript before send it to php, and the javascript function after validation post the text the user entered in a

tag at the bottom of the page; however, this message displays briefly and then disappears... how can I make the message stay in the page, and send the rest of data to a php script

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Contact Us</title>

    <!-- Bootstrap -->
    <link href="bootstrap.min.css" rel="stylesheet">
    <!-- stylesheet for this form -->
    <link href="contact-stylesheet.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="http://ift.tt/1xwklwE"></script>
      <script src="http://ift.tt/1qIredN"></script>
    <![endif]-->
    <script type="text/javascript">
        function validateForm() {
            var message = "";
            var letters = /^[A-Za-z]+$/;        

            var name = document.forms["myForm"]["name"].value;
            var email = document.forms["myForm"]["email"].value;
            var subject = document.forms["myForm"]["subject"].value;
            var text = document.forms["myForm"]["text"].value;
            var outputMsg = "";

            if (name == null || name == "") {               
                message += "name field missing!\n";
            }               
            if (name != "" && !name.match(letters)) {
                message += "Invalid name: only letters allowed!\n";
            }
            if (subject == null || subject == "") {
                message += "Subject field is empty!\n";
            }
            if (text == null || text == "") {
                message += "Text field is empty!\n";
            }           

            if (message != "" ) {
                alert(message);
                return false;
            }

            outputMsg = "Message Sent!....\n" + 
                        "Name: " + name + "\n" +
                        "Email: " + email + "\n" + 
                        "Subject: " + subject + "\n" +
                        "Text: " + text + "\n";

            document.getElementById("msg-result").innerHTML = outputMsg;
            return true;

        }    
    </script>
  </head>
  <body>

      <div class="row">
          <div class="hero-unit" style="padding:20px 100px">
            <h1>Contact Us</h1>
            <p>aldkfjasdkfjaskdfasdfkasdkfjadsfjsdkfjaskfjasdkfjasjfaskdfjsdkfjsksdsdkjsd</p>       
        </div>
          <div class="col-sm-6">
            <div class="my-form">
                <form class="form-horizontal" name="myForm" action="" onsubmit="validateForm()" method="post">
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">Name:</label>
            <div class="col-sm-8">
              <input type="name" name="name" class="form-control" id="inputEmail3" placeholder="Name">
            </div>
          </div>
          <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">Email:</label>
            <div class="col-sm-8">
              <input type="email" name="email" class="form-control" id="inputPassword3" placeholder="Email">
            </div>
          </div>
          <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">Subject:</label>
            <div class="col-sm-8">
              <input type="text" name="subject" class="form-control" placeholder="Subject">
            </div>
          </div>

          <div class="form-group">
              <label for="inputPassword3" class="col-sm-2 control-label">Text:</label>
              <div class="col-sm-8">
                <textarea name="text" class="form-control" rows="7" placeholder="Text"></textarea>
              </div>    
          </div>
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
              <button type="submit" class="btn btn-default">Send</button>
            </div>
          </div>    
            </div> 
        </form>
          </div>
          <div class="col-sm-6">
              <div style="width:500px;heigth:350px;border:solid 1px brown">
                <h1>GOOGLE MAP HERE!</h1>
              </div>
             <!-- <img sytle="padding:0px 20px" src="http://ift.tt/1zD6rJ0">                     -->
          </div>      
      </div>
      <div class="col-sm-6" style="padding:10px 140px">
              <p id="msg-result"></p>
            <!-- display form result message here! -->
      </div>

    <!--
    Welcome <?php echo $_POST["name"]; ?><br>
    Your email address is: <?php echo $_POST["email"]; ?>
    -->
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="http://ift.tt/13qgtmt"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>

  </body>
</html>

Codeigniter contact form email

I'm trying to set up a contact form in Codeigniter (I'm running on xampp using Apache and I have a virtual host set up). I have the actual form working but when I try to link it up so that it emails the results I get an error. I've looked around and tried a few different solutions but I can't figure out what it is that I'm doing wrong.

Controller:

public function contact()
    {
            $this->load->helper('form');
            $this->load->library('form_validation');

            $this->form_validation->set_rules('name', 'your name', 'required', array('required'=>"<p class='required'>Please provide %s</p><br>"));
            $this->form_validation->set_rules('email', 'your email address', 'required', array('required'=>"<p class='required'>Please provide %s</p><br>"));


            if($this->form_validation->run() == FALSE)
            {
                $this->load->view('templates/headder');
                $this->load->view('contact');
                $this->load->view('templates/footer');
            }
            else
            {
                $config = array(
                    'protocol' => 'smtp',
                    'smtp_host' => 'smtp.mydomain.co.uk',
                    'smtp_port' => 465,
                    'smtp_user' => 'example@mydomain.co.uk',
                    'smtp_pass' => 'mypassword',
                    'charset' => 'iso-8859-1',
                    'wordwrap' => TRUE,
                );

                $message = 'This is a test message... do I work?';

                $this->load->library('email');
                $this->email->initialize($config);
                $this->email->from('ecample@example.co.uk', 'Tester');
                $this->email->to('example@mydomain.co.uk');
                $this->email->subject('New Query');
                $this->email->message($message);
                $this->email->send();

                if($this->email->send()){
                $this->load->view('templates/headder');
                $this->load->view('sent');
                $this->load->view('templates/footer');
                }
                else
                {
                $this->load->view('templates/headder');
                $this->load->view('contact');
                $this->load->view('templates/footer');
                }
            }
    }

View:

 <?php echo form_open('home/contact'); ?>
                <h3><span class='required'>*</span>Name:</h3>
                    <label><input type='text' name='name' value="<?php echo set_value('name'); ?>" maxlength="50" ></label>
                    <?php echo form_error('name'); ?>
                <h3>Company:</h3>
                    <label><input type='text' name='company' value="<?php echo set_value('company'); ?>" maxlength="100"></label>
                <h3><span class='required'>*</span>eMail:</h3>
                    <label><input type='email' name='email' value="<?php echo set_value('email'); ?>" maxlength="100"></label>
                    <?php echo form_error('email'); ?>
                <input id='submit_button' type='submit' value='Submit'>

Error Message:

A PHP Error was encountered

Severity: Notice

Message: fwrite(): send of 11 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host.

Filename: libraries/Email.php

Line Number: 2131

Backtrace:

File: C:\xampp\htdocs\root\CI\application\controllers\home.php
Line: 80
Function: send

File: C:\xampp\htdocs\root\public_html\index.php
Line: 292
Function: require_once

Any advice would be much appreciated.

EDIT

I have changed the port as suggested on another post however now I am getting the following message:

A PHP Error was encountered

Severity: Warning

Message: fsockopen(): unable to connect to mail.myemail.co.uk:25 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. )

Filename: libraries/Email.php

Line Number: 1949

Backtrace:

File: C:\xampp\htdocs\root\CI\application\controllers\home.php
Line: 68
Function: send

File: C:\xampp\htdocs\root\public_html\index.php
Line: 292
Function: require_once

I tried changing my email address so that it was incorrect just to make sure I hadn't entered my email details wrong and got a different message to say that the connection had been forcibly closed by my host so I don't believe it's anything that simple either.

EDIT I have set a different email address up and I'm now no longer getting an error however now I'm always getting the "message failed to send" screen and I can't understand why.

EDIT

I have now used print_debugger as suggested in the comments. The result was as followed.

220 BLU436-SMTP37.smtp.hotmail.com Microsoft ESMTP MAIL Service, Version: 8.0.9200.16384 ready at Mon, 20 Apr 2015 11:25:59 -0700

hello: 250-BLU436-SMTP37.smtp.hotmail.com Hello [90.209.233.150]
250-TURN
250-SIZE 41943040
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250-TLS
250-STARTTLS
250 OK

Failed to send AUTH LOGIN command. Error: 530 5.7.0 Must issue a STARTTLS command first
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

User-Agent: CodeIgniter
Date: Mon, 20 Apr 2015 20:25:58 +0200
From: "Tester" <myemail@mydomain.co.uk>
Return-Path: <myemail@mydomain.co.uk>
To: myemail@mydomain.co.uk
Subject: =?SIO-8859-1?Q?=4E=65=77=20=51=75=65=72=79?=
Reply-To: "myemail@mydomain.co.uk" <myemail@mydomain.co.uk>
X-Sender: myemail@mydomain.co.uk
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <553544b61c447@mydomain.co.uk>
Mime-Version: 1.0


Content-Type: multipart/alternative; boundary="B_ALT_553544b61c451"

This is a multi-part message in MIME format.
Your email application may not support this format.

--B_ALT_553544b61c451
Content-Type: text/plain; charset=SIO-8859-1
Content-Transfer-Encoding: 8bit

this is a test


--B_ALT_553544b61c451
Content-Type: text/html; charset=SIO-8859-1
Content-Transfer-Encoding: quoted-printable

this is a test

--B_ALT_553544b61c451--

I'm reading this as a MIME problem, is this correct?

Input Total Product Price with GST Function

I would like the sub total value to be toFixed(2), so that the sub-total input will display "864.91" instead of "864.9100000000001".

Also, I have 2 inputs below sub-total called GST and total_price.

Basically, GST should take the sub-total value and multiply it by (5/100) and display it in the GST input value.

The total price should take the GST value and the sub-total value, add them together, and display the number in the total_price input value.

  1. Any ideas on what code I can use to do this?
  2. I would also like all values to be no longer than 2 decimal places like the sub-total input value (864.91 instead of 864.9100000...).

Here is the code I have so far:

function selectProduct(sel) {
    var end = sel.value;
    var typeId = sel.id;
        $.ajax({    //create an ajax request to load_page.php
            type: "GET",
            url: "ajax_all.php?from=pos_product&name="+end,
            dataType: "html",   //expect html to be returned
            success: function(response){
                var sub_total = $("#sub-total").val();
                $("#sub-total").val(+sub_total + +response);

            }
    });
}

Thank you for any help, all help is greatly appreciated.

Pass all option values in form

Within a form, I have a select element with id="keywords". This element has size="6", but no option elements in the HTML file. A JavaScript function that executes when the page loads populates the list as follows:

function setKeywords() {
    for (i = 0; i <= 5; i = i + 1) {
        var option;
        option = document.createElement("option");
        option.name = "keyword" + (i + 1).toString();
        option.value = "null";
        option.text = "";
        document.getElementById("keywords").add(option);
    }
}

Users then can add and remove up to six values to the list via other JavaScript functions (the code of which isn't useful for the purpose of this question), and then I wish to pass the values of these six option elements (among other inputted values in the form) to another page via POST. I thought that since I named these elements keyword1, keyword2, etc., that these values would then pass with the rest of the form. The other inputted values in the form are being passed successfully, but that's not happening with these keywords.

What must I change in order to pass the values of these option elements with the rest of the form?

EDIT 1: This is the HTML code that creates the select element in question.

<select id="keywords" size="6"></select>

EDIT 2: These are the JavaScript functions used to add and remove options to/from the select element. keywordslist is another select element in the form with keywords the user may add to the keywords element.

var keywordsArea = document.getElementById("keywords");
var keywordslist = document.getElementById("keywordslist");
function addKeyword() {
    if (keywordsArea.options[5].value === "null") {
        for (i = 0; i <= 5; i = i + 1) {
            if (keywordsArea.options[i].value === "null") {
                keywordsArea.options[i].value = keywordslist.value;
                keywordsArea.options[i].text = keywordslist.value;
                break;
            }
        }
    }
}
function removeKeyword() {
    if (keywordsArea.selectedIndex >= 0 && keywordsArea.selectedIndex <= 5) {
        for (i = keywordsArea.selectedIndex; i <= 5; i = i + 1) {
            if (i === 5) {
                keywordsArea.options[i].value = "null";
                keywordsArea.options[i].text = "";
            } else {
                keywordsArea.options[i].value = keywordsArea.options[i + 1].value;
                keywordsArea.options[i].text = keywordsArea.options[i + 1].text;
            }
        }
    }
}