Topic 8 Homework Marking Guide
Topic 8 Homework Marking Guide
Task:
a) Develop the form using HTML5. Include JavaScript for validation of form fields such that no
field is left empty upon submission. [10 Marks]
The code for the form and its validation should be as follows:
<!DOCTYPE html>
<html>
<head>
<title>Suggestion Form</title>
<script type=”text/javascript”>
function validateForm()
{
var name=document.forms["myForm"]["name"].value;
if (name==null || name=="")
{
alert("Name must be filled");
return false;
}
var email=document.forms["myForm"]["email"].value;
if (email==null || email=="")
{
alert("Email Address must be filled");
return false;
}
var subject=document.forms["myForm"]["subject"].value;
if (subject==null || subject=="")
{
alert("Subject must be filled");
return false;
}
var msg=document.forms["myForm"]["suggestion"].value;
if (msg==null || msg=="")
{
alert("Suggestion must be filled");
return false;
}
}
</script>
</head>
<body>
<p><b>Suggestion Form</b></p>
</form>
</body>
</html>
b) On Submission, the Suggestion Form data is saved in a MySQL database called “suggestion”.
Create a database and table with appropriate fields that will store the data. Then,
implement the script that process form data and sends it to the database. [10 Marks]
The code for the script that processed form data should be as follows:
<?php
$server=”localhost”;
$user="root";
$password="";
$database="suggestion";
$con = mysql_connect($server,$user,$password);
@mysql_select_db($database, $con) or die( " Could not establish the connection!");
$name=$_POST['name'];
$email=$_POST['email'];
$gender=$_POST['gender'];
$subject=$_POST['subject'];
$suggestion=$_POST['suggestion'];
$result = mysql_query("INSERT INTO datatable (Name, Email, Gender, Subject, Suggestion) VALUES
('$name','$email','$gender','$subject','$suggestion')", $link);
mysql_close ($con);
?>