kieron oates
kieron oates

Reputation: 115

How to take javascript from one page and use it in php on another page

So I am making a form which allows the user to input their name, email and comment and i want to take them and put it into a php mail form on another page. i have tested both seperately and both work but i want to have the Name, Email and Comment show up in the php form. can anyone help?

This is the mail system

        require 'PHPMailer/class.phpmailer.php';
        require 'PHPMailer/class.smtp.php';
        require 'PHPMailer/PHPMailerAutoload.php';

        $mail = new PHPMailer;

        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = '[email protected]';                 // SMTP username
        $mail->Password = 'testexample';                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                                    // TCP port to connect to

        $mail->setFrom('//I want their email to go here which is entered on the form', 'Test');
        $mail->addAddress('[email protected]', '//and their name here');     // Add a recipient
        //$mail->addAddress('[email protected]');               // Name is optional
        //$mail->addReplyTo('[email protected]', 'Information');
        //$mail->addCC('[email protected]');
        //$mail->addBCC('[email protected]');
        //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
        //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
        $mail->isHTML(true);                                  // Set email format to HTML

        $mail->Subject = 'Here is the subject';
        $mail->Body = '//and i would like the comment to go here';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';


    if (!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }

And this is my input form (uses bootstrap)

<div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3 emailForm">
                <form method="post">
                    <div class="form-group"
                         <label for="name">Your Name:</label>
                        <input type="text" name="userName" class="form-control" placeholder="Your Name"/>
                    </div>
                    <div class="form-group"
                         <label for="email">Your Email:</label>
                        <input type="email" name="userEmail" class="form-control" placeholder="Your Name"/>
                    </div>
                    <div class="form-group"
                         <label for="comment">Your Comment:</label>
                        <textarea class="form-control" name="userComment"></textarea>
                    </div>
                    <input type="submit" class="btn btn-success btn-lg" value="Submit">
                </form>
            </div>
        </div>
    </div>
    <script>
    var userName = document.getElementById('userName').value;
    var userEmail = document.getElementById('userEmail').value;
    var userComment = document.getElementById('userComment').value;
    </script>

(Sorry in advance for any messy code) I just want to take the "var userEmail, userName and userComment" and put them into the php email sender on another page

Upvotes: 0

Views: 42

Answers (1)

Rahul
Rahul

Reputation: 2474

Change <form method="post"> to <form method="post" action="your_mail_file.php" method="POST"> and then in your mail system file use:

$userEmail = $_POST['userEmail'];

$userName = $_POST['userName'];

$userComment = $_POST['userComment'];

Upvotes: 1

Related Questions