Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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"); }
    }

  • #2
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •