-3

I want to add script code in php but it giving error on this line

$a=echo "document.getElementById('t1').value;";

of unexpected use of ECHO! Can any one help???

<?php
    echo "<script>";
    $a=echo "document.getElementById('t1').value;";


    if($_session['user']==$a) {
        echo 'function fun() {
            document.write("welcom");
        }';
    }

    echo "</script>";
?>
Albzi
  • 15,431
  • 6
  • 46
  • 63
  • just get rid of the `$a=` – RightClick May 07 '15 at 15:18
  • also, echo isn't the only way to write output. You can stop php with `?>` and write out html/css/script and then start php again with ` – RightClick May 07 '15 at 15:20
  • Check these answers - http://stackoverflow.com/questions/10596218/how-to-write-javascript-code-inside-php http://stackoverflow.com/questions/8471945/how-can-you-use-php-in-a-javascript-function These will help you – Neha Narlawar May 07 '15 at 15:22
  • 1
    Syntax problems aside, I think you're trying to mix up code that runs on the server and code that runs on the client. Your PHP code can generate and return some Javascript to the browser, but it can't interact with javascript in the way you appear to be attempting. – Paul Dixon May 07 '15 at 15:25
  • Any values that you want sent from Javascript (website user's computer) to PHP (web server) _must_ be done by a HTTP request. Either post a form, use a querystring or use ajax. – James May 07 '15 at 15:29

2 Answers2

0

Your problem is from $a=echo because echo is a php function.

The correct syntax is:

<?php
$a= "document.getElementById('t1').value;";
echo "<script>";
echo $a;
if($_session['user']==$a) {
    echo 'document.write("welcom");';
}
echo "</script>";
?>
Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60
-1

You cant use echo function in variable. You can echo variable only and there are 2 ways, how to achieve this:

1) you can save the content into variable via:

$a = "document.getElementById('t1').value;"; 

and somewhere in your code where you will need it you can echo the content of $a variable via: echo $a;

2) you can echo the content of your variable in the concrete line:

echo "document.getElementById('t1').value;";
pes502
  • 1,597
  • 3
  • 17
  • 32
  • 1
    `if($_session['user']=="document.getElementById('t1').value;")` doesn't make any sense. The OP is trying to compare the *return value* of a JavaScript expression with a PHP string. – Quentin May 07 '15 at 15:25
  • but he asked, why he has got unexpected use of ECHO, so I write him this explanation for his question – pes502 May 07 '15 at 15:27
  • You're focusing too much on the literal question title and ignoring the underlying problem that is demonstrated by the code example provided in it. – Quentin May 07 '15 at 15:29
  • The main problem in this question is, that he trying pass the JS variable into PHP I think. But now he asked, why the posted code showing him error, this is explained in my answer. Now, when he will fix the error, he will find, that the code not workin and can create a new question with title like: "I am trying to pass JS variable into my PHP code and its not workin" .... – pes502 May 07 '15 at 15:34