PHP Form, PHP guest form
Posted on: September 23, 2010 at 12:00 AM
In this section we will develop PHP PHP Form for accepting some information from the guest.

PHP email form

HTML form

It's not a good idea to leave your private e-mail address on your web-site due to impossible activity of spammers. But nevertheless you need some way to get feedback from your visitors. In the three parts of this tutorial I'll tell you in details how to create a mailbox, that would be spam-protected, and will recieve text messages from you visitors. We'll create 2 small PHP files, one would send messages, the other will recieve them and simultaneuously delete them from the server. Realization of this project requires about 2 kbs of space on a server that supports PHP. Despite the fact, that these example files lack any ornamentation of programming or any other nature, they must just work without requiring any additional settings.

Let's create the first file, index.php

<html><head><title>BRIEF MESSAGE</title></head><body><br>
<table width="100%" height="100%"><tr><td align=center><br>
<form action="index.php"><br>
Message:<br><textarea name="mes"></textarea><br><br><br>
<input name="yes" type="hidden" value="1"><br>
<input type="reset" value="Clean"><br>
<input type="submit" value="Send"><br>
</form><br>
<?php
//php-code from the Part 2<br>
?>
<br>
</td></tr></table></body></html>


The first two lines and the last one - are well-known HTML tags with their attributes, their purpose is to place the input form into the center of the screen. The visible part of the form (form) contains a named multi-line input field textarea and two standard controlling buttons - one for clearing the form (reset) and the other for sending the message (submit). Their captions (value) match their purpose. The action attribute of form tag links to itself, so for controlling the php-script, that we'll write in the second part of the tutorial, let's add a hidden field named yes to the tutorial. As we'll see later, what really matters is not it's value, but the fact of it's existence. But in order not to make the variable empty, let's assign it some value, for example, 1.

If you see our new-created file in some browser, you'll see an input field with two buttons in the center of screen. Though that won't work yet.

receiving the message

Let's now examine the code of PHP script that is to be entered into the file that we've created in the fist part of the tutorial. Now let's give names to 2 simple text files, that will be being created and deleted dynamically by our scripts. Let it be n.txt to store the current number of messages, and m.txt - to store their text.

In order for our script to be executed only in return to the sending button (submit) being pressed, let's state the main condition. If a non-empty variable yes is received, then do work. That's how this rule will look from the point of view of PHP interpretation. After pressing Send button, this rule is held automatically.

if (!empty($_GET['yes'])){
//php code
}


Inside this rule we process the pressure of Send button. If there is an input, let's assign the value entered (text) to the $mes variable, or, if no input is made, we quit the script and inform user about it.

if(!empty($_GET['mes']))$mes=($_GET['mes']);else exit("Input message!");

Now let's write a code for the counter file - n.txt

if (!file_exists("n.txt")){//if there is no counter file,
// let's create it, write into it value "1" and close it.
$fp = fopen("n.txt","w+");
fputs($fp,1);
fclose($fp);
$n[0]=1;//it' s the value of the first and the only one lement of the array.
}else{//if there is a counter file,
//let's read it's value to the array $n, add 1 and close the file
$fp = @fopen("n.txt","r+");
$n = file("n.txt");
$n[0]++; // increase the counter onto 1
fputs($fp, $n[0]);
fclose($fp);
}


