Calling javascript function with php code -


i trying call javascript function declared @ top in php area. not working. can tell me reason it. else working except part. please me.

  <!doctype html public "-//w3c//dtd html 4.01 transitional//en">   <html>   <head>   <title>aes (rijndael) encryption test in javascript</title>   <script src="aes-enc.js" type="text/javascript" language="javascript"></script>   <script src="aes-dec.js" type="text/javascript" language="javascript"></script>   <script src="aes-test.js" type="text/javascript" language="javascript"></script>   <script type="text/javascript">     function dodecryption()             {             document.write("inside javascript");             var ct, key;    ct = hex2s(<?php echo $myvalue; ?>);   document.write("inside javascript");   document.write(ct);  // key = hex2s(theform.key.value);  // theform.plaintext.value = bytearraytohex(rijndaeldecrypt(ct, key, "ecb"));               }     </script>   </head>    <body>   <?php   mysql_connect("localhost","root","");   mysql_select_db("encryption") or die(mysql_error());   $userid = $_post['userid'];     if (($_server['request_method'] == 'post') && ($_post['key'] == ""))   {      $query = mysql_query("select * employee_details id = '$userid'");                       if($row=mysql_fetch_assoc($query))                 {                     echo '<tr>';                     foreach($row $value)                     echo '<td>'.$value.'</td>';                     echo '</tr>';                 }              else { echo "no rows returned"; }}     else if (($_server['request_method'] == 'post') && ($_post['key']))         {             $columname = "ciphertext";            $tablename = "employee_details";                    function getfield($field, $tbl_name, $condition)             {                  $result = mysql_query("select $field $tbl_name id =  ".$condition);                   return @mysql_result($result, 0);             }                  $myvalue = getfield($columname,$tablename,$userid);                  echo "$myvalue";                 [b]echo '<script type="text/javascript">                     dodecryption();                     </script>';[/b]                 echo "whats happening";                 //dodecryption();          }     ?>   </body>   </html> 

$myvalue doesn't have value when try use in js.

the php runs on server, outputs html document embedded javascript, document sent client , javascript runs.

if don't know value javascript variable needs have until php gets end of document, can't generate part of js until then. want write argument function call instead.

once have problem — if data string, needs quoted (and matching quote marks inside need escaped).

in nutshell: php outputs text might processed js, cannot call javascript functions (unless start mixing extensions can talk rhino/spidermonkey/etc on server).

all said, in case, there doesn't seem reason use javascript in first place , better off moving logic php.

incidentally, choice of doctype trigger quirks mode in browsers. highly undesirable.

a better choice be:

<!doctype html public "-//w3c//dtd html 4.01//en"    "http://www.w3.org/tr/html4/strict.dtd"> 

or if want transitional:

<!doctype html public "-//w3c//dtd html 4.01 transitional//en"    "http://www.w3.org/tr/html4/loose.dtd"> 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -