Thursday 21 January 2016

How To Do Validation With HTML And PHP

















Basic Form Validation in HTML and PHP

Form validation is a important part of submitting form using PHP or other web development languages, it's mandatory to use form validation, otherwise hacker can play with your website or they can send spam etc, so it is important to learn form validation, today i am going to teach you basic form validation with HTML and PHP.
Now in HTML 5 there are many validation attributes like required, disabled, number, email, patterns, url many more, i added some html validation attributes for a simple validation, it's a just a little validation form a basic validation form that may help you for learning basic validation with HTML and PHP.
Let's start with HTML validation attributes, i used required attribute which is HTML 5 attribute, it's easy to set it, just write required on the last of your input types, after done, if user will not perform input and if user click on the submit button so a little box appear with the message of required.




I used required attribute in all input types, i also used on gender so if user not set gender then user will get a message box like this.





HTML Codes Explanation

I used form elements and attributes like action and method , it's important to set this for data sending or retrieving data on the page, action attribute is use for to do action, like if we set action="process.php" so it will perform the action process.php if we set nothing on action like this action="" so it will perform action to the current page as default, i am using method POST, for data sending or retrieving there are two ways POST And GET, for sending sensitive data always use POST method.
I created table i used tr table row and td table data, i used input types like text for user name, email for user email, number for mobile number, user can't enter text, only numbers are allowed, i used password for user password and radio for select gender then at the last i created submit button, it is very important to create submit button.
I tried to make my form validation page look cool and little attractive , so i tried to use CSS (Cascading Style Sheet), i hope you liked the design, let's don't talk about it, our focus should be on HTML, PHP.

HTML CODES WITH CSS


<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">

body{
background:#eee;
}input[type=text], input[type=email], input[type=number], input[type=password]{
width:400px; height:40px; padding:0px 5px; margin-bottom:5px; font-size:20px; font-family:verdana; font-weight:normal; color:#aaa;
}
tr td{
font-size:20px; font-family:arial;
}
input[type=submit]{
cursor:pointer;width:100%; height:60px; border:none; background:orange; color:#fff; font-size:20px; margin-top:10px;
}
input[type=submit]:hover{
background:red;
}
.form-box{
width:450px; padding:10px; border:1px solid #ddd;margin:0 auto; margin-top:100px ;background:#fff; font-family:verdana;
}
</style>
</head>
<body>
<div class="form-box">
<center>
<h1>Form Validation</h1>
<form action="" method="post">
<table>
<tr><td><input type="text" name="name" id="" placeholder="Enter Full Name" required /></td></tr>
<tr><td><input type="email" name="email" id="" placeholder="Enter Email Address" required /></td></tr>
<tr><td><input type="number" name="mobile" id="" placeholder="Enter Mobile Number " required /></td></tr>
<tr><td><input type="password" name="pass" id="" placeholder="Choose Password" required /></td></tr>
<tr><td><input type="password" name="confirm_pass" id="" placeholder="Confirm Password" required /></td></tr>
<tr><td>Gender : <input type="radio" name="gender" value="male" id="" required /> Male <input type="radio" name="gender" value="female" id="" required/> Female</td></tr>
<tr><td><input type="submit" name="submit" value="Sign Up" /></td></tr>
</table>
</form>
</center>
</div>
</body>
</html>

PHP CODE


<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$pass = $_POST['pass'];
$confirm_pass = $_POST['confirm_pass'];
$gender = $_POST['gender'];

$num_length = strlen((string)$mobile);
if($num_length == 10) {
if($pass == $confirm_pass)
{
echo '<script type="text/javascript">alert("Account Created Successfuly");</script>';
}
else
{

echo '<script type="text/javascript">alert("Password do not match!");</script>';
exit();
}
}
else{
echo '<script type="text/javascript">alert("Mobile digits should be 10");</script>';
exit();
}
}




?>

PHP Code Explanation

I used isset() function to check the parameter in isset() function is set or not like i used if(isset($_POST['submit'])){} isset function will check if user clicked on submit or not if user clicked then if statements will execute.
I created some local variables like $name, $email etc, local variables are holding the value of user input.
I added little PHP validation like Mobile number can't be greater or lesser than 10 digits.
i added another validation to check if user password and confirm password is matching or not matching.
if you fill all the things correctly then you will receive the alert pop up from JavaScript.
Thanks, if you have any query or any problem just comment below.

Wednesday 20 January 2016

How To Start With PHP Web Language


















Today this topic is for the peoples who wants to start PHP language or web development, I will tell you how can you learn fast without taking the coaching or classes, I will tell you which website is good for learning, which stuffs is important, which apps is help you for learning PHP language, which forums will help you.

What is PHP?
Php stand for PHP: Hypertext Preprocessor, PHP is very popular server side scripting language, it is open source language, it is a powerful tool for creating dynamic and interactive websites, this language is designed for web development, this web development language is created by Rasmus Lerdorf.

How to start?
You don’t need to purchase anything, its open source; you need two things for getting started in very popular web development language.
What need to do?
  1.     Wamp Server Or Xamp Server
  2.      Notepad++ or any other text editor

For learning purpose I will suggest you Notepad++.
Install Wamp server or Xamp server in your computer machine.-
Download Notepad++ on your computer machine, Just google these things.

Now what to do and how to do?
You did these things so what is next.
Now I am going to suggest you top websites for learning PHP
  1. www.w3schools.com
  2. www.codeacademy.com
  3. www.tutorialspoint.com
  4. www.phpbuddy.com
  5. www.php.net

w3schools is a great website for learning purpose if want to become a web designer or a web developer, it’s a totally about Web design and development, there are a lot of stuffs which will help you very very much.
Another codeacademy, tutorialspoint is very good website for learning PHP language.

Some Great Android PHP Apps
If you don’t have computer or if you can’t use your computer all time, and you have smart phone, you don’t have to worry, I am going to tell you good android apps that will help you to learn, just search on your goolge play store.
1.      PHP Programs
2.      PHP-MySQL
3.      Learn PHP
4.      PHP Programming
5.      PHP Manual

PHP Forums

If you have some queries or questions in mind or if you have any bug or error in your PHP script so you have to make some friends who are PHP learner, If you don’t have, it’s okay, i will mention here some good PHP forums, where programmer is ready to solve your problems.
1.      www.invisionpower.com
2.      www.phpbb.com
3.      www.bbpress.com

After Learning Basic
After learning basic you should learn OOP (Object Oriented Programming) concept in PHP, there are several YouTube videos will help you on the internet.

PHP Frameworks
PHP Framework is use for speeding up the web development process, frameworks saves a lot of time for building web applications, you have to learn frameworks for web development, a lot of web developers asked for if they know any PHP framework, so here the list of best and top PHP frameworks.
Top PHP frameworks are:
  1.       Laravel Framework
  2.      Phalcon
  3.      Code ignitor
  4.     Yii


I hope my this post will help you for learning php, If you have any question or anything you want to ask about this, Just comment below I will definitely help you.



Tuesday 19 January 2016

How To Create Beautiful Calculator Using HTML, CSS, PHP


I will teach you how to create beautiful calculator using Html, CSS, Php.
I used HTML (Hyper Text Markup Language) for creating calculator structure then i did some CSS (Cascading Style Sheet) to make my project attractive and stylist, then i written script for my Calculator project using PHP, i am sure it will help you a lot.

What you need to do first

Make sure you have installed wamp or xamp server on your computer.
Make sure your wamp server or xamp server is turned on.
You can make new folder as calculator into your www directory, after that just create index.php in your calculator folder, and paste the code which i added here, it's so easy then save your index.php.
You can also download via link.

What is important to do in HTML

<form action="" method="post"> </form> tag is very important for sending data or retrieving data, set action attribute as you like, i wanted to display result on current page so i didn't set this, like if you want to do the result on another file so just write the file url.
There are two types of Super global variables for sending data or retrieving data, $_POST and $_GET, depend on your requirements, i am using post method, for sending sensitive data use post.
Submit button is very important.
Submit button is required.
Set the name of Text inputs, i have set the name first text box as first_value and second text box as second_value.
Then i added Select input i given name operator, i created some options and assigned them values.

PHP Code Expanation

I used Switch statement in our calculator project, for me i feel very easy to do calculation script with Switch statement, you can also use if else if statements.
I used isset() function to check, if you click on submit button it will do action on current file because we have not set any action so as default it will perform action on the current page.
First it will collect user data and store user data into our local variables like $first_value and $second_value and $operator.
Switch statement will check user options if user clicked on addition then it will execute '+' statement if user click on subtraction then it will execute '-' statement some others depend on user input.

Here is the source code
I hope you will like it, If you have any query just comment below.