Now we need only to write down or add the text, that is entered by the user (together with it's number and date/time) from $mes into m.txt file and inform user about it. Of course, the file must be closed afterwards, and the script must be also be completed.

$dat = date("d m y H:i");//enter the variable - current date and time.
$fp = fopen ("m.txt", "a+");
fwrite ($fp, $n[0].". ".$dat."\n".$mes."\n\n");
fclose ($fp);
exit("Your message is accepted.");


The whole PHP code from the first part of the tutorial will look like the following:

if (!empty($_GET['yes'])){
if(!empty($_GET['mes']))$mes=($_GET['mes']);else exit("Input message!");
if (!file_exists("n.txt")){
$fp = fopen("n.txt","w+");
fputs($fp,1);
fclose($fp);
$n[0]=1;
}else{
$fp = @fopen("n.txt","r+");
$n = file("n.txt");
$n[0]++;
fputs($fp, $n[0]);
fclose($fp);
}
$dat = date("d m y H:i");
$fp = fopen ("m.txt", "a+");
fwrite ($fp, $n[0].". ".$dat."\n".$mes."\n\n");
fclose ($fp);
exit("Your message is accepted.");
}
?>

Reading and deleting the message

Let's create a file for reading and deleting messages. We'll call it, for example, read.php. As in the previous file, in this one the first and the last lines create the html-arrangement of a table.

<html><head><title>Read messages</title></head><body>
<table><tr><td>
<?php
//php-code
?>
</td></tr></table></body></html>


The meaning of the php-code listed below is the following. If no messages (the message file does not exist), we finish the script and inform the user about it. If there are messages, we create an array ($n) of m.txt file lines, and string by string, by the means of a loop from 0 up to the array size (count($n)) print the lines out to the screen. Afterwards we delete the n.txt and m.txt files in order to save disk space.

<?php
if (!file_exists("m.txt")){//if there is no message file
    exit("No messages!");
}else{//if there is a message file
//read messages from it into an array $n
$fp=@fopen("m.txt","r+");
$n=@file("m.txt");
fclose($fp);
//create a loop for a line-by-line print out to the screen
for ($i=0; $i<count($n); $i++)
echo $n[$i]."<br>";
unlink("n.txt");//delete the file
unlink("m.txt");//delete the file
exit;//finish the script
}
?>


Now we place both files creates in this lesson - that is, index.php и read.php - into a folder, that is called, for example, antispam, and put the folder onto a PHP-supporting server.

Than the send-message URL will be http://www.name_of_server.domen/antispam/, and read-message URL - http://www.name_of_server.domen/antispam/read.php

Keep in mind, that the information is deleted from the server, and save the received messages on your PC, if you need them.

Just one note. On a UNIX server it may be required for normal work to add a text files' attributes' setting chmod(), and, if the mailbox will be used intensely, a flock() blockage.

 

More Tutorials on roseindia.net for the topic PHP Form, PHP guest form.
PHP Form, PHP guest form
PHP email form HTML form It's not a good idea to leave your private e...'ll create 2 small PHP files, one would send messages, the other... of this project requires about 2 kbs of space on a server that supports PHP. Despite
 
PHP form
PHP form  Hi Sir/Madam, I am developing an attendance form using php, which displays the name and ID of a Employees which are fetched from Db and I... form and update them to respective coloumn... Please any one help mo out please
 
php form post to mysql
php form post to mysql  How to post data into mysql database from the PHP post data form
 
HTML to php form
HTML to php form  Hi, How I can submit the HTML form data to PHP Script? Give me example code. Thanks   Hi, Please see the following tutorials: PHP Form PHP email form Thanks
 
php loging form connecitivity with mysql
php loging form connecitivity with mysql  my loging form is not connect
 
PHP JavaScript form Validation - PHP
PHP JavaScript form Validation  Just looking for a general PHP JavaScript form Validation. How can I write external JavaScript Validation? Please suggest!  Hi Friend, 1) form Enter Name Enter Address
 
php
php  plz tell me code for PHP SQL Insert,delete,update,view is used to insert the record from HTML page to Mysql database using in single PHP form
 
