0

I am trying to get a PHP variable to pass values to a sample javascript but nothing shows up if I tried to echo the result.

I tried passing regular strings as arguments and those worked fine. It just doesn't seem to be working if I tried to pass PHP variables as arguments.

AES (Rijndael) Encryption Test in JavaScript

<script type="text/javascript" src="Decrypt.js"></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 * from employee_details where id = '$userId'");
            if($row=mysql_fetch_assoc($query))
                {
                    echo '<tr>';
                    foreach($row as $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 FROM $tbl_name WHERE id = ".$condition);

                 return @mysql_result($result, 0);
            }

                $myValue = getField($columname,$tablename,$userId);

                echo "Ciphertext = $myValue";
                echo "<br>";
                //doDecryption();

        }
        echo '<script type="text/javascript">
doDecryption("<?php $myValue; ?>");
</script>';
echo "whats happening";
?>
</body>
</html>

The JS file

function doDecryption(param)
 {
    document.write(param);
    document.write("Programming change");
 }

Thanks in advance. Any help much appreciated!!!!

4
  • Upvotes for whoever actually explains why this isn't working rather than offering code examples. There are two types of PHP strings and the semantics of <?php. A whole 10 points up for grabs! Commented Dec 3, 2010 at 8:14
  • possible duplicate of Calling javascript function with php code Commented Dec 3, 2010 at 8:14
  • 1
    The reason it's not woking is because he forgot the call to "echo". Commented Dec 3, 2010 at 8:45
  • 1
    Your problem is nested <?php ?> tags. you can't use php tags inside the echo. Commented Dec 3, 2010 at 9:23

6 Answers 6

3

You do not need the echo statement to build the javascript-part, just try this:

<script type="text/javascript">
  doDecryption("<?php echo $myValue; ?>");
</script>

But if you do not want to change it and you want to write out the -Block via echo, you should not use the "

echo '<script type="text/javascript"> doDecryption(' . $myValue . '); </script>';
Sign up to request clarification or add additional context in comments.

1 Comment

Can you post the generated JavaScript?
1

I think you should try to echo your var:

doDecryption("<?php echo $myValue; ?>");

Comments

0

I don't have access to PHP atm to check, but I think its because you echo uses single quotes which won't evaluate the variable. Use double quotes for the echo and escape your " inside the string and see what happens

Comments

0

yes its better to "echo" your variable.

Comments

0

or change your echo to this

echo '<script type="text/javascript">
doDecryption("'.$myValue.'");
</script>';

Comments

-1

Try this,

<script type="text/javascript" src="Decrypt.js"></script>
<?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 * from employee_details where id = '$userId'");
            if($row=mysql_fetch_assoc($query))
                {
                    echo '<tr>';
                    foreach($row as $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 FROM $tbl_name WHERE id = ".$condition);

                 return @mysql_result($result, 0);
            }

                $myValue = getField($columname,$tablename,$userId);

                echo "Ciphertext = $myValue";
                echo "<br>";
                //doDecryption();

        } ?>
<script type="text/javascript">
doDecryption("<?php $myValue; ?>");
</script>

</head>

<body>
<?php
echo "whats happening";
?>
</body>
</html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.