You should be getting the following error in your <textarea>
as soon as the page is loaded:
<b>Notice</b>: Undefined variable: content in <b>/path/to/file.php</b> on line <b>x</b><br />
Yet you're not checking for errors.
What you need to do is to use a conditional statement inside it for the $content
variable.
Sidenote: The missing semi-colon in there, is perfectly valid since there is no further (PHP) instructions being passed after it, so let's lose that misconception (in comments).
However, it would be needed if there was a conditional statement when using the following:
if(isset($content)){ echo $content }
and would fail because it's looking for an "end of statement" and throwing the following:
Parse error: syntax error, unexpected '}', expecting ',' or ';' in...
- The semi-colon being an "end of statement" character.
It's unclear as to what you really want to do here with the arrays, so I won't be able to help you there.
Another sidenote:
&&! empty
is valid, but for the sake of argument, I've made it more readable, && !empty
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$search = array('foo','bar','foobar');
$replace = array('f**','b**','f**b**');
if (isset($_POST['cencor']) && !empty($_POST['cencor'])){
$content = $_POST['cencor'];
}
?>
<form action="findstring.php" method="post">
<textarea name="cencor" rows="10" cols="80"><?php if(isset($content)){ echo $content; } ?></textarea><br><br>
<input type="submit" value="submit">
</form>
</body>
</html>
Footnotes:
This conditional statement:
if (isset($_POST['cencor']) && !empty($_POST['cencor']))
can be cut down to merely:
if (!empty($_POST['cencor']))
There's no need to use isset()
, as !empty()
is quite enough.
On an added note:
In order to avoid an undefined variable notice, you could have done the following and assigning "nothing" to the $content
variable, which is perfectly valid:
$search = array('foo','bar','foobar');
$replace = array('f**','b**','f**b**');
if (isset($_POST['cencor']) &&! empty($_POST['cencor'])){
$content = $_POST['cencor'];
}
$content = "";
Closing notes:
If the goal here is to keep the value that was entered in the <textarea>
for the duration of the browser's session and the user navigating throughout your site, then that is just what you need to use here, sessions.
For example:
PHP:
if(!empty($_POST['cencor'])){
$_SESSION['var'] = $_POST['cencor'];
$_POST['cencor'] = $_SESSION['var'];
}
HTML:
<textarea name="cencor" rows="10" cols="80"><?php if(isset($_SESSION['var'])){ echo $_SESSION['var']; } ?></textarea>
NOTA: If this answer does not solve what it is you want to achieve, you will need to elaborate on your question.