Reputation: 29
Need help identifying the problem with the following basic html/php code which works correctly when I input plain text in the textarea. Why does it return blank page when I enter a URL e.g. http://www.example.com?
<form action="" method="POST">
<textarea rows="10" cols="100" name="userdata">
<?=$_POST["userdata"]; ?></textarea>
<input type="submit" name="submit" value="Send" />
</form>
<?php
echo $_POST["userdata"];
?>
Upvotes: 2
Views: 204
Reputation: 571
You code is working. Only first time you open the page $_POST["userdata"] does not exist yet, so try this code:
<form action="" method="POST">
<textarea rows="10" cols="100" name="userdata">
<?php
if (isset($_POST["userdata"])) {
echo $_POST["userdata"];
}
?>
</textarea>
<input type="submit" name="submit" value="Send" />
</form>
<?php
if (isset($_POST["userdata"])) {
var_dump( $_POST["userdata"]);
} else{
echo 'No data';
}
?>
When you see a blank page it is an error, to see all errors, put this code on the beginning of you page:
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
?>
Upvotes: 0
Reputation: 34914
When you load page first time value of $_POST["userdata"]
is not set and is empty. and when your submit then only its value changed. just because of post data.
If you again hard refresh then its value will be empty. because of not post.
Simply I must say, store data in DB and fetch and then display. To do so
Post your value to another page or if in same page check by isset($_POST['userdata'])
and store into db.
And Fetch from db before your html and display into textarea.
Upvotes: 1