Mohammad Umer
Mohammad Umer

Reputation: 534

Store JavaScript Result In Php variable

I have Some Passwords which contain some special and methimatical chracters like this password "HHE1134ƒ" if i want to generate password with php md5() then this not give me correct hashes like this "679b6dc6122a9c83ed31476ee82af36e".

So i have Java Script which can generate correct hash for my passwords But how can i store this value to mysql

my code is given below please Help Me.

$result = mysql_query("SELECT * FROM `table`") or die(mysql_error());

while ($row = mysql_fetch_assoc($result))
{

$user_id = $row['user_id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$spno = $row['spno'];
$password = $row['password'];

$tmp =  "<script type='text/javascript'> document.write(MD5('".$password."'));</script>";

$query1="UPDATE `wp_110504users` SET `user_pass` = '$tmp' WHERE `user_login` = '$user_id'";

mysql_query($query1) or die(mysql_error());
}

this will give me error like

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'text/javascript'> document.write(MD5('HW72'));' WHERE `user_login` = '0' at line 1

?>

Some one tell me use Ajax, Please tell me how can i use Ajax for this code can you give me an example .....

Upvotes: 0

Views: 1920

Answers (7)

Paul
Paul

Reputation: 141829

Javascript doesn't run until it reaches the user's browser. This is called client side.

PHP is server side. All your PHP code executes before the web page is delivered to the user. Then your Javascript is executed on their own computer, by their web browser.

The only way to send something from Javascript to the server is using Ajax. You could look into it, but I don't think you should at your current experience level with the two languages. How long have you been using them for?

You can just use the md5() function which is built right into php btw: http://php.net/manual/en/function.md5.php

Upvotes: 2

WooCaSh
WooCaSh

Reputation: 5212

Try this:

$tmp = md5($password);
$query1="UPDATE `wp_110504users` SET `user_pass` = '$tmp' WHERE `user_login` = '$user_id'";

Also u can use MySQL function:

$query1="UPDATE `wp_110504users` SET `user_pass` = MD5('$tmp') WHERE `user_login` = '$user_id'";

Upvotes: 0

Alex Ackerman
Alex Ackerman

Reputation: 1341

Use php native md5 function:

md5($password);

Upvotes: 1

Timur
Timur

Reputation: 6718

I think you copy-pasted code not correctly. According to error, you trying to put $userpass variable in SQL statement, but, in code, you are using $tmp variable, which is not defined at all.

Upvotes: 0

Aif
Aif

Reputation: 11220

This is very normal, you userpass value will just be <script type.... Javascript is a client side scripting language, whereas php runs server side, how would you want your md5 to be interpreted server-side?

What you can do, is hash the password before sending it, but it would make you trust the visitor, what you should never do, or simply use php function for md5 hashing.

Upvotes: 0

Tobias
Tobias

Reputation: 7380

PHP is interpreted before javascript reaches the browser. You cannot do this that way. Read more about AJAX, this will help you.

BTW: why are you generating md5 hashes with javascript?

Upvotes: 0

Dan Grossman
Dan Grossman

Reputation: 52372

You cannot run JavaScript code inside your PHP code on the server. The PHP interpreter only interprets PHP code.

You don't need to, both MySQL and PHP have md5 functions to compute those hashes as well.

Upvotes: 3

Related Questions