My php code gets commented out by the browser [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: Code gets turned into HTML <!— Comments? -->
I have some very, very simple php code.
<html>
<head>
<title>Something</title>
</head>
<body>
<h2>Something</h2>
<?php
$val = $_POST['a'];
echo 'Value: ' . $val . '<br />;
?>
</body>
</html>
When I press the submit button in an form I've made, the browser comments out the php code like this:
<!--?php
//the stuff i do above
?-->
Anyone knows what this is?

Are you using CMS?
This looks like Joomla behavour for PHP code when you just insert it in template without using special module.

PHP is a server-side language. It is evaluated on the server, and the results of the evaluation replace the code itself.
This is by design and it would terrible if the PHP source code for every page were included in the HTML comments (eg you can see database passwords, etc.).
You cannot see PHP from the client (i.e. HTML source).

Yes, you're missing a closing ' at the end of your echo statement.

Related

Failing to pass variables via URL to php page [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
Im trying to send 2 variables to my php page via a link, i can't figure out what im doing wrong. The outcome is not right. Im expecting just the display of values and instead the whole php code displays.
Thanks for helping in advance, below is my html, php and result.
My html page is named "index.html" and my php "default.php"
HTML:
<!DOCTYPE html>
<html lang="sv">
<head>
<title>Information sänd via adressfältet och länkar</title>
<meta charset="UTF-8">
</head>
<body>
Link
</body>
</html>
php:
<?php
header("Content-Type: text/plain");
echo $_GET["name"];
echo $_GET["gender"];
?>
Result:
<?php
header("Content-Type: text/plain");
echo $_GET["name"];
echo $_GET["gender"];
?>
There are a couple of factors affecting such
first one confirm that your php module is properly configured to Apache and it reads the .php extension
two that your file is actually saved as .php if you are using a text-editor like notepad which appends the .txt file format at the end, unless you select file type as all files
I found the problem thnx to this thread: PHP code is not being executed, instead code shows on the page
The problem was:
Make sure you are accessing your file over your webserver using an URL like http://localhost/file.php not via local file access file://localhost/www/file.php

PHP seems to run the first line then the rest displays in browser [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 6 years ago.
So I am learning PHP from a book and I understand it is done server side. I installed wampserver and it's running. When I open my php file, it'll run the first line but the rest just displays in the browser. Here's the code:
<!DOCTYPE html>
<html>
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
echo "<p>Order processed at ";
echo date('H:i, jS F Y');
echo "</p>";
?>
</body>
</html>
Here's what displays: Screen Shot
It displays the first 'echo' statement it seems. But the rest... I haven't changed any settings on the wampserver program, I just installed it and made sure all services were started up. Is there something else I need to do?
I'm using Windows 10.
I think PHP doesn't run at all.
Look in your source code in your browser, if you can see <?php your PHP doesn't work.
Create a file "info.php" and write the code: <?php phpinfo(); ?> to test your PHP.

Multiple content across pages (html) [duplicate]

This question already has answers here:
How to create a template in HTML?
(3 answers)
Closed 9 years ago.
I looked around, but couldn't find a satisfying answer.
Problem:
I have a menu bar that appears on the top of the page. I want it to show across all of the pages on the website. So how would someone do that without copying the same code each time. Would someone use html, php, css, or javascript/jQuery to accomplish this?
Note: I want to have a separate html file to access the information from.
From what I have seen, this is typically done with php using a template file.
The template file may have HTML code in it that you want to display on every page, as well as placeholders for content that is page specific. e.g: template.php
<html>
<head>
<title><?php print $title; ?></title>
</head>
<body>
<nav>Test</nav>
<?php print $content; ?>
</body>
</html>
In this case, as long as $title and $content variables are set, you can then do a include 'template.php'; to output this HTML code in other php files.
Read more about php's include.
It seems that you will need to use include, although an explaination on how to use it (or at least an example can be found here: https://www.youtube.com/watch?v=XmoF-6vshSI

Javascript isn't parsing PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse a JavaScript file through PHP
PHP no longer working inside javascript
I am running .php files on my localhost and yesterday they worked and today they have errors, but I haven't changed the code. Here is a test file that has the same error as my longer actual code:
<html>
<script type="text/javascript">
<? $message = "Hello"; ?>
</script>
<? echo $message; ?>
</html>
I know this is a silly way to write Hello, but it demonstrates what error I am getting when I try to use php inside javascript. It was working perfectly fine yesterday and none of the code has changed. When I run this, it says "unexpected token <" on line 3. I think it is a problem with apache/php/mysql but I'm lost trying to fix it. Any suggestions would be great. Thanks.
I guess, you have short tags disabled. You need to enable PHP short tags in order for this to work. Check if this works:
<html>
<script type="text/javascript">
<?php $message = "Hello"; ?>
</script>
<?php echo $message; ?>
</html>
And in my opinion, I have no clue, why you open a <script> tag and declare a PHP Server Side statement there. You know what? It doesn't output anything. Anyways, the output of the above code would be this:
<html>
<script type="text/javascript">
</script>
Hello
</html>
So there's no need of having the <script> tag as it is client side and PHP doesn't care about that. You can just give it anywhere you want, and as long as they are inside <?php ?> tags, they won't be parsed by the browser. :)
If you are doing this in your own system, to enable short tags in PHP, do the following:
Locate the php.ini file.
Find this:
short_open_tag = Off
Change it to:
short_open_tag = On
From the manual:
short_open_tag: Tells PHP whether the short form (<? ?>) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"?>'; ?>. Also, if disabled, you must use the long form of the PHP open tag (<?php ?>).
its not valid javascript.
after the php is run you end up with a dom looking like <script type="text/javascript"></script> because you're not writing anything.
try:
<script>
var hello = "<?php echo 'hello' ?>"
</script>
<script type="text/javascript">
document.writeln(hello)
</script>
The script as it is works fine for me. Is the code posted in your question complete?
The only way I was able to reproduce the problem was with the following code:
<?php
<html>
<script type="text/javascript">
<? $message = "Hello"; ?>
</script>
<? echo $message; ?>
</html>
?>
Correct me if I'm wrong, but I'm assuming the code you posted is in a *.php file and in between php tags.. If this is the case, remove said tags and it should work.

How do HTML comments work with PHP code?

I just wonder how does html comment tag work with php code.
If I have the following code, does php parse it anyway?
<!--
<?php
echo "hi";
?>
-->
a simple question, but I guess it is important to know.
Oh yes, anything in PHP blocks gets executed, but in this case, it isn't shown to the end user because it's in HTML comments.
In the source of the page that is generated by this PHP script, you will see the output surrounded by HTML comments, simple as that.
The only way comments will affect the output of a PHP script is with valid PHP comments.
Yes it does. If that is between <?php ?> tags
You can use PHP comments /* commment */ and they won't execute.
As a side note to the answers above: You can also interrupt the HTML comment:
<!--
<?php
echo "-->This will be seen!<!--";
?>
-->
gives this output:
<!--
-->This will be seen!<!---->

Categories