My form is working fine with the validations being done by PHP.
I have three fields: Name, EMail and Message.
Form and PHP code is within the same pgae, same page is called for validations when user submits the form.
When a user submits the form, same page is called and it checks whether the form is submitted or not.
If the form is submitted it then does the validations for blank entries and throws error message below the fields to inform user that field is left blank. It also shows error icon next to field.
Till this, it is working fine.
However, the problem, is if the user has filled any field, for example name filed and left the other two fields(EMail and Message) blank, then on submittion, it throws error messages for blank fields which is ok, but for name field which was filled by user it empty the content and shows blank name field and does not show error(as earlier user had filled it).
My only concern is that when it relods the form after submission, it should also reload the earlier values in the respective fields which user input before submitting.
Below is the PHP validation code.
<?php
error_reporting(E_ALL & ~E_NOTICE);
if(isset($_POST['nameField_Name']) AND isset($_POST['nameField_EMail']) AND isset($_POST['nameField_Message']) AND isset($_POST['nameSubmit'])){
// Form Submited
if ($_POST['nameField_Name']) {
$phpVarNameField = mysql_escape_string($_POST['nameField_Name']);
} else {
$errormsgNameField = "Name field is required, Please enter your Name.";
}
if ($_POST['nameField_EMail']) {
$phpVarEMailField = mysql_escape_string($_POST['nameField_EMail']);
} else {
$errormsgEMailField = "E-Mail field is required, Please enter your E-Mail ID.";
}
if ($_POST['nameField_Message']) {
$phpVarMessageField = mysql_escape_string($_POST['nameField_Message']);
} else {
$errormsgMessageField = "Message field is required, Please enter your Message.";
}
}
?>
Below is the form code.
<form name="myform" action="contactus.php" method="post"">
<div id="r1">
<div id="r1c1">
<input type="text" name="nameField_Name" id="idField_Name" placeholder="Enter your name here"/>
</div>
<div id="r1c2">
<?php
if(isset($errormsgNameField)){ // Check if $msg is not empty
echo '<img src="error.png" width="45" height="45" style="margin: 5px 0px" alt="">';
}
?>
</div>
</div>
<div id="afterr1">
<?php
if(isset($errormsgNameField)){ // Check if $msg is not empty
echo '<div class="statusmsg" id="idErrorMsgNameField">'.$errormsgNameField.'</div>'; // Display our message and wrap it with a div with the class "statusmsg".
}
?>
</div>
<div id="r2">
<div id="r2c1">
<input name="nameField_EMail" type="text" id="idField_EMail" placeholder="Enter your E-Mail address here" />
</div>
<div id="r2c2">
<?php
if(isset($errormsgEMailField)){ // Check if $msg is not empty
echo '<img src="error.png" width="45" height="45" style="margin: 5px 0px" alt="">';
}
?>
</div>
</div>
<div id="afterr2">
<?php
if(isset($errormsgEMailField)){ // Check if $msg is not empty
echo '<div class="statusmsg" id="idErrorMsgEMailField">'.$errormsgEMailField.'</div>'; // Display our message and wrap it with a div with the class "statusmsg".
}
?>
</div>
<div id="r3">
<div id="r3c1">
<textarea name="nameField_Message" id="idField_Message" placeholder="Enter your message for us here"></textarea>
</div>
<div id="r3c2">
<?php
if(isset($errormsgMessageField)){ // Check if $msg is not empty
echo '<img src="error.png" width="45" height="45" style="margin: 115px 0px" alt="">';
}
?>
</div>
</div>
<div id="afterr3">
<?php
if(isset($errormsgMessageField)){ // Check if $msg is not empty
echo '<div class="statusmsg" id="idErrorMsgMessageField">'.$errormsgMessageField.'</div>'; // Display our message and wrap it with a div with the class "statusmsg".
}
?>
</div>
<div id="r4">
<div id="r4c">
<input type="Submit" name="nameSubmit" id="idButton_Submit" value="Submit" alt="Submit Button"/>
</div>
</div>
</form>
Any help will be great on this.
Thank You.
You will need to add a value attribute on your <input> elements:
<input type="text"
name="whatever"
value="<?php echo htmlspecialchars($_POST['whatever']); ?>"
>
It may be easier to read if PHP outputs the field:
<?php
printf('<input type="text" name="%s" value="%s">',
'whatever',
htmlspecialchars($_POST['whatever']));
?>
This can even be wrapped in a function so you don't need to retype it for every single form field.
Note the call to htmlspecialchars. It is needed so that < and > and quotes don't destroy your HTML document.
Try changing your tag like :
<input type="text"
name="nameField_Name"
id="idField_Name"
placeholder="Enter your name here"
value ="<?php
if (isset($phpVarNameField))
echo $phpVarNameField;
?>"
/>
.......
<input
name="nameField_EMail"
type="text"
id="idField_EMail"
placeholder="Enter your E-Mail address here"
value ="<?php if (isset($phpVarEMailField)) echo $phpVarEMailField; ?>"
/>
.......
<textarea name="nameField_Message" id="idField_Message" placeholder="Enter your message for us
here" value ="<?php if (isset($phpVarMessageField)) echo $phpVarMessageField; ?>" ></textarea>
Good Luck !
Well, You could do validation with jQuery validation plugin - easy and good. jQuery plugin
Or with PHP store POST data in array, check for errors and fields that are not empty set as value to input text.
if (isset($_POST)) {
$data = $_POST;
}
foreach ($data as $row) {
if ($row == "")
$error = true; // do what ever you want
}
and then in form
<input type="text" name="name" value="<?php ($data['name'] != "")? $data['name'] : '' ?>" />
something like this.
Related
I've been trying to solve this problem for several hours, but it didn't work.
The problem is:
I only want to upload photos such as jpg png, but when I upload photos with this type, I get the error message that should only be displayed if the type of uploaded data is wrong.
I wrote all the code, and I also wrote some parts of the code where the problem might be, sSo that everything is clear for you.
<?php
//msg for the first name,
$msg='';
//msg2 for the last name
$msg2='';
//msg3 for the email
$msg3='';
//msg4 for the date
$msg4='';
//msg5 for the password
$msg5='';
//msg6 for the re Password
$msg6='';
//msg7 for the image
$msg7='';
//msg8 for the checkbox
$msg8='';
$msg9='';
$msg10='';
$firstname='';$lastname='';$email='';$date='';$password='';$c_password='';$image='';
include("includes\header.php");
include("includes\config.php");
/**isset: checks if a variable is set, which means that it has to be declared and is not NULL */
if(isset($_POST['submit']))
{
//mysqli not mysql`enter code here
$firstname=mysqli_real_escape_string($con, $_POST['fname']);
$lastname=mysqli_real_escape_string($con, $_POST['lname']);
$email=mysqli_real_escape_string($con, $_POST['mail']);
$date=$_POST['dob'];
$password=$_POST['pass'];
$c_password=$_POST['cpass'];
$image=$_FILES['image']['tmp_name'];
$checkbox=isset($_POST['check']);
// this two line was for testin, if all this is ok
//echo $firstname. "<br>".$lastname."<br>"."<br>".$email."<br>". "<br>".$date."<br>". "<br>".$password."<br>"
//."<br>".$c_password."<br>"."<br>".$image."<br>"."<br>".$checkbox."<br>";
//strlen is to returns the length of the string(firstname)
if(strlen($firstname) <3)
{
// This message must be displayed if the number of letters of the given name is less than 3
$msg="<div class='error'>First name must contain atleast 3 characters</div>";
}
else if(strlen($lastname) <3)
{
// This message must be displayed if the number of letters of the given lastname is less than 3
$msg2="<div class='error'>Last name must contain atleast 3 characters</div>";
}
//filter_var: filters the email with FVE.
else if(!filter_var($email,FILTER_VALIDATE_EMAIL))
{
// This message must be displayed if the number of letters of the given lastname is less than 3
$msg3="<div class='error'>Enter Vaild Email</div>";
}
else if (empty($date))
{
$msg4="<div class='error'> Please Enter Your Date of Birth</div>";
}
else if (empty($password))
{
$msg5="<div class='error'> Please Enter Your Password </div>";
}
else if (strlen($password) <5)
{
$msg5="<div class='error'>Password must contain atleast 5 Character</div>" ;
}
else if ($password!==$c_password)
{
$msg6="<div class='error'> Password is not same </div>";
}
else if ($image=='')
{
$msg7="<div class='error'> Please Upload Your Profile Image </div>";
}
else if($checkbox=='')
{
$msg8="<div class='error'> Please Agree Our Termas an Conditions </div>";
}
else
{
$img_ext=explode(".",$image);
$image_ext=$img_ext['1'];
if($image_ext=='jpg' || $image_ext=='png' || $image_ext=='PNG' || $image_ext=='JPG' || $image_ext=='jpeg' || $image_ext=='JPEG' )
{
// to conection to the database and gives the values
mysqli_query($con, "INSERT INTO users(first_name,last_name,mail,password,dob,img)
VALUES ('$firstname', '$lastname','$email','$date','$password','$image')");
$msg9="<div class='success'> You are Successfully Registered </div>";
$firstname='';$lastname='';$email='';$date='';$password='';$c_password='';$image='';
}
else
{
$msg7="<div class='error'> Please Upload an Image File</div>";
}
}
// this slash is from the if sumbmit
}
?>
//this part of code is from body
</head>
<style>
</style>
<body>
<!-- container: to set the content's margins dealing with the responsive behaviors of the layout.-->
<div class='container'>
<div class='login-form col-md-4 offset-md-4'>
<!--jumbotron: ndicates a big box for calling extra attention to.-->
<div class='jumbotron' style='margin-top:40px; padding-top:20px; background-color:00FF00; font-size: 20px; width:450px'>
<h3 align='center'>SignUp</h3><br>
<?php echo $msg9;?><br>
<!-- form: to creat form for user input
method=”POST” Specifying a value of POST means the browser will send the data to the web server to be processed.
enctype: specifies how the form-data should be encoded when submitting it to the server:
-->
<form method='post' enctype="multipart/form-data">
<div class="form-group">
<label> First Name: </label>
<!-- 1name: the name of th input
2. placeholder: specifies a short hint that describes the expected value of a input field / textarea.
3. All textual <input>, <textarea>, and <select> elements with class .form-control have a width of 100%.
-->
<input type='text' name='fname' placeholder="First Name" class='form-control' value='<?php echo $firstname;?>'>
<!--IF the type of information provided is not the required one, this message will be displayed-->
<?php echo $msg;?>
<!--the end of the div of First Name-->
</div>
<!--start div of the Last Name-->
<div class="form-group">
<label> Last Name: </label>
<input type='text' name='lname' placeholder="Last Name" class='form-control' value='<?php echo $lastname;?>'>
<!--IF the type of information provided is not the required one, this message will be displayed-->
<?php echo $msg2;?>
<!--the end of the div of the Last Name-->
</div>
<!--start div of the Email-->
<div class="form-group">
<label> Email: </label>
<input type='email' name='mail' placeholder="Your Email" class='form-control' value='<?php echo $email;?>'>
<!--IF the type of information provided is not the required one, this message will be displayed-->
<?php echo $msg3;?>
<!--the end of the div of the Email-->
</div>
<!--start div of the birth day-->
<div class="form-group">
<label> Date of Birth: </label>
<input type='date' name='dob' value='<?php echo $date;?>'>
<?php echo $msg4; ?>
<!--the end of the div of birth day-->
</div>
<!--start div of the Password-->
<div class="form-group">
<label> Password: </label>
<input type='password' name='pass' placeholder="Password" class='form-control' value='<?php echo $password;?>'>
<?php echo $msg5;?>
<!--the end of the div of the Password-->
</div>
<!--start div of the Re-enter Password-->
<div class="form-group">
<label> Re-enter Password: </label>
<input type='password' name='cpass' placeholder="Re-enter Password" class='form-control' value='<?php echo $c_password;?>'>
<?php echo $msg6;?>
<!--the end of the div of the Re-enter Password-->
</div>
<!--start div of the image profile -->
<div class="form-group">
<label> Profile Image: </label>
<input type='file' name='image' value='<?php echo $image;?>'/><br>
<?php echo $msg7;?>
<?php echo $msg10;?>
<!--the end of the div of the image profile -->
</div>
<!--start div of the checkbox -->
<div class="form-group">
<input type='checkbox' name='check' />
I Agree the terms and conditions
<?php echo $msg8;?>
<!--the end of the div of the checkbox -->
</div>
<!-- submit button-->
<br><input type='submit' value='submit' name='submit' class='btn btn-success' style="background-color:blue;"><br>
<!--the end of the form for the sign up icon-->
</form>
<!--the end of the jumbtorn class-->
</div>
</div>
</div>
</body>
</html>
I have rewritten the part of the code here where the problem could be.
// this part is from php
else
{
$img_ext=explode(".",$image);
$image_ext=$img_ext['1'];
if($image_ext=='jpg' || $image_ext=='png' || $image_ext=='PNG' || $image_ext=='JPG' || $image_ext=='jpeg' || $image_ext=='JPEG' )
{
// to conection to the database and gives the values
mysqli_query($con, "INSERT INTO users(first_name,last_name,mail,password,dob,img)
VALUES ('$firstname', '$lastname','$email','$date','$password','$image')");
$msg9="<div class='success'> You are Successfully Registered </div>";
$firstname='';$lastname='';$email='';$date='';$password='';$c_password='';$image='';
}
else
{
$msg7="<div class='error'> Please Upload an Image File</div>";
}
}
This part is from body.
I have rewritten the part of the code here where the problem could be.
<!--start div of the image profile -->
<div class="form-group">
<label> Profile Image: </label>
<input type='file' name='image' value='<?php echo
$image;?>'/><br>
<?php echo $msg7;?>
<?php echo $msg10;?>
<!--the end of the div of the image profile -->
</div>
You can add to your input accept parameter:
<input type="file" name="myImage" accept="image/jpeg" />
When user choosing image he will see only JPG files in own file manager. I don't remember is that worked in every browser. In Chrome must work. In Safari maybe not.
Also you can check your file extention before send to server side. Just write custom validator like this (JS code):
var file = "test.mp4";
var valid = (file.substring(file.lastIndexOf("."), file.length)).toLowerCase();
if (valid != '.jpg') {
console.log("Please Select Valid Image");
} else {
console.log("Is valid image");
}
I want to make Login page with PHP.
And want to use button but it didn't works.
and it looks like just refresh the page.
I tried button tag in the form.
How can I make this works??
<?php
include "login.php";
?>
<form method="post" id="sign">
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="email" name="user_mail" id="user_mail" value="<?php echo addslashes($_POST['user_mail'] ?? '') ?>">
<label class="mdl-textfield__label" for="user_mail">Email</label>
</div>
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="password" name="passcode" id="passcode" value="<?php echo addslashes($_POST['passcode'] ?? '') ?>">
<label class="mdl-textfield__label" for="passcode">Password</label>
</div>
</form>
//this <input> works
<input type="submit" name="signup" form="sign"/>
//I want to use this <button>
<button type="submit" name"signup" form "sign" formmethod="post"></button>
<?php
if ($error ?? '') {
echo addslashes($error);
}
login.php
if ($_POST['signup'] ?? '') {
$error = '';
if (!$_POST['user_mail']) $error .= "<br />Please enter email";
else if (!filter_var($_POST['user_mail'], FILTER_VALIDATE_EMAIL)) $error .= "<br />Please enter valid email";
if (!$_POST['passcode']) $error .= "<br />Please enter password";
else {
if (strlen($_POST['passcode']) < 8) $error .= "<br />Please enter a password more than eight";
if (!preg_match('`[A-Z]`', $_POST['passcode'])) $error .= "<br />please least one capital letter";
}
if ($error) $error = "there were errors:" . $error;
<button> means put a button on the page and execute whatever is in the onclick method. <submit> submits a form.
If you want a button to sumbit your form, use the onclick method of the button to do it. Why can't you use submit, though?
See here, they use a link <a onclick='...' but you can do the same for your button: How to submit a form with JavaScript by clicking a link?
As #Arun says below, you can use:
<button type='submit'> inside your form, too. There are lots of ways to do things, so my only advice on top of this is to be consistent. I use input out of habit, and that way I don't have a bunch of mixed conventions throughout my code.
If you want to submit a form on button click, add click event of button using jquery and submit a form like:
Html:
<button type="submit" name"signup" id="btn_submit" value="submit" >Submit</button>
Jquery:
$("#btn_submit").click(function() {
$("#sign").submit(); // submit a form
});
You need to make 2 changes:
Add action attribute
Move your button element inside the form. You can't expect any button on the page to submit your form. It must be inside the form, to which it is associated.
<form method="post" id="sign" action="form-destination.php">
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="email" name="user_mail" id="user_mail" value="<?php echo addslashes($_POST['user_mail'] ?? '') ?>">
<label class="mdl-textfield__label" for="user_mail">Email</label>
</div>
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="password" name="passcode" id="passcode" value="<?php echo addslashes($_POST['passcode'] ?? '') ?>">
<label class="mdl-textfield__label" for="passcode">Password</label>
</div>
<button type="submit" name"signup" form "sign" formmethod="post"></button>
</form>
<?php
if ($error ?? '') {
echo addslashes($error);
}
?>
I think you need to put a action on the form element:
<form method="post" id="sign" action="login.php">
i don't know PHP so i might be wrong.
I'm having some problem with html input validation using PHP. The validation itself is working. I have two inputs: name and office.
If I enter a value on Name input but i didn't put value to the office input and click the submit button, the validation on office input works but it CLEAR/null the data i entered on name input
What am I doing wrong here?
This is my PHP validation:
if (isset($_POST['submit'])){
$signatory_name = $_POST['sig_name'];
$signtory_position = $_POST['sig_position'];
if (!$_POST['sig_name']) {
$errname='<div class="alert alert-danger">Sorry there was an error: Please Enter Your Name</div>';
}
if (!$_POST['sig_office']) {
$erroffice='<div class="alert alert-danger">Sorry there was an error: Please Enter Your office</div>';
}
}
this is my html code
<form action="signatory.php" method="Post" role="form">
<input class="form-control " id="signatoryname" name="sig_name" placeholder="Name:" >
<input class="form-control " id="signatoryoffice" name="sig_office" placeholder="Office:">
</form>
It's not really clear what you are doing or trying to do but here is my attempt:
First: You should know that if (!$_POST['sig_name']) { means if the value assigned is FALSE you may want to reconsider this and use empty() instead.
After validating the inputs you need to repopulate the form with the submitted values - here is an example:
<?php
$errname = "";
$erroffice= "";
if (!empty($_POST)) { // Only if there are POST values attached.
$signatory_name = $_POST['sig_name'];
$signtory_position = $_POST['sig_position'];
if (empty($_POST['sig_name'])) {
$errname='<div class="alert alert-danger">Sorry there was an error: Please Enter youre Name</div>';
}
if (empty($_POST['sig_office'])) {
$erroffice='<div class="alert alert-danger">Sorry there was an error: Please Enter youre office</div>';
}
if (empty($errname) && empty($erroffice)) {
//Do whatever you need with the validated inputs...
} else {
//Expose the alerts:
echo $errname.$erroffice;
}
}
?>
<form method="POST" role="form">
<input class="form-control" id="signatoryname" name="sig_name" value="<?php echo (isset($_POST['sig_name']))?$_POST['sig_name']:""; ?>" placeholder="Name:" />
<input class="form-control" id="signatoryoffice" name="sig_office" value="<?php echo (isset($_POST['sig_office']))?$_POST['sig_office']:""; ?>" placeholder="Office:" />
<!-- rest of your form and buttons -->
</form>
You need to re-populate your form as Scuzzy said.
Most browsers might do you for you, but you can't rely on it.
<form action="signatory.php" method="Post" role="form">
<input class="form-control " id="signatoryname" name="sig_name" placeholder="Name:" value="<?php echo !empty($_POST['sig_name']) ? $_POST['sig_name'] : ''; ?>">
<input class="form-control " id="signatoryoffice" name="sig_office" placeholder="Office:" value="<?php echo !empty($_POST['sig_office']) ? $_POST['sig_office'] : ''; ?>">
</form>
I am new with php, but I have already made a registration script that works fine. But the problem is every time I press the submit button to check my error, I'm going to a new page.
My question is how I make that error comes on the same page?
The code I am useing for the html form.
I want the error display in the error div box that I made Any idea ?
<div id="RegistrationFormLayout">
<h1>Registration Page</h1>
<div id="ErrorMessage"></div>
<form action="script/registration.php" method="post">
<label for="Username">Username</label>
<input type="text" name="Regi_username">
<label for="FirstName">FirstName</label>
<input type="text" name="Regi_Firstname">
<label for="LastName">LastName</label>
<input type="text" name="Regi_Lastname">
<label for="EamilAddress">Regi_EmailAddres</label>
<input type="text" name="Regi_EmailAddres">
<label for="Password">Password</label>
<input type="password" name="Regi_password">
<button type="submit" value="Submit" class="Login_button">Login</button>
</form>
</div>
If I understand correctly, you want form validation errors there. This is a very common pattern, and the simple solution is to always set a form's action attribute to the same page that displays the form. This allows you to do the form processing before trying to display the form (if there are $_POST values). If the validation is successful, send a redirect header to the "next step" page (with header()).
The basic pattern looks like this (in very very simplified PHP)
<?php
if(count($_POST)) {
$errors = array();
$username = trim($_POST['Regi_username']);
if(empty($username)) {
$errors[] = 'username is required';
}
if(count($errors) == 0) {
header('Location: success.php');
die();
}
}
<ul class="errors">
<?php foreach($errors as $error) { ?>
<li><?php echo $error;?></li>
<?php } ?>
</ul>
I am generating form and handling the submit event in the same file.
If user has not entered the title, I want to display the form again and include an error message (e.g. "You forgot the title.").
That means that I have to duplicate code twice - once to diplay empty form and second to display form with body and ask user to enter title:
<?php if(strlen(strip_tags($_POST['posttitle'])) == 0):
// Display the form with question body that user has entered so far and ask user to enter title.
?>
<label for="title"><b>Title:</label><br/>
<input type="text" name="posttitle" id="posttitle" />
<?php endif;?>
<?php elseif ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action']) && $_POST['action'] == 'post') : ?>
<!-- Everything ok - insert post to DB -->
<?php else :
// just display form here (again ouch!)
<label for="title"><b>Title:</label><br/>
<input type="text" name="posttitle" id="posttitle" />
?>
I would do it like this:
If REQUEST_METHOD is POST I will validate the input and collect messages in an array ($errors in my code).
Then I would just print the form and if there was an error the code will print it.
<?php
$errors = array();
function print_value_for($attr) {
if (isset($_POST[$attr]))
echo $_POST[$attr];
}
function print_error_for($attr) {
global $errors;
if (isset($errors[$attr]))
echo $errors[$attr];
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// do validation here and add messages to $errors
// like $errors['posttitle'] = "The title you entered is bad bad bad";
if (empty($errors)) {
// update database and redirect user
}
}
?>
<!-- display the form and print errors if needed -->
<form>
<?php print_error_for('posttitle'); ?>
<input name="posttitle" type="text" value="<?php print_value_for('posttitle') ?>">
<?php print_error_for('postauthor'); ?>
<input name="postauthor" type="text" value="<?php print_value_for('posttitle') ?>">
<?php print_error_for('postbody'); ?>
<textarea name="postbody">
<?php print_value_for('posttitle') ?>
</textarea>
<input type="submit">
</form>
PS. Consider using MVC to separate code and templates.
Here is a quick way to do that.
<form>
<input type="text" name="title" value="<?php echo $_REQUEST['title']; ?>"/>
<input type="text" name="field_a" value="<?php echo $_REQUEST['field_a']; ?>"/>
....
</form>
But I can also advise you to display a var called $title which is the result of a check on $_REQUEST['title].
You could use an output buffer to grab the form and then assign it to a variable like so:
<?php
ob_start();
include('path/to/your/form');
$form = ob_get_flush();
// then later you can just go
print $form;
?>
Hope this helps
When you display the form, use the possibly empty $_POST values as default field values for both the title and question body. If either is empty, the form will display the second time with the other already filled in:
<?php
$message = "";
if (empty($_POST['title'])) $message .= " Please enter a title.";
if (empty($_POST['body'])) $message .= " Please enter a body.";
?>
<form action='me.php'>
<input name='title' type='text' value='<?php if (!empty($_POST['title'])) echo htmlentities($_POST['title'], ENT_QUOTES); ?>' />
<textarea name='body'><?php if (!empty($_POST['body'])) echo $_POST['body']; ?></textarea>
</form>
Read this MVC
Your can write form in view, handler in controller, and business logic in model