Php in HTML doesn't work - php

Look, i have html with included php link but that doesn't work well.Information what was in html doesn't come to php page. Please help with that.
HTML CODE:
<!DOCTYPE html PUBLIC "-//W3C//DTD
➝ XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/
➝ DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/
➝ 1999/xhtml" xml:lang="en"
➝ lang="en">
<head>
<meta http-equiv="Content-Type"
➝ content="text/html;
➝ charset=utf-8"/>
<title>Greetings!</title>
</head>
<body>
<!-- Script 3.6 - hello.html -->
<div><p>Click a link to say
➝ hello:</p>
<ul>
<li><a href="hello.php?
name=Michael">Michael</a></li>
<li><a href="hello.php?
name=Celia">Celia</a></li>
<li><a href="hello.php?
name=Jude">Jude</a></li>
<li><a href="hello.php?
name=Sophie">Sophie</a></li>
</ul>
</div>
</body>
</html>
PHP code:
<!DOCTYPE html PUBLIC "-//W3C//DTD
➝ XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/
➝ DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/
➝ 1999/xhtml" xml:lang="en"
➝ lang="en">
<head>
<meta http-equiv="Content-Type"
➝ content="text/html;
➝ charset=utf-8"/>
<title>Greetings!</title>
</head>
<body>
<?php // Script 3.7 - hello.php
error_reporting (E_ALL | E_STRICT);
$name - $_GET['name'];
print "<p>Hello, <span
style=\font-weight:
bold;\">$name</span>!</p>";
?>
</body>
</html>
And this how it's look on web
http://santa-monica.comli.com/hello.html
http://santa-monica.comli.com/hello.php?name=Michael

change your php code to this -
<!DOCTYPE html PUBLIC "-//W3C//DTD
➝ XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/
➝ DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/
➝ 1999/xhtml" xml:lang="en"
➝ lang="en">
<head>
<meta http-equiv="Content-Type"
➝ content="text/html;
➝ charset=utf-8"/>
<title>Greetings!</title>
</head>
<body>
<?php // Script 3.7 - hello.php
error_reporting (E_ALL | E_STRICT);
$name = $_GET['name'];
print '<p>Hello, <span style="font-weight:bold;">'.$name.'</span>!</p>';
?>
</body>
</html>

your link to the php page is working fine, your problem is where u define the name variable, you have a typo, instead of $name = $_GET['name']; you put $name - $_GET['name'];, dont worry this happens to the best of us, fix that and it should run smoothly, btw if you are testing your php code on an online server dont, because it is slower than on your pc, unless you are contacting an api, it doesnt work unless its executing online
EDIT :
change your code to this :
<?php // Script 3.7 - hello.php
error_reporting (E_ALL | E_STRICT);
$name = $_GET['name'];
?>
<p>Hello, <span
style=\font-weight:
bold;\"><?php echo $name; ?></span>!</p>

Related

pass variable via GET to decide if UTF-8 will be used

So the idea was this, if i use example.php?u=8, UTF-8 will be used.
I thought this would do the trick.
<?php
$u=($_GET["u"]); // 8 is utf8
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php if ($u=8): ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php else: ?>
<?php endif ?>
<title>just any title</title>
</head>
<body>
</body>
</html>
Obviously I am doing something completely, wrong, since it doesnt even give me the variable.
Where did i go wrong?

PHP stops after return

I have a PHP function that displays an error message as part of form validation, I have mixed in some HTML to create a page for it;
function displayMessage($msg)
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EDGE"/>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
<title>Error sending message</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div style="box-sizing: border-box; width:100%;font-family:arial;font-size:16px;color:#333333;background:#ffe6e3;padding:10px;background:#ffe6e3;border:1px solid #fb8d8d;">
<?php return $msg; ?>
</div>
</body>
</html>
<?php
}
When the code is run everything looks right in the browser, however, looking at the source code it stops right after the message is displayed so the closing div, body, and html tags are not shown.
How do I get them to show, I have tried echo instead of return but that displays the error message but still processes the form.
You are using functions incorrectly, (similar to a require_once). Functions are meant to be called and return a value, not used to include dynamic code in this manner. The reason why it stops after return $msg is because you are telling the function to return a variable called $msg and end further execution of the function block.
I'd suggest using something like this
function displayMessage($msg)
{
$html = <<<EOD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EDGE"/>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
<title>Error sending message</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div style="box-sizing: border-box; width:100%;font-family:arial;font-size:16px;color:#333333;background:#ffe6e3;padding:10px;background:#ffe6e3;border:1px solid #fb8d8d;">
$msg;
</div>
</body>
</html>
EOD;
return $html;
}
echo displayMessage($msg);
return from a function means instantly going back to whoever called it. Means none of the lines below return will be executed.
Change the below:
<?php return $msg; ?>
To:
<?php echo $msg; ?>
The return keyword should be used only inside a function call.
return means "return this value from the function, and stop executing the function". The keyword that you're actually looking for is echo:
function displayMessage($msg)
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EDGE"/>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
<title>Error sending message</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div style="box-sizing: border-box; width:100%;font-family:arial;font-size:16px;color:#333333;background:#ffe6e3;padding:10px;background:#ffe6e3;border:1px solid #fb8d8d;">
<?php echo $msg; ?>
</div>
</body>
</html>
<?php
}
Instead of use a return, change to echo. return means that you want return something to outside of the function. One way of use a return would be:
function displayMessage($msg)
{
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EDGE"/>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
<title>Error sending message</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div style="box-sizing: border-box; width:100%;font-family:arial;font-size:16px;color:#333333;background:#ffe6e3;padding:10px;background:#ffe6e3;border:1px solid #fb8d8d;">
'.$msg.'
</div>
</body>
</html>';
}
And when you call the function you just do:
<?php echo displayMessage(); ?>
Or you can change to echo:
function displayMessage($msg)
{
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EDGE"/>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
<title>Error sending message</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div style="box-sizing: border-box; width:100%;font-family:arial;font-size:16px;color:#333333;background:#ffe6e3;padding:10px;background:#ffe6e3;border:1px solid #fb8d8d;">
'.$msg.'
</div>
</body>
</html>'
}
And call the function:
<?php displayMessage(); ?>

