>> PHP: JS Integration & s
function validate() { } Inserting / Uploading Items to DB Web-Based Systems - Misbhauddin <?php ?> type=“submit” 2 1
1. Using the submit() in JS Steps – After successful validation in JavaScript, call the submit function Web-Based Systems - Misbhauddin 3 document.form-name.submit();
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. Web-Based Systems - Misbhauddin 4
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 Web-Based Systems - Misbhauddin 5 $(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
S IN PHP PHPMailer
Using a Third-Party Library PHPMailer – Probably the world's most popular code for sending from PHP! – Integrated SMTP support Download – Web-Based Systems - Misbhauddin 7
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 Web-Based Systems - Misbhauddin 8
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($ ); Web-Based Systems - Misbhauddin 9
Using PHPMailer – To/From $mail->From = $mail->FromName = 'Mailer'; Add a recipient - Name is optional 'Josh Adams'); 'Information'); Web-Based Systems - Misbhauddin 10
Using PHPMailer – Subject & Body $mail->Subject = 'Here is the subject'; $mail->Body = $message; If message is HTML $mail->IsHTML(true); // Set 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'); Web-Based Systems - Misbhauddin 11
Using PHPMailer – Confirmation if(!$mail->Send()) { echo 'Message could not be sent.'; echo 'Mailer Error: '. $mail->ErrorInfo; exit; } echo 'Message has been sent'; Web-Based Systems - Misbhauddin 12
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', // 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; Web-Based Systems - Misbhauddin 13
Web-Based Systems - Misbhauddin 14 Issue with using Gmail Address