I have two php files, form and the results page. Form page accepts user info and the result pages inserts the information into the database and displays it.
form page code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Record Insertion Form</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php
echo "<div style='font-size: large; font-family: sans-serif'><center><h1>
<p style='color: white; background-color: black'>Resume Generator</p></h1></center> </div>";
echo "Mandotary fields with <font color='red'>*</font> are required!";
?>
<form id="form1" name="form1" method="post" action="resumeinsert.php">
<fieldset>
<legend>Personal Details</legend>
<input type="hidden" name="id" id="id" />
<br class="clear" />
<label for="name"><font color="red">*</font> Name:</label><input type="text" name="name" id="name" />
<br class="clear" />
<label for="gender"><font color="red">*</font> Gender:</label>
<input type="radio" name="gender" value="Male" id="gender_0" />Male
<input type="radio" name="gender" value="Female" id="gender_1" />Female
<br class="clear" />
<label for="dateofbirth"><font color="red">*</font> Date of birth:</label><input type="text" name="dateofbirth" id="dateofbirth" />
<br class="clear" />
<label for="placeofbirth"><font color="red">*</font> Place of birth :</label><input type="text" name="placeofbirth" id="placeofbirth" />
<br class="clear" />
<label for="address"><font color="red">*</font> Address:</label><textarea name="address" id="address" cols="45" rows="5"></textarea>
<br class="clear" />
</fieldset>
<fieldset>
<legend><b>Educational Details</b></legend>
<label for="schoolname"><font color="red">*</font> School name:</label><input type="text" name="schoolname" id="schoolname" size=50 />
<br class="clear" />
<label for="qualification"><font color="red">*</font> Qualification(s):</label><textarea name="qualification" id="qualification" cols="45" rows="5"></textarea>
<br class="clear" />
<label for="skills1"><font color="red">*</font> Skills 1:</label><textarea name="skills1" id="skills1" cols="45" rows="5"></textarea>
<br class="clear" />
<label for="skills2">Skills 2:</label><textarea name="skills2" id="skills2" cols="45" rows="5"></textarea>
<br class="clear" />
</fieldset>
<fieldset><legend><b>Other Details</b></legend>
<label for="awards">Awards:</label><textarea name="awards" id="awards" cols="45" rows="5"></textarea>
<br class="clear" />
<label for="Volunteer">Volunteer:</label><textarea name="Volunteer" id="Volunteer" cols="45" rows="5"></textarea>
<br class="clear" />
<input type="submit" name="submit" id="submit" value="Submit" />
<br class="clear" />
</fieldset>
</form>
</body>
</html>
resumeinsert.php page code:
<?php
$mysqli = mysqli_connect("localhost", "hecton", "ccna", "joomladb");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$id = $_POST['id'];
$name = $_POST['name'];
$gender = $_POST['gender'];
$dateofbirth = $_POST['dateofbirth'];
$placeofbirth = $_POST['placeofbirth'];
$address = $_POST['address'];
$schoolname = $_POST['schoolname'];
$qualification = $_POST['qualification'];
$skills1 = $_POST['skills1'];
$skills2 = $_POST['skills2'];
$awards = $_POST['awards'];
$Volunteer = $_POST['Volunteer'];
$query = " INSERT INTO j71mi_resumeinfo ( id, name, gender, dateofbirth, placeofbirth, address, schoolname, qualification, skills1, skills2, awards, Volunteer ) VALUES ( '$id', '$name', '$gender', '$dateofbirth', '$placeofbirth', '$address', '$schoolname', '$qualification', '$skills1', '$skills2', '$awards', '$Volunteer' ) ";
$result = mysqli_query($mysqli, $query);
if ($result === TRUE) {
echo "A record has been successfully inserted into the database!.";
echo "<b><h1><center>My Resume</h1></b></center>";
echo "<div style='font-size: large; font-family: sans-serif'>
<p style='color: white; background-color: black'>Personal Details</p></div>";
echo "<br>";
echo "<b>Name:</b>".$_POST['name'];
echo "<br>";
echo "<b>Gender:</b>".$_POST['gender'];
echo "<br>";
echo "<b>Date of Birth:</b>".$_POST['dateofbirth'];
echo "<br>";
echo "<b>Place of Birth:</b>".$_POST['placeofbirth'];
echo "<br>";
echo "<b>Home Address:</b>".$_POST['address'];
echo "<div style='font-size: large; font-family: sans-serif'>
<p style='color: white; background-color: black'>Educational Details</p></div>";
echo "<br>";
echo "<b>School Name:</b>".$_POST['schoolname'];
echo "<br>";
echo "<b>Qualification(s):</b>".$_POST['qualification'];
echo "<br>";
echo "<b>Skill 1:</b>".$_POST['skills1'];
echo "<br>";
echo "<b>Skill 2:</b>".$_POST['skills2'];
echo "<br>";
echo "<div style='font-size: large; font-family: sans-serif'>
<p style='color: white; background-color: black'>Other Details</p></div>";
echo "<br>";
echo "<b>Award(s):</b>".$_POST['awards'];
echo "<br>";
echo "<b>Volunteer Activity:</b>".$_POST['Volunteer'];
echo "<br>";
echo "<br>";
echo "<b><h5><center>End of Resume</h5></b></center>";
} else {
printf("Could not insert record: %s\n", mysqli_error($mysqli));
}
mysqli_close($mysqli);
}
?>
My problem is that user sometimes submit the form without even filling out all the fields and it displays " Name: ", which contains nothing.
- How can I make the users to fill out the missing required fields after they click submit button with a red text next to the missing fields ? So that empty fields are not sent to database.
2.Some fields are set to NULL in the database, which means the field is optional. How can I not display that specific field in display page if user enters nothing in the form?