Friday, 6 January 2012

HTML - Upload Forms


Use an upload form to allow users to upload pictures, movies, or even their own webpages. An upload form is another type of input form, simply set the type attribute to file.

 

HTML Code:

<input type="file" />

Upload Form:



Max File Size

To limit the size of the file being uploaded and saving you precious webserver space. We make use of a hidden input field and set a few specific attributes.

HTML Code:

<input type="hidden" name="MAX_FILE_SIZE" value="500" />
< input type="file" />

Max File Size:


The value specified is the maximum allowable KB to be uploaded via this form. A value of 100 will allow a file up to 100kb.

Javasript Codes For Putting Current Date On Your Website

The Date object is useful when you want to display a date or use a timestamp in some sort of calculation. In Java, you can either make a Date object by supplying the date of your choice, or you can let JavaScript create a Date object based on your visitor's system clock. It is usually best to let JavaScript simply use the system clock.
When creating a Date object based on the computer's (not web server's!) internal clock, it is important to note that if someone's clock is off by a few hours or they are in a different time zone, then the Date object will create a different times from the one created on your own computer.

JavaScript Date Today (Current)

To warm up our JavaScript Date object skills, let's do something easy. If you do not supply any arguments to the Date constructor (this makes the Date object) then it will create a Date object based on the visitor's internal clock.

HTML & JavaScript Code:

<h4>It is now  
<script type="text/javascript">
<!--
var currentTime = new Date()
//-->
</script>
</h4>

Display:

It is now

Nothing shows up! That's because we still don't know the methods of the Date object that let us get the information we need (i.e. Day, Month, Hour, etc).

Get the JavaScript Time

The Date object has been created, and now we have a variable that holds the current date! To get the information we need to print out, we have to utilize some or all of the following functions:
  • getTime() - Number of milliseconds since 1/1/1970 @ 12:00 AM
  • getSeconds() - Number of seconds (0-59)
  • getMinutes() - Number of minutes (0-59)
  • getHours() - Number of hours (0-23)
  • getDay() - Day of the week(0-6). 0 = Sunday, ... , 6 = Saturday
  • getDate() - Day of the month (0-31)
  • getMonth() - Number of month (0-11)
  • getFullYear() - The four digit year (1970-9999)
Now we can print out the date information. We will be using the getDate, getMonth, and getFullYear methods in this example.

HTML & JavaScript Code:

<h4>It is now  
<script type="text/javascript">
<!--
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
document.write(month + "/" + day + "/" + year)
//-->
</script>
</h4>

Display:

It is now 1/7/2012 !

Notice that we added 1 to the month variable to correct the problem with January being 0 and December being 11. After adding 1, January will be 1, and December will be 12.

Javasript Codes For Putting Current Time On Your Website

Before we begin this javascript codes is program to get the current Time on your Computer but not the place or country you are in....

<html>
<head>
<script type="text/javascript">
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout('startTime()',500);
}

function checkTime(i)//you can change the name of your function to any name you desired but remember to declare it first in the s=checkTime(s) and m=checkTime(m)
{
if (i<10)
  {
  i="0" + i;
  }
return i;
}
</script>
</head>

<body onload="startTime()"><div id="txt"></div>
</body>
</html>

Remember you will insert the blue javascript codes in the head section of your HTML codes..

Sending An Enquiry Form Using Ajax Technology With Php


Before we begin let me introduce you to Ajax.AJAX is about updating parts of a web page, without reloading the whole page.

What is AJAX?

AJAX = Asynchronous JavaScript and XML.
AJAX is a technique for creating fast and dynamic web pages.
AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.
Examples of applications using AJAX: Google Maps, Gmail, Youtube, and Facebook tabs.

How Ajax Works

AJAX

Now we will create a very basic enquiry form using a combination of AJAX, PHP and the Javascript library jQuery. I use jQuery to deal with the AJAX functionality in a simple manner but you could use another Javascript framework or even try to deal with this manually.

