In this artical i am going to explain how to create PHP
forum and how to connect it to the mysql database. For this purpose I
use NetBeans 7.3 as PHP development IDE, HTML5 and PHP 4.0. You can
select development technologies as your wish. This is I describing
regarding one of my project which i did. You can use this as a help of
your projects. This is something similar to registrations. I'll
explain this under 3 main steps.
You
can use javascript functions for validations and CSS for the apply
certain styls for the form. Here I used separate two classes for "form
labels" and "form inputs". Then i can add css separately. Here is some
sample javascript for password comparisons.
function validatePass(p1, p2) {
if (p1.value !== p2.value || p1.value === '' || p2.value === '') {
p2.setCustomValidity('Your passwords does not match.');
} else {
p2.setCustomValidity('');
}
}
javacript for sequrity validations. - This javascript do not allows user to runs the scripts on textbox.
function illegelChar(fld) {
var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
var error = "";
if (fld.value.match(illegalChars)) {
fld.style.background = 'Red';
error = "Some field or fields contains illegal characters.\n";
} else {
fld.style.background = 'White';
}
return error;
}
Note :- It is better to call this function using "onblur(parameter1)" methord.
- You can use following css code to apply styls on your form.
input[type="text"],input[type="email"],input[type="tel"],
input[type="date"],input[type="number"],input[type="textarea"],
input[type="password"],select,input[type="time"],radio,textarea{
width:250px;
border: 2px;
border-radius:5px;
text-indent:2px;
background-color: #FFFFFF;
box-shadow: 0px 0px 5px rgba(0, 255, 51, 0.7), 0px 1px 2px 0px rgba(0, 0, 0, 0.9) inset;
}
input[type="text"]:focus,input[type="email"]:focus,input[type="tel"]:focus,
input[type="date"]:focus,input[type="number"]:focus,input[type="textarea"]:focus,
input[type="password"]:focus,select:focus,option:focus,input[type="time"]:focus,radio{
border: 2px solid rgb(153, 255, 51);
border-radius:5px;
border-top-width: 2px;
border-bottom-width: 2px;
border-top-style: solid;
border-bottom-style: solid;
background-color: #FFFFFF;
border-top-color: rgb(153, 255, 51);
border-bottom-color: rgb(153, 255, 51);
box-shadow: 0px 0px 5px rgba(0, 255, 51, 0.7), 0px 1px 2px 0px rgba(0, 0, 0, 0.9) inset;
}
.formlabels{
vertical-align: top;
text-align: left;
text-shadow: calc;
font-size: 12px;
font-family: sans-serif;
width: 25%;
}
.forminputs{
vertical-align: top;
text-align: left;
text-shadow: calc;
font-size: 12px;
font-family: sans-serif;
width: 75%;
}
.formheadings{
vertical-align: top;
text-align: center;
text-shadow: calc;
font-size: 14px;
text-decoration-color: whitesmoke;
font-family: monospace;
background-color: greenyellow;
}
02 Establish Database Connectivity
To
connect with the database we have to write simple code part in PHP
file. Then it should be include to your code. Otherwise you can write
database connectivity code as a one file along with the form code. But
in that case another place if you wants db connection you have to write
it again and again. So it better to write separately and include it your
code..!
<?php
$host="localhost";
$userName="root";
$password="";
$db_name="sep";
mysql_connect($host,$userName,$password) or die("cannot connect");
mysql_select_db("$db_name") or die ("no database");
?>
03 Pass values to Database
<?php
include "db.php"; // including above cording
if (isset($_POST["submit"])) {
$email = $_POST["textEmail"];
$Fname = $_POST["fname"];
$Lname = $_POST["lname"];
$DOB = $_POST["dob"];
$memType = $_POST["comboMemType"];
$password = md5($_POST["password"]);
$addr = $_POST["address"];
$country = $_POST["countryName"];
$phNo = $_POST["phone"];
$phoneNo = (int) $phNo;
$zip = $_POST["zip"];
$Gender = $_POST["gender"];
$nextYear = mktime(0,0,0,date("m"),date("d"),date("Y")+1);
$next = date("Y/m/d", $nextYear);
$querry = "INSERT INTO member VALUES
('$email','$password','$Fname','$Lname','$DOB','$memType','$addr','$phoneNo','$country','$zip','$Gender','$next')";
$result = mysql_query($querry);
if ($result == 1) {
echo "<script type='text/javascript'>alert('Registration
successful.');window.parent.location='../dehiwalazoo.php';</script>";
} else {
echo "<script type='text/javascript'>alert('Registration was not successfully. Please try again.')
</script>";
}
}
?>