Well i got to encrypt certain form text with a key. i have used a button to encrypt and another to decrypt. The problem is how to assign a javascript variable to a php variable. The code is as follows:

function encryption()
{
document.encryptfrm.decryptTxt.value="";
var keyenc = document.encryptfrm.key.value;
var enc = document.encryptfrm.encryptTxt.value;

if(keyenc.length == 0)
{ 
	alert('Please enter value for key');
	document.encryptfrm.key.focus();

} 

if(enc.length > 0)
{
	<?php 
	$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
	$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$key ?> =keyenc;
	<?php $text?> = enc;
	<?php $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);?> 
	document.encryptfrm.decryptTxt.value=<?php echo $crypttext;?>;
	document.encryptfrm.encryptTxt.value="";
}
else
{
	alert('Please enter the text to be encrypted');
	document.encryptfrm.encryptTxt.focus();
}

return true;
}

    Not sure what you want to do, but you cannot directly address a PHP variable from within JavaScript. Remember that the sequence of events is:

    1. PHP script runs on the server, generating HTML output (including JavaScript code).

    2. Web server sends the output from the PHP script to the client (web browser).

    3. The client executes the JavaScript code within the HTML page, totally ignorant of the fact that PHP has anything to do with it.

    The only common way for JavaScript to communicate with PHP or any other server-side language while the client code is executing is via an AJAX interface, sending HTTP requests to a server-side PHP script, then waiting for a response from that script.

      Write a Reply...