Step 1
Create a new file called ‘enquiry_form.html’ and place the below code into it:
  1. <html>
  2. <head>
  3. <title>My Enquiry Form</title>
  4. <script src="jquery.js" language="javascript" type="text/javascript"></script>
  5. <script language="javascript" type="text/javascript">
  6. function send_enquiry() {
  7. $.post("process_enquiry.php", $("#frmEnquiry").serialize(),
  8. function(returnData){
  9. if (returnData=="success") {
  10. $("#enquiry-form").html("Thank you. Your enquiry has been sent successfully");
  11. }else{
  12. alert("An error occured whilst trying to send your enquiry. Please try again shortly\n\n"+returnData);
  13. }
  14. }
  15. );
  16. }
  17. </script>
  18. </head>
  19. <body>
  20. <div id="enquiry-form">
  21. <form id="frmEnquiry">
  22. <table>
  23. <tr>
  24. <td>Name</td>
  25. <td><input type="text" name="name" /></td>
  26. </tr>
  27. <tr>
  28. <td>Enquiry</td>
  29. <td><textarea name="enquiry"></textarea></td>
  30. </tr>
  31. <tr>
  32. <td>&nbsp;</td>
  33. <td><input type="button" name="send" value="Send Enquiry" onclick="send_enquiry()" /></td>
  34. </tr>
  35. </table>
  36. </form>
  37. </div>
  38. </body>
  39. </html>
At the top of the code we are including the jQuery library and the Javascript that will send the users input to a separate file, process_enquiry.php, that we will create in Step 2. In this Javascript we also deal with the outcome of sending the email; If successful remove the enquiry form and display a success message or alternatively, output an error message if for some reason the email could not be sent.
The code above also generates a simple form containing two fields, ‘Name’ and ‘Enquiry’ and should look like the below image:

Simple AJAX/PHP Enquiry Form Step 1
Step 2
Now that we have our form we need a PHP script to actually send the email that contains the user’s details. To do so, create another new file called ‘process_enquiry.php’ and copy the below code into it:
  1. <?php
  2. $to = "your@address.com";
  3. $subject = "Enquiry Received";
  4. $body = "A new enquiry has been received:\n\n";
  5. $body .= "Name:\n".$_POST['name']."\n\n";
  6. $body .= "Enquiry:\n".$_POST['enquiry'];
  7. mail($to, $subject, $body);
  8. echo 'success';
  9. ?> 
    Simply change the email address on line 3 to your own address, upload the three files to a webserver (enquiry_form.html, process_enquiry.php and jquery.js) and you’ve just finished creating a very simple AJAX enquiry form. Go on, give it a go. With any luck once you click ‘Send Enquiry’, the form should disappear and present you with a message and an email should drop into your inbox containing the information you entered.
    Please note, this tutorial has shown how to create an AJAX enquiry form in it’s most simplest format to show the core processes involved. In order to maintain simplicity and focus on the key elements there has been no validation or security efforts put into the code provided. Adding these once you have the code working is highly recommended.Note that you can use the Secure Email PHP script in the previous tutorial on the above Email PHP script.

PHP Codes For Sending A Secure Email

There is a weakness in the PHP e-mail script in the previous  tutorial.NOW let us discuss about PHP email injections.Let`s look at the previous PHP script..

<html>
< body>

< ?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("someone
@example.com", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='mailform.php'>
Email: <input name=
'email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>
< /body>
< /html>


The problem with the code above is that unauthorized users can insert data into the mail headers via the input form.
What happens if the user adds the following text to the email input field in the form?

someone@example.com%0ACc:person2@example.com
%0ABcc:person3@example.com,person3@example.com,
anotherperson4@example.com,person5@example.com
%0ABTo:person6@example.com


The mail() function puts the text above into the mail headers as usual, and now the header has an extra Cc:, Bcc:, and To: field. When the user clicks the submit button, the e-mail will be sent to all of the addresses above.

The best way to stop e-mail injections is to validate the input.
The code below is the same as in the previous chapter, but now we have added an input validator that checks the email field in the form:

<html>
< body>
< ?php
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);

//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}

if (isset($_REQUEST['email']))
{//if "email" is filled out, proceed

//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("someone@example.com", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
}
else
{//if "email" is not filled out, display the form
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>

< /body>
< /html>


In the code above we use PHP filters to validate input:
  • The FILTER_SANITIZE_EMAIL filter removes all illegal e-mail characters from a string
  • The FILTER_VALIDATE_EMAIL filter validates value as an e-mail address.....Now do not forget to use this kind of codes..if you want to code a live website that will be online.....

Totorials For Coding A non-web Form in C# To send Data to an Acess Database

Hi
 This is a tutorial for coding a form example a desktop application to send data from a form to an access database.Lets begin with the codes.This example is part of my final software project for developing a reservation system.
   Let us Begin..by typing the namespace that we will be using for this form..Note that codes in blue are the codes you need to insert the red ones is generated by visual studio..


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

As you can see most of the namespace will be provided by Visual Studio but when you are trying to send data to an access database you it is important to include this namespace  below as i have shown above:
using System.Data.OleDb;
 The Oledb shows that we will be inserting data to an access database .

Now let`s move on to the body of the program that we will be coding but before we begin .Let me inform you that this form will have three textbox ,three labels, a submit button and a cancel button..That being said now let us continue...

