Keep the form and the receiving logic easy to follow: read the expected field, validate it, and escape it when displaying it back to the visitor.
$name = trim($_POST['name'] ?? '');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($name === '') {
echo 'Enter your name.';
} else {
echo 'Hello ' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
}
}
This is the recommended starting pattern. The detailed examples below are retained for explanation, comparison and related variations.
<form method=post action='demo-basic-form2.php'>
<input type=hidden name=todo value=submit-form>
<table cellspacing=0 cellpadding=0 width='484' border=0 align=left bgcolor='#f1f1f1'>
<tr><td>Name</td><td><input type=text name=t1 value=''></td></tr>
<tr bgcolor='#ffffff'><td>gender</td><td><input type=radio name=gender value='Male' >Male
|<input type=radio name=gender value='Female' >Female |</td></tr>
<tr><td>Languages you know</td><td><input type=checkbox name=c1 value=yes >PHP
<input type=checkbox name=c2 value=yes >ASP
<input type=checkbox name=c3 value=yes >JavaScript</td></tr>
<tr bgcolor='#ffffff'><td>Select one day</td><td><select name=s1>
<option value='' >Select One</option>
<option value=Mon >Mon</option>
<option value=Tue >Tue</option>
<option value=Wed >Wed</option>
<option value=Thu >Thu</option>
<option value=Fri >Fri</option>
<option value=Sat >Sat</option>
<option value=Sun >Sun</option>
</select> </td></tr>
<tr><td colspan=2><input type=submit value=Submit> </td></tr>
</table></form>
You can read the above code and remember each component has its own unique name which we can use to collect the data entered by user. Radio buttons of one group will have same name as one of them will be selected. You can read all form components in our html form section.
<html>
<head>
<title>Demo of basic form design and submission to collect data by PHP</title>
<META NAME="DESCRIPTION" CONTENT="Basic form submission ">
<META NAME="KEYWORDS" CONTENT="Basic form validation, demo">
</head>
<body>
<?Php
$name=$_POST['t1']; // Name tag value
$todo=$_POST['todo']; // hidden tag value
$gender=$_POST['gender']; // Value of gender, male or female
$c1=$_POST['c1']; // checkbox for PHP
$c2=$_POST['c2']; // checkbox of ASP
$c3=$_POST['c3']; // checkbox for JavaScript
$s1=$_POST['s1']; // Drop down selection box for day
//// Let us display the variables /////
echo "<pre>
Name = $name
Todo = $todo
gender = $gender
PHP = $c1
ASP = $c2
JavaScript = $c3
Day = $s1
</pre>";
?>
</body>
</html>
After getting the data in receiving page and creating variables, we can store the data in MySQL table or store in simple text file. Using this concept we have created a signup script where members can register. To understand the data inserting or adding process in Database we have different scripts. Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.