0
<html>
<head>
<?php
$your_name=$_POST['name'];
?>
<script language="javascript">
function fash(at1,at2)
{
    alert(at1+at2);
}
</script>
</head>
<body>
<?php
echo $your_name;
echo '<script language="javascript">fash("the key is: "+'<?php echo $your_name; ?>');</script>';
?>
</body>
</html>

The output is:

the key is : undefined

How can I fix this?

jcolebrand
  • 15,889
  • 12
  • 75
  • 121
NamoBhagavan
  • 35
  • 1
  • 5

6 Answers6

4

This can't be the right code, it has syntax errors (e.g. you have a <?php block inside another <?php block). Probably the line that outputs a script tag is supposed to look like this:

echo "<script language=\"javascript\">fash(\"the key is: $your_name\");</script>";

Or like this:

?>
<script language="javascript">fash("the key is: <?php echo $your_name; ?>");</script>
<?php

That will get it to output "the key is: testnameundefined" (if $your_name is testname). The undefined comes from you defining fash to take two arguments and concatenate them, but you only passing one; at2 is undefined

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
  • This will break if $your_name contains a doublequote. Best to do something like 'var somevar = ;` beforehand and use somevar instead. – Marc B Jan 20 '11 at 01:32
  • @Marc Good point; some sort of sanitation is always a good idea, just outputting form variables directly won't end well – Michael Mrozek Jan 20 '11 at 02:04
1

It's hard to see what´s going on with all the editing, but it seems you are using <?php ?> tags when you are already inside php tags.

Just echoing and removing the inner <?php ?> should solve part of your problem, although it seems that your javascript function wants 2 paramenters as well instead of one (the text given).

jeroen
  • 91,079
  • 21
  • 114
  • 132
1

You're passing only one parameter into your function:

fash("the key is: "+'php_value');

"the key is: " and your php string are concatenated before the function is called.

Try

fash("the key is: ", 'php_value');

instead.

Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
0

The function fash takes two parameters. You are only passing one.

Use:

fash("the key is: ", "$your_name");

This will correctly display the alert after it concatenates these two strings. You do not need the PHP tags here because you are already in a PHP code block.

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
0

Because in your definition of fash, you take two parameters and concatenate them there, but in your echoed script, you concatenate them there and so only pass one parameter. So at2 is passed as undefined. Clearly your name is also blank.

Tesserex
  • 17,166
  • 5
  • 66
  • 106
0
echo '<script language="javascript">fash("the key is: "+'<?php echo $your_name; ?>');</script>';

It looks like you're echo'ing php code inside a php echo. Try changing it to:

echo '<script language="javascript">fash("the key is: "+'$your_name;');</script>';

Not sure if that solves the entire problem but I'm pretty sure you can't echo a new echo inside an echo.

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175