namespace Form1
{
    public partial class Form1 : Form
    {
        private OleDbConnection myCon;  // type this code to declare or initializes the name of your Olebd connection
        public Form1()
        {
            InitializeComponent();
            myCon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Navutustars.accdb");//this code shows where the path to your database which is a connection string you can find in your app.config file in your solutions explorer.this is connection string example (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Navutustars.accdb
        }

Below is the code of the submit button before you insert this code note that you must double click the submit button in your form to generate the code then below that code you can insert the line of code which is blue..

private void button1_Click(object sender, EventArgs e)        {
           
            OleDbCommand cmd = new OleDbCommand(); //cmd is the name of our OleDb command            cmd.CommandType = CommandType.Text;// command type will be using is Text command
            cmd.CommandText = "insert into customer (CustomerFirstName,CustomerLastName,CustomerPhoneNo,) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
    
            cmd.Connection = myCon;//assigning our cmd connection as myCon since it is the OleDb connection which we have declare above
            myCon.Open(); //now we are opening the connection
            cmd.ExecuteNonQuery();  //query being execute
            System.Windows.Forms.MessageBox.Show("Data Succefully Send to Database", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);//if suceccful then message box pop out informing you that data has been inserted
            myCon.Close(); //now we are closing the connection           
           
        }
Let me explain about this codes below..
cmd.CommandText = "insert into customer (CustomerFirstName,CustomerLastName,CustomerPhoneNo,) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
In our access database we have to create a table named customer and three fields in that tables namely
CustomerFirstName,CustomerLastName,CustomerPhoneNo  now code above is trying to tell the the machine or computer that we are inserting data from textBox1,textBox2,textBox3 into three fields in a table named customer which has three fields namely CustomerFirstName,CustomerLastName,CustomerPhoneNo...

Ok let us move on to the Cancel Button..For the Cancel button it is simple all you need to do is double click on your cancel button and insert this codes this.Close().

PHP Codes For Sending Emails

PHP allows you toe send emails directly From a Script

Now let`s begin by discussing about the PHP  mail() function.The PHP mail() function is used to send emails directly from a script.
        SYNTAX
    mail(to,subject,message,headers,parameters)

ParameterDescription
toRequired. Specifies the receiver / receivers of the email
subjectRequired. Specifies the subject of the email. Note: This parameter cannot contain any newline characters
messageRequired. Defines the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters
headersOptional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n)
parametersOptional. Specifies an additional parameter to the sendmail program

Moving On To Sending Email

The simplest way to send an email with PHP is to send a text email.
In the example below we first declare the variables ($to, $subject, $message, $from, $headers), then we use the variables in the mail() function to send an e-mail:

< ?php
$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>


Now let us put ourself to the test by creating a simple feedback form for our website.
The example below sends a text message to a specified e-mail address:

<html>
< body>


< ?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email {
//send email $email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("someone@example.com", "$subject",
$message, "From:" . $email);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form {
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>


< /body>
< /html>
This is how the example above works:
  • First, check if the email input field is filled out
  • If it is not set (like when the page is first visited); output the HTML form
  • If it is set (after the form is filled out); send the email from the form
  • When submit is pressed after the form is filled out, the page reloads, sees that the email input is set, and sends the email
Note: This is the simplest way to send e-mail, but it is not secure.I will be discussing the next tutorials about sending secure PHP email in a few days which i will be posting...Hope this tutorials will help you...