I'm trying that validating the text box inside echo in php. I tried some methods but it is not working.
php form code:
echo '<form method="post" action="singlepage.php?id='.$idn.'"';
echo '<label id="blogtextarea2">Comment:</label><textarea rows="10" cols="75" name="comment" id="blogtextarea" ></textarea><br>';
echo '<input type="submit" name="post" id="blogsubmit" value="post">';
echo '</form>';
and validation code is
if(isset($_POST['post']))
{
if ($_POST['comment'] != "") {
$_POST['comment'] = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);
if ($_POST['comment'] == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors= 'Please enter your name.<br/>';
}
}
Ok found it why it is not working
if(isset($_POST['post']))
{
if (!empty($_POST['comment'])) {
$_POST['comment'] = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);
} else {
$errors = 'Please enter your name.';
}
}
echo $errors;
You have to check defined conditions on singlepage.php
I'm not exactly sure what you meant by "not working", but I have cleaned up your code and changed the check for an empty POST comment data using the empty() function instead.
There was also a redundant check within your first check for an empty comment which I've removed:
$errors = '';
if(isset($_POST['post'])) {
if(!empty($_POST['comment'])) {
$_POST['comment'] = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);
} else {
$errors = 'Please enter your name.<br/>';
}
}
echo $errors;