PHP Sticky form problem
PHP Sticky form problem  I have done the full coding of a sticky form... Calculator</title> <?php if (isset($_POST['submitted']) &&...;/head> <body> <h1>Simple Calculator</h1> <form action
 
PHP Sticky form problem
PHP Sticky form problem  I have done the full coding of a sticky form... Calculator</title> <?php if (isset($_POST['submitted']) && !isset...> <form action="simple_calculator.php" method="post"> <p>Number 1
 
Custom Form Validation - PHP
Custom Form Validation  Which is the best way or where should I implement the custom validation rules such as ? using number, capital letters and symbols to secure the password
 
php
php  im beginner to php. i faced problem with creating form and data...;head> <title>Form Input Data</title> </head> <body> <table border="1"> <tr> <td align="center">Form
 
php
php  im beginner to php. i faced problem with creating form and data...;head> <title>Form Input Data</title> </head> <body> <table border="1"> <tr> <td align="center">Form
 
how to post data in mysql php form
how to post data in mysql php form  how to post data in mysql php form
 
how to create a user registration form in php
how to create a user registration form in php  how to create a user registration form in php
 
PHP Form
PHP Form In this tutorial you will learn how to pass any data from a html page to a php file, and how to use all those data. To do this we need at least two files one is html file and a php file. Here's the given example
 
php form having 2 submit buttons
php form having 2 submit buttons  i have a php form and some text boxes.and 2 buttons"approve" and "disapprove". when i click on approve button the data from form should go in mysql database and mail should go to appropiate
 
value in div from php, reloading form
value in div from php, reloading form  I am having one form with name... woth other login.php <?php include 'login2.php'; function form($name...; </form> <?php } ?> <?php form('','') ?>
 
Fetch the data from mysql and display it on php form
Fetch the data from mysql and display it on php form  when i press on login button, after succesful login the related data of that person should be display in other textbox
 
PHP HTML Form
PHP & HTML Form: In the current tutorial we will study about PHP and HTML form, we will see how to send HTML . We will combine one html file and PHP...;form method="get" action="<?php $_SERVER['PHP_SELF'
 
Multipage form
Multipage form  I have a multipage form in php with 2 pages wnhen i submit the form data from both the pages should go in database how should i pass teh data from 1st page to 2nd page and then put the entire form data in mysql
 
PHP MySQL Login Form
Login Form using PHP and MySQL: In any website generally it is mandatory to have a login form to keep your data secure. In this regard we need to have... will study how to create a login form, connect with the database server and login
 
Session management in php - PHP
Session management in php  I am creating a simple program in PHP to manage the session of user. It's basically a simple form that will allow a user to fill & submit the offer form after login. Please Explain me how can i create
 
How to implement Captcha in PHP Form using GD Library
How to implement Captcha in PHP Form using GD Library  Hi, How to show Captcha in PHP Form. So, please suggest any online reference so that i will try myself. Thanks, (adsbygoogle = window.adsbygoogle || []).push
 
PHP Form Part-1
Topic : HTML FORM Part - 1  In this tutorial, we will learn how to deal with Html Form, JavaScript Validation, MySQL Database and PHP Scripts. We..._TO_REPLACE_1 Let's start with Html Form : If you have little bit  knowledge
 
PHP : Form to Email
PHP : Form to Email       With the help of this tutorial you can send mails to a user using a form, as we have..._TO_REPLACE_8 { ?> <form name="mail" action="<?php
 
How to Create Login/ Registration Form using PHP and MYSQL
How to Create Login/ Registration Form using PHP and MYSQL  Hi, I am learning PHP. I have some dilemma how to create a Login Form and make connectivity with HTML page using PHP and MYSQL. Is there any body can guide me
 
php mail
php mail  how to send the submitted form to mail after 24 hours? pls help me
 
PHP Form Part-2
_TO_REPLACE_2 As this tutorial is based on PHP, so there are several ways to send the form... that the form is on. In other words, if you want send it the form to itself in PHP...Topic : HTML FORM Action Attribute Part - 2 If you noticed, in the previous
 
connectivity with php
connectivity with php  i have make one html form and doing connectivity with php database but when i click on submit button on html form display all coding in php form about connectivity instead of adding data, i used in html
 
PHP Form Part-5
= $_POST['name_attribute']; Now, use this syntax into our php form to get the result... button in php form.  ...Topic : HTML FORM Part - 5 In the previous parts of this tutorial, we covered
 
Invoice Form
Invoice Form  How can I create an Invoice web application (preferable PHP) with customer look-up (with option to add new customer) and a grid to add items (search for items) with a total line below the grid. The grid should have
 
PHP HTML Form Method Attribute
Topic : PHP HTML Form Method Attribute Part - 3 The second attribute in the form tag is Method. With the help of Method attribute, we can tell the browser that how the form information should be sent. Let's see how we used in our form
 
dynamic form
dynamic form  I need to make a dynamic form using php, for example, i... button wich once clicked we have a new list created on the same form. Thank you very much for your help   Here is a php application that creates
 
PHP Regular Expression
PHP regular expression allows programmer to perform various task like... expression one can easily validate user form or perform complex manipulations between strings. PHP Regular expression also allows you to validate a email, phone
 
pre-populate form
pre-populate form  how to pre populate form in php   what do you mean by pre populate
 
project on php
project on php   suppose i have five tabels and i must form them html then link them database and php then i want to make query between them these five tabels and data in each one pleaze help me Table of suppliers Source
 
PHP Question
PHP Question  I am trying to make php Readline where it reads from line 2 and loops to the end. > if (isset($_POST['oldpass'])) { > >... = > @fopen("/home/users/".$username.".txt", > "r"); // Open file form read
 
PHP Question
PHP Question  I am trying to make php Readline where it reads from line 2 and loops to the end. > if (isset($_POST['oldpass'])) { > >... = > @fopen("/home/users/".$username.".txt", > "r"); // Open file form read
 
Sitemap PHP Tutorial
| XML Expat Library | PHP : Form to Email | Create your Email Server... text file in a HTML form. | Create a directory in php | Creating a File... | Check PHP MySQL Connectivity | PHP MySQL Login Form | PHP MySQLI Prep
 
PHP HTML Form Submit Button
important when we use PHP Script on the same Html form. This is the end of Part 4...Topic : HTML FORM SUBMIT BUTTON Part - 4 The another part which is important... of the tutorial then our Html form has covered the action and method attribute
 
PHP Displaying URL Content
PHP Displaying URL Content  In my PHP application form, on submitting the form details it always displaying url content. Can anyone tell me what is the reason and how to restrict it from displaying
 
PHP FORM Part-8
Topic : HTML FORM While Loop Part - 8 In this part of tutorial, we will learn... when the condition is false. The syntax for a while loop in PHP... at the same value.  Let's see the example below : <?php $num
 
PHP - Ajax
PHP  How can ajax is used in php,plz expalin with example.  ...-Type', 'application/x-www-form-urlencoded'); xmlHttp.onreadystatechange... the current date and time from server and shows on the form. To view
 
PHP Training In Delhi
Introduction to PHP arrays Creating form based application in PHP Using MySQL...PHP Training In Delhi If you looking for getting trained in PHP in Delhi, then join our PHP Training course which is provided at our office in Delhi. We
 
connection in php
connection in php  <?php include("include/db.php"); ?> <...;div> <form method="get" action="result.php" enctype="multipart/form...;categories</div> <ul id="cate"> <?php
 
connection in php
connection in php  <?php include("include/db.php"); ?> <...;div> <form method="get" action="result.php" enctype="multipart/form...;categories</div> <ul id="cate"> <?php
 
connection in php
connection in php  <?php include("include/db.php"); ?> <...;div> <form method="get" action="result.php" enctype="multipart/form...;categories</div> <ul id="cate"> <?php
 
database and php
database and php   suppose i have five tabels and i must form them html then link them database and php then i want to make query between them... in html or php and insert the form value to the msyql database. or some thing else
 
checkbox value in php
checkbox value in php  In my HTML,PHP form ..we have a check box that is working fine. But anyhow i am not able to get the value of it. Can anyone suggest how to get the value of checkbox in PHP
 
php - Ajax
php  hi i need ajax registration form validation coding   Hi friend, I am sending you a link. This link will help you. Visit for more information: http://www.roseindia.net/ajax/ajaxlogin/ajax-registration
 
Ads

Related Tags for PHP Form, PHP guest form:


Ads

 
Advertisement null

Ads