0

Im still new in PHP, HTML or Javascipt. Im trying to find solution about this problem.

I got this error

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING when trying to show <?php echo $_GET["q"]; ?> in echo.

Original Code

<input type="hidden" name="q" value="<?php echo $_GET["q"]; ?>" /><input type="submit" name="ResetPasswordForm" value=" Reset Password " />

when change to echo

echo "<input type=\"hidden\" name=\"q\" value=\"<?php echo $_GET[\"q\"]; ?>\" /><input type=\"submit\" name=\"ResetPasswordForm\" value=\" Reset Password \" />\n";

Can someone help me or explain why? Thank you..

3 Answers 3

1

You can't have an echo statement inside an echo statement.

value=\"<?php echo $_GET[\"q\"]; ?>\"

should be

value=\"$_GET["q"]\"

or

value=\"" . $_GET["q"] . "\"
Sign up to request clarification or add additional context in comments.

2 Comments

You should also take care of escaping the user input, as this might lead to XSS issues.
@TobiasZander That goes without saying. I just addressed their main issue.
0

Change it to:

echo "<input type=\"hidden\" name=\"q\" value=\"" . $_GET['q'] . "\" /><input type=\"submit\" name=\"ResetPasswordForm\" value=\" Reset Password \" />\n";

Comments

0

The other answers below are right about your problem but you maybe need to get your head round how PHP used this way (aka Dynamic HTML) works. When the web server picks up the basic script, all the PHP is interpreted and executed. The resulting output is passed to the client(browser) for rendering. So if you are doing an echo to output a bit of html such as an input element, like your example, you don't need further PHP tags but there are different ways to skin a cat.

You could echo out all your html (icluding Javascript) line by line OR you can jump in and out of PHP - like you were kind of doing with the value attribute:

i.e:

echo "<input type=\"hidden\" name=\"q\" value=\"" . $_GET['q'] . "\" /><input type=\"submit\" name=\"ResetPasswordForm\" value=\" Reset Password \" />\n";

OR

<?php
:
...some PHP stuff ...
:
//then pop out of PHP to do your html raw (maybe with a little bit of PHP mixed in)
?>
<input type="hidden" name="q" value="<?php echo $_GET['q']?>" />
<input type="submit" name="ResetPasswordForm" value=" Reset Password " />
<?php
:
.... some more PHP stuff....
:
?> 

How you mix it up depends on what is easiest for maintenance etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.