>> php: js integration & emails. function validate() { } inserting / uploading items to db...

14
>> PHP: JS Integration & Emails

Upload: cecil-robbins

Post on 19-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: >> PHP: JS Integration & Emails. function validate() { } Inserting / Uploading Items to DB Web-Based Systems - Misbhauddin  type=“submit” 2 1

>> PHP: JS Integration & Emails

Page 2: >> PHP: JS Integration & Emails. function validate() { } Inserting / Uploading Items to DB Web-Based Systems - Misbhauddin  type=“submit” 2 1

<script> function validate() {

}</script>

<input type=“button” onclick=“return validate();” value=“ADD”/>

Inserting / Uploading Items to DB

Web-Based Systems - Misbhauddin

<form method=“POST” action=“add.php” name=“add”>

<?php

?>

type=“submit”

2

1

Page 3: >> PHP: JS Integration & Emails. function validate() { } Inserting / Uploading Items to DB Web-Based Systems - Misbhauddin  type=“submit” 2 1

Web-Based Systems - Misbhauddin 3

1. Using the submit() in JS

• Steps– After successful validation in JavaScript, call the

submit function

document.form-name.submit();

Page 4: >> PHP: JS Integration & Emails. function validate() { } Inserting / Uploading Items to DB Web-Based Systems - Misbhauddin  type=“submit” 2 1

Web-Based Systems - Misbhauddin 4

2. Using the onsubmit() attribute in form

• Steps– Include the onsubmit attribute in the form tag

– In javaScript validation, return 'true' if everything's fine, or 'false' if not. • Returning 'true' means continue with the submit

process and send data over to the php code.

<form method=“POST” action=“add.php” onsubmit=“return validate();” name=“add”>

Page 5: >> PHP: JS Integration & Emails. function validate() { } Inserting / Uploading Items to DB Web-Based Systems - Misbhauddin  type=“submit” 2 1

Web-Based Systems - Misbhauddin 5

3. Using Ajax• AJAX = Asynchronous JavaScript and XML.

– Allows web pages to be updated asynchronous– Possible to update parts of a web page, without reloading the whole

page$(document).ready(function(){ $("#login_form").submit(function() { $.post("login_verify.php",{userid:$('#userid').val(),password:$('#password').val()} ,function(data) {

if(data=='yes') //if correct login detail {

document.location='clientprofile.php'; } else

{ }

); });});

Sample

Page 6: >> PHP: JS Integration & Emails. function validate() { } Inserting / Uploading Items to DB Web-Based Systems - Misbhauddin  type=“submit” 2 1

EMAILS IN PHPPHPMailer

Page 7: >> PHP: JS Integration & Emails. function validate() { } Inserting / Uploading Items to DB Web-Based Systems - Misbhauddin  type=“submit” 2 1

Web-Based Systems - Misbhauddin 7

Using a Third-Party Library

• PHPMailer– Probably the world's most popular code for

sending email from PHP!– Integrated SMTP support

• Download– https://github.com/Synchro/PHPMailer

Page 8: >> PHP: JS Integration & Emails. function validate() { } Inserting / Uploading Items to DB Web-Based Systems - Misbhauddin  type=“submit” 2 1

Web-Based Systems - Misbhauddin 8

Including PHPMailer

• Use the require_once() command– include() – [suppresses warnings if file does not

exists]– require() – [warns if file does not exist]– require_once() – [similar to require but loads file

once]

• The file of concern– class.phpmailer.php– class.smtp.php

Page 9: >> PHP: JS Integration & Emails. function validate() { } Inserting / Uploading Items to DB Web-Based Systems - Misbhauddin  type=“submit” 2 1

Web-Based Systems - Misbhauddin 9

Using PHPMailer

• Prerequisite Knowledge– PHPMailer uses Object-Oriented in PHP

• Steps– 1. Create a new Object • $mail = new PHPMailer();

– 2. Access function for the newly created object using ‘->’• For example: $mail->ValidateAddress($email);

Page 10: >> PHP: JS Integration & Emails. function validate() { } Inserting / Uploading Items to DB Web-Based Systems - Misbhauddin  type=“submit” 2 1

Web-Based Systems - Misbhauddin 10

Using PHPMailer – To/From

• $mail->From = '[email protected]'; • $mail->FromName = 'Mailer';

Add a recipient - Name is optional• $mail->AddAddress('[email protected]', 'Josh Adams'); • $mail->AddReplyTo('[email protected]', 'Information');• $mail->AddCC('[email protected]'); • $mail->AddBCC('[email protected]');

Page 11: >> PHP: JS Integration & Emails. function validate() { } Inserting / Uploading Items to DB Web-Based Systems - Misbhauddin  type=“submit” 2 1

Web-Based Systems - Misbhauddin 11

Using PHPMailer – Subject & Body

• $mail->Subject = 'Here is the subject'; • $mail->Body = $message;

If message is HTML• $mail->IsHTML(true); // Set email format to HTML • $mail->MsgHTML($message);• $mail->AltBody = 'This is the body in plain text for non-HTML

mail clients';

Adding Attachments• $mail->AddAttachment('/var/tmp/vcard.info');

Page 12: >> PHP: JS Integration & Emails. function validate() { } Inserting / Uploading Items to DB Web-Based Systems - Misbhauddin  type=“submit” 2 1

Web-Based Systems - Misbhauddin 12

Using PHPMailer – Confirmation

if(!$mail->Send()) {

echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; exit;

} echo 'Message has been sent';

Page 13: >> PHP: JS Integration & Emails. function validate() { } Inserting / Uploading Items to DB Web-Based Systems - Misbhauddin  type=“submit” 2 1

Web-Based Systems - Misbhauddin 13

SMTP Server• PHPMailer has its own SMTP Server which may not work on

localhost but will work once deployed on a server• Other options

– Use a custom SMTP Server (Gmail)– Steps:

• Define Your Username & Passowrd– define('GUSER', '[email protected]'); // GMail username– define('GPWD', 'password'); // GMail password

• Functions– $mail->IsSMTP(); // enable SMTP – $mail->SMTPAuth = true; // authentication enabled – $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail – $mail->Host = 'smtp.gmail.com'; – $mail->Port = 465; – $mail->Username = GUSER; – $mail->Password = GPWD;

Page 14: >> PHP: JS Integration & Emails. function validate() { } Inserting / Uploading Items to DB Web-Based Systems - Misbhauddin  type=“submit” 2 1

Web-Based Systems - Misbhauddin 14

Issue with using Gmail Address