View Full Version : Using php inside javascript

12-29-2010, 06:56 AM
I am currently creating a page to allow a user to change their password. I have a field which the user will enter their current password into and I am then using javascript to check whether this is the correct password. My passwords are encrypted using md5 and so I need to incorporate this into the javascript function. I have tried but unfortunately cannot get the correct value for the encrypted password.

My javascript code is as follows...

function verifyCurrentPassword(){
//Gain the hashed password that is in the database
var password;
print "password='" .$password. "';\n";

//Find the password that the user has entered as the current password
var hashedpassword;
var currentpassword = document.getElementById("currentpassword").value;
print "hashedpassword='" .md5("currentpassword"). "';\n";

if(password == hashedpassword){ alert("passwords are the same"); }

12-29-2010, 06:58 AM
You will need to use AJAX for this as you can not use Javascript and PHP together, you can use PHP in Javascript but you can't use Javascript in PHP without AJAX, this will also allow you to have better security over the safety of the password and the user will never see anything to do with the password or it's encryption.

12-29-2010, 07:07 AM
Ok thanks, I have never used ajax before so do you have some example code I could possibly use or know of any websites with decent tutorials/examples?

12-29-2010, 07:11 AM
Here's some AJAX code.

$.AJAX = function() {
var xmlhttp;
if(window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
return xmlhttp;
$.checkPassword = function() {
var AJAX = new $.AJAX();
AJAX.onreadystatechange = function() {
if(AJAX.readyState == 4 && AJAX.status == 200) {