<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Simple Calculator</title>
</head>
<style type="text/css">
table{
width:60%; background:orange; margin:0 auto; box-shadow:10px 10px 30px #ccc; border-bottom:20px solid #aaa;
}
.result-box{
width:60%; margin:0 auto; background: orange; height:100px; margin-top:20px; padding:20px; color:white;box-shadow:10px 10px 30px #ccc; font-family:verdana;
}
td{
font-size:20px; padding:10px; font-family:verdana; color:#fff;
}
input[type=text]{
padding:10px; width:100px; font-size:20px;
}
input[type=submit]{
padding:10px;  font-size:16px; cursor:pointer;border:none; background:yellow; color:#333; width:300px; height:50px;
}
input[type=reset]{
padding:10px;  font-size:16px;cursor:pointer; border:none;background:yellow; color:#333; height:50px;
}
input[type=submit]:hover{
background:#eee; color:#444;
}
input[type=reset]:hover{
background:#eee; color:#444;
}
h1{
background:orange; color:white; font-family:verdana; padding:20px;
}

</style>
<body>
<form action="" method="post">
<table>
<center>
<h1>How To Create Simple Calculator Using PHP</h1>
<tr><td>Select First Value</td><td><input type="text" name="first_value" id="" /></td></tr>
<tr><td>Select Second Value</td><td><input type="text" name="second_value" id="" /></td></tr>
<tr><td>Select Oprator </td>
<td>
<select name="operator" id="">
<option value="static">Select Operator</option>
<option value="+">Addition</option>
<option value="-">Substraction</option>
<option value="*">Multiplication</option>
<option value="/">Division</option>
</select></td></tr>
<tr><td><input type="reset" value="Reset The Numbers" /></td><td><input type="submit" name="submit" value="Let's Calculate" /></td></tr>
<tr><td colspan="2">Author : Abuzar Ansari</td></tr>
</center>
</table>
</form>
<?php
if(isset($_POST['submit'])){
$first_value = $_POST['first_value'];
$second_value = $_POST['second_value'];
$operator = $_POST['operator'];
?>
<div class="result-box">
<h3>Your Results : <?php
switch($operator){ case "+":
echo $first_value + $second_value; 
break;
case "-":
echo $first_value - $second_value; 
break;
case "*":
echo $first_value * $second_value; 
break;
case "/":
echo $first_value / $second_value; 
break;
default:
echo "Please Use Only Numbers";
break;
}
}
?></h3>
</div>
</body>
</html>


How To Upload Image In PHP

















Today I am going to teach you how to upload photos using Html and Php.

A lot of website like Facebook, Twiitter and Google+ allow their users to upload the images on their profile, so how it’s work?

I will teach you basic of how to upload images using Html and Php with little validation.


Do First Step By Step:

  • First create folder in www directory, name this folder as upload. 
  • Now open your upload folder. 
  • Create new folder in upload, name this folder as uploaded_images. 
  • This folder “uploaded_images” will save our uploaded images. 

Let’s code Html form first

<form action="" method="post" enctype="multipart/form-data">
<center> <table>
<h1>How To Upload Image</h1>
<tr><td>Select Image</td><td><input type="file" name="image" id=""/></td></tr>


<tr><td><input type="submit" value="Upload Image" /></td></tr> </table>
<h1>Author Abuzar Ansari</h1>
</center>
</form>








It is important to use  enctype=”multipart/form-data ” for uploading file purpose if we not use it, our uploading script will not upload images.
Make sure your input type is set to file.
Set the name image.

Let’s write php script for uploading image.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$image_name = $_FILES['image']['name'];
$image_size = $_FILES['image']['size'];
$image_type = $_FILES['image']['type'];
$image_tmp  = $_FILES['image']['tmp_name'];
if($image_type == 'image/jpeg' or $image_type == 'image/png' or  $image_type == 'image/gif' )
{
$uploaded = move_uploaded_file($image_tmp, "images/$image_name");
if($uploaded)
{
echo '<script type="text/javascript">alert("Image Has Been Successfuly Uploaded");</script>';
}
}
else
{
echo '<script type="text/javascript">alert("Please Upload Only Jpeg, Png Or Gif Format File ");</script>';
}
}
?>
<!--- abuzaransariweb@gmail.com Author Abuzar Ansari --->

PHP Script Explanation:

We have used If condition that mean if request is post then it will execute if statements.
We are using POST method.
Uploading data is multipart data like image name, image size, image type, image temp it’s a multiple data.
We have created local variables like $image_name, $image_size etc.
All the local variables holding the value of Image name, size etc.
We used if condition to check it is image format file or another format file.
If it is a image format file like Jpeg, Png, Gif then code will execute, if it is another format file ‘else’ statement code will execute.
If it is a image format file then our if statement will execute and upload our file sucessfuly.
We use move_uploaded_file() function for uploading purpose.
Now go to your images directory, you have successfully uploaded images.
We can use many validation like if image size is larger than 1mb or if image is not selected, this is a basic example for uploading images.
Thank you. If you face any problem just comment below.

