...

View Full Version : Using php inside javascript



supersav144
12-29-2010, 05: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;
<?php
print "password='" .$password. "';\n";
?>

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

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

DJCMBear
12-29-2010, 05: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.

supersav144
12-29-2010, 06: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?

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



(function($){
$.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) {
alert(AJAX.responseText);
}
};
AJAX.open("POST",'/ajax.php',true);
AJAX.setRequestHeader("Content-type","application/x-www-form-urlencoded");
AJAX.send();
};
)(window);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum