If I leave all the fields blank, the code blow is not showing the else message.
<form action="index.php" method="POST">
<textarea name="input_area" rows="6" cols="20"></textarea><br/><br/>
Search<input type="text" name="find"/><br/><br/>
Replace<input type="text" name="replace" /><br/><br/>
<input type="submit" value="Find and replace"/>
</form>
PHP
if(isset($_POST['input_area'])&&($_POST['find'])&&($_POST['replace']))
{
$text=$_POST['input_area'];
$find=$_POST['find'];
$replace=$_POST['replace'];
if(!empty($text)&&!empty($find)&&!empty($replace))
{
$new_str=str_replace($find,$replace,$text);
echo $new_str;
}
else
{
echo 'FILL ALL THE FIELDS';
}
}
The values will be set in $_POST, but they will be blank. A check like this would work in all situations, even in some cases where someone has modified your html and tried something funny.
<?php
$text = isset($_POST['input_area']) ? $_POST['input_area'] : "";
$find = isset($_POST['find']) ? $_POST['find'] : "";
$replace = isset($_POST['replace']) ? $_POST['find'] : "";
if($text != "" && $find != "" && $replace != ""){
$new_str=str_replace($find,$replace,$text);
echo $new_str;
}else{
echo 'FILL ALL THE FIELDS';
}
?>
This statement is the root of the problem
if(isset($_POST['input_area'])&&($_POST['find'])&&($_POST['replace']))
The correct one is
if(isset($_POST['input_area'])&&isset($_POST['find'])&&isset($_POST['replace']))
Aman has a pretty good answer. You may want to check if a user just entered spaces though. Look up ctype_space($str) for that. You could also adapt strlen for this purpose and trim to knock off whitespaces that may appear at the beginning or end of the input (or all of it)... if you want your code structure to look the same.
if ( strlen(trim($_POST['input_area']))>0 && etc.