Monday 18 January 2016

How to insert data into Database using Php and MySqli

Today I will teach you how to create PHP data insert script, using the Text, Html, CSS, Php, Mysqli.
For the data sending or retrieving it is important to use form element or we can say tags.
Like this <form method=”post” action=””></form>.
We are using method POST.
We have not set action attribute because we are giving action to current page.
  1. Create new folder in www name this folder Student_Record
  2. Create New File as index.php in your Student_Record folder
  3. Now Open your browser chrome or firefox type in address tab localhost/phpmyadmin






Make sure your wamp server or xamp server is turned on.

Create new database name this database record.



















Then click on record and create new table in record.
Name the table student







Your student table structure will look like this.







Create columns like Name, Email, City, Website, Father, Mother. Give the VARCHAR to all, set the value 100 to all columns.
Your connection script will look like this
<?php
$connection = mysqli_connect("localhost","root","","record");
?>


PHP MySQLi = PHP MySQL Improved!
Edit your index.php and write script for inserting data in your Html elements, write code below.
Here is the full version of index.php file.
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Insert Record</title>
    <style type="text/css">
    *{
        margin:0; padding: 0;
    }
    body{
        background:#eee;
    }
    .container{
        width:60%; background:#eee; margin:0 auto; margin-top:20px;
    }
    h1{
        background:#ffba34; font-family:verdana; color:#fff; margin:10px 0px;padding:10px;
    }
    input[type=text]{
        width:320px; height:40px; font-size:20px; font-family:century gothic;
    }
    input[type=text]:focus{
        outline:none;  font-weight: normal;
    }
    tr td{
        font-family:verdana; font-size:20px; background:#ed6c9e; padding:10px; color:#fff;
    }
    input[type=submit]{
        width:427px; cursor:pointer;transition: background 0.5s;border:1px solid #fff; height:50px; background:#fff;font-size:20px;float:left; border:none;
    }
    input[type=submit]:hover{
        background:#ffba34; color:#fff; border:1px solid #fff;
    }
    </style>
</head>
<body>
    <div class="container">
    <form action="" method="post">
        <center>
        <table>
        <h1>Insert Record Using PHP Mysqli</h1>
        <tr><td>Name</td><td><input type="text" name="name" id="" placeholder="Enter Name.."/></td></tr>
        <tr><td>Email</td><td><input type="text" name="email" id="" placeholder="Enter Email.."/></td></tr>
        <tr><td>City</td><td><input type="text" name="city" id="" placeholder="Enter City.."/></td></tr>
        <tr><td>Website</td><td><input type="text" name="website" id="" placeholder="Enter Website.."/></td></tr>
        <tr><td>Father</td><td><input type="text" name="father" id="" placeholder="Enter Father.."/></td></tr>
        <tr><td>Mother</td><td><input type="text" name="mother" id="" placeholder="Enter Mother.."/></td></tr>
        <tr><td colspan="2"><input type="submit" name="submit" value="Insert Record" id="" /></td></tr>
        </table>
        </center>
    </form>
     
    <?php
$connection = mysqli_connect("localhost","root","","record");
?>
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$city = $_POST['city'];
$website = $_POST['website'];
$father = $_POST['father'];
$mother = $_POST['mother'];
$query = "insert into student(name,email,city,website,father,mother) values('$name', '$email', '$city', '$website', '$father', '$mother')";
mysqli_query($connection,$query);
}
?>
    <center><h1 style="red">Author Abuzar Ansari</h1></center>
    </div>
</body>
</html>


Let’s explain the php script
Hope you have learned the php basic, basic will help you.
We use if, for the conditional statement like in our script it is saying if isset, isset is a php function that check the value is set or not.
If submit button is clicked, that mean button is set, so our if statement will execute.
$name is a local variable which is holding the value of ‘name’ which is coming from the user input.
All the variables are holding the values that are coming to user input.
We have set variables names.
We created $query local variable which is holding the query values, or we can say what query we would like to do.
$query is saying -> insert data into student table, we entered column name like name, email, city, website, father mother, which are available in student table.
We will enter the value from users input, Like ‘$name’ will take the first place $email take second place as described in $query variable.
After this we will use mysqli_query(); function, which is used to run your query in this function we will set two parameters mysqli_query($connection,$query); first parameter for database connection second parameter for query.
Now Go To localhost/phpmyadmin and check the records database and student table, We have successfully submitted data. If any query comment below.