Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-29-2010, 05:56 AM   PM User | #1
supersav144
New Coder

 
Join Date: Dec 2010
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
supersav144 is an unknown quantity at this point
Using php inside javascript

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...

Code:
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"); }
}
supersav144 is offline   Reply With Quote
Old 12-29-2010, 05:58 AM   PM User | #2
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
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.
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 12-29-2010, 06:07 AM   PM User | #3
supersav144
New Coder

 
Join Date: Dec 2010
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
supersav144 is an unknown quantity at this point
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?
supersav144 is offline   Reply With Quote
Old 12-29-2010, 06:11 AM   PM User | #4
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Here's some AJAX code.

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);
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, md5, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:45 PM.


Advertisement
Log in to turn off these ads.