Reputation: 21573
I just got started with php. I have a form in a web page that directs to handle.php, like this:
<form action = "handle.php" method="post">
<p><b>name:</b> <input type = "text" name = "name" size="20"/><p>
<input type="submit" name="submit" value="submit my info">
</form>
In my handle.php
, this is what I have:
<!DOCTYPE HTML>
<html>
<head>
<title> Hello! </title>
</head>
<body>
<?php
echo "<p>Name: $_REQUEST['name']<p>";
?>
<p> Hi</p>
</body>
</html>
This page appears completely empty. Interestingly, if I remove $_REQUEST['name']
, then everything is fine. Seems to me that calling $_REQUEST
makes the page empty (i check the source code and theres nothing there).
What am I doing wrong? thanks.
Upvotes: 2
Views: 1135
Reputation: 14905
You should change your code to this:
echo "<p>Name: {$_REQUEST['name']}<p>";
You should take a look at the "complex curly syntax" section on the PHP Manual string page.
And you should read how to enable error reporting.
Upvotes: 2
Reputation: 28326
Repost of my previous answer, with the incorrect bit removed
You didn't wrap the value in {}
properly.
<!DOCTYPE HTML>
<html>
<head>
<title> Hello! </title>
</head>
<body>
<?php
echo "<p>Name: {$_REQUEST['name']}<p>";
?>
<p> Hi</p>
</body>
</html>
You should also not use $_REQUEST
, but rather $_GET
and $_POST
. It's generally a bad idea to get into the habit of using $_REQUEST
because it can open up CSRF (Cross-Site Request Forgery) attacks.
I would also suggest wrapping your user variable in htmlspecialchars
to prevent them from being able to inject HTML or script into your page:
<?php
$name = htmlspecialchars($_REQUEST['name']);
echo "<p>Name: {$name}<p>";
?>
Upvotes: 0
Reputation: 6237
What is probably happening is that you get a fatal error but the configuration of your php.ini in the section error_reporting just ignore it, this is why you get a white page. Change it to E_ALL or something like that to have an easier development!
Once you have an explicit error, if it is not still solved, post it here.
Upvotes: 1
Reputation: 6106
I would guess your script is throwing a syntax error.
Try replacing this line
echo "<p>Name: $_REQUEST['name']<p>";
with
echo "<p>Name: {$_REQUEST['name']}<p>";
See the manual for echo for info on how single- and double-quoted strings work.
In general, it's often a good idea on development sites to enable error reporting so the page displays errors like this rather than just "throwing a whitey". Make sure if you do, that displaying errors is disabled on production sites (although you might want to log errors still).
Upvotes: 1
Reputation:
If you want to echo an array-variable you need to kind of "escape" it like so:
<?php
echo "<p>Name: {$_REQUEST['name']}<p>";
?>
Another solution is this:
<?php
echo '<p>Name: '.$_REQUEST['name'].'<p>';
?>
Look up the diffrences between strings with '
and strings with "
Upvotes: 1
Reputation: 16943
Proper syntax:
echo "<p>Name: ".$_REQUEST['name']."<p>";
or
echo "<p>Name: {$_REQUEST['name']}<p>";
I really recommend you to read this documentation:
Upvotes: 3
Reputation: 3351
Try with this:
echo "<p>Name: " . $_REQUEST['name'] . "<p>";
You may also enable the error report in order to avoid situations like this. You can do this in the php.ini file or at the top of you PHP page: error_reporting(-1).
http://php.net/manual/en/function.error-reporting.php
Upvotes: 5
Reputation: 1894
replace this
echo "<p>Name: $_REQUEST['name']<p>";
to
echo "<p>Name: $_REQUEST[name]<p>";
Upvotes: -5
Reputation: 9121
You have error reporting off and you are receiving a fatal error, which is why you are not seeing anything. Inline variables can't be used the way you do it so try replacing your echo with this:
echo "<p>Name: ".$_REQUEST['name']."<p>";
or this:
echo "<p>Name: {$_REQUEST['name']}<p>";
Upvotes: 4