Very Simple echo in PHP error

i have try this code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php phpinfo(); die; ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>TRY IT</title>
</head>
<body>
<?php
echo "test";
?>
</body>
</html>
It is very simple echo syntax in php. Output that should i got is string "test" on my screen. but output that i got just a blank page.
For your information php version that installed on my computer is version 5.3.8
anyone know what happen?
Remove the die on the second line.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php phpinfo(); die; ?>
^^^^ Remove this
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>TRY IT</title>
</head>
<body>
<?php
echo "test";
?>
</body>
</html>

</script> in PHP heredoc

Why I am not able to end a javascript inside a PHP heredoc?
The rest of the code below this line:
</script>
become not part of PHP's code. They become HTML code.
It is like the end script code ends the PHP block.
$headerContent = <<<HEAD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>$title</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-2" />
<script>
</script> // Here is the problem
</head> // code here and below becomes not part of PHP.
<body>
.
.
.
HEAD;
Any tips for solving this problem?
Although I can't reproduce this with HEREDOC (it's possible that different versions of PHP behave differently in this respect), </script> is equivalent to ?> in PHP code, because it's a counterpart to <script language="php">. Example:
<script language="php"> $a = 1; </script>
Test: <?= $a ?>
So wherever you encounter problems with the ?> closing tag, you'll also encounter the same problems with the </script> closing tag. One option would be to store this in a variable and use it. Example:
<?php
$endScript = '</' . 'script>';
$headerContent = <<<HEAD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>$title</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-2" />
<script>
$endScript
</head>
<body>
.
.
.
HEAD;

PHP redirection returns "Cannot modify header information"

I currently have a very simple page that redirects to another URL. However for some reason, I cannot get this to work.
I'm sure there is a very simple solution to this problem and any help to make me understand exactly what's going on would be appreciated.
Here is my page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test - Youtube</title>
</head>
<body>
<?php
ob_start();
header('Location: vnd.youtube:xjTEqVQVsQs');
ob_end_flush();
?>
</body>
</html>
Thanks
You are having some Outputs on your page before sending header and that's why you can not redirect, you should use output buffering like this:
<?php
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test - Youtube</title>
</head>
<body>
<?php
header('Location: vnd.youtube:xjTEqVQVsQs');
die();
?>
</body>
</html>
<?php
ob_end_flush();
?>
<?php
ob_start();
header('Location: vnd.youtube:xjTEqVQVsQs');
ob_end_flush();
?>
remove all the html except above php code if it's only a redirect page.
This is how the code should look like:
<?php
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test - Youtube</title>
</head>
<body>
<?php
header('Location: vnd.youtube:xjTEqVQVsQs'); // Are you sure that's the right
address?
?>
</body>
</html>
<?php
ob_end_flush();
?>
Anyway, the page is redirecting to another page, why do you need the html tags?
You should remove all the page content except PHP script:
<?php
ob_start();
header('Location: vnd.youtube:xjTEqVQVsQs');
ob_end_flush();
?>
Or
put your PHP script on top of the page:
<?php
ob_start();
header('Location: vnd.youtube:xjTEqVQVsQs');
ob_end_flush();
?>
Make sure there is no empty space before php tag above.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test - Youtube</title>
</head>
<body>
</body>
</html>
As redirect sends headers to the browser it MUST be sent before any output.
You can use the method below to redirect using a different method depending on whether the headers have been sent:
<?php
function redirect($filename) {
if ( ! headers_sent())
header('Location: '.$filename);
exit; // just a good practice to EXIT after redirecting
else {
echo '<script type="text/javascript">';
echo 'window.location.href="'.$filename.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$filename.'" />';
echo '</noscript>';
}
}
redirect('http://www.google.com');
?>
This will fallback to using javascript if the headers have already been sent.
If you want to use the header() method it must be at the top of your code before any other output (including whitespace).

Categories