PDA

View Full Version : Form help



kenyan
11-13-2006, 04:13 AM
Hallo,
I want to create a multi page form using dreamweaver such that there is a save and continue button.
The only way I know how to create a form is to have it send by email the input.
Is there any other way i can do this?

;)

rolling
11-13-2006, 07:43 AM
it all depends on what the action is told to do...

The FORM element defines an interactive form. The element should contain form controls--INPUT, SELECT, TEXTAREA, and BUTTON--through which the user interacts.

When the user submits the form, through an INPUT or BUTTON element with TYPE=submit, the form values are submitted to the URI given in FORM's required ACTION attribute. ACTION usually points to a CGI script or Java servlet that handles the form submission.

A mailto URI (e.g., mailto:liam@htmlhelp.com) is also allowed as an ACTION, but this is not supported by all browsers. Non-supporting browsers such as Microsoft Internet Explorer 3.x typically will open a blank e-mail message when the user submits a mailto form. Even on supporting browsers, mailto forms are troublesome in that they fail to provide feedback to the user after the form submission.

Free CGI scripts exist for handling forms; some are even remotely hosted for authors whose providers refuse to allow CGI scripts to be run locally.

How the form input is sent to the server depends on the METHOD and ENCTYPE attributes. When the METHOD is get (the default), the form input is submitted as an HTTP GET request with ?form_data appended to the URI specified in the ACTION attribute.

Using the get method allows the form submission to be contained completely in a URL. This can be advantageous in that it permits bookmarking in current browsers, but it also prevents form data from containing non-ASCII characters such as "" and "". As well, the amount of form data that can be handled by the get method is limited by the maximum length of the URL that the server and browser can process. To be safe, any form whose input might contain non-ASCII characters or more than 100 characters should use METHOD=post.

With a METHOD value of post, the form input is submitted as an HTTP POST request with the form data sent in the body of the request. Most current browsers are unable to bookmark POST requests, but POST does not entail the character encoding and length restrictions imposed by GET.

The ENCTYPE attribute specifies the content type used in submitting the form, and defaults to application/x-www-form-urlencoded. This content type results in name/value pairs sent to the server as name1=value1&name2=value2... with space characters replaced by "+" and reserved characters (like "#") replaced by "%HH" where HH is the ASCII code of the character in hexadecimal. Line breaks are encoded as "%0D%0A"--a carriage return followed by a line feed.

Authors should generally only use a different ENCTYPE when the form includes a TYPE=file INPUT element, in which case the ENCTYPE should be multipart/form-data and the METHOD must be post. The format of multipart/form-data requests is given in RFC 1867.

Tools such as cg-eye allow authors to easily create and view a request, simulating the submission of a form. However, authors often do not need to concern themselves with the exact format of the submission; CGI libraries including CGI.pm transparently handle get and post submissions sent as application/x-www-form-urlencoded or multipart/form-data.

The ACCEPT-CHARSET attribute specifies a list of character encodings that are accepted by the form handler. The value consists of a list of "charsets" separated by commas and/or spaces. The default value is UNKNOWN and is usually considered to be the character encoding used to transmit the document containing the FORM.

The TARGET attribute is used with frames to specify in which frame the form response should be rendered. If no frame with such a name exists, the response is rendered in a new window unless overridden by the user. Special frame names begin with an underscore:

* _blank renders the response in a new, unnamed window
* _self renders the response in the current frame (useful for overriding a BASE TARGET)
* _parent renders the response in the immediate FRAMESET parent
* _top renders the response in the full, unframed window

The FORM element also takes a number of attributes to specify client-side scripting actions for various events. In addition to the core events common to most elements, INPUT accepts the following event attributes:

* ONSUBMIT, when the form is submitted;
* ONRESET, when the form is reset.

kenyan
11-14-2006, 01:45 AM
Richard,
thanks for your reply.Your link especially has alot of very useful info to me.
However am still stumped.
I would like to use My Sql and PHP to handle the forms.can I do so using weshost?
or
Alternatively can i have the output inform of a txt file?
Kenyan

wildjokerdesign
11-14-2006, 07:25 AM
Yes WestHost supports both MySQL and PHP. :) You need to install both via your Site Manager first. I would also suggest installing phpMyAdmin which is a browser based interface to your MySQL databases. That helps in manageing things. Do you have a PHP script in mind that you want to use? That would be your first step unless you plan on codeing it yourself.

rolling
11-14-2006, 07:51 AM
Richard,
I would like to use My Sql and PHP to handle the forms.can I do so using weshost?

Of course you can! Just install MySQL and php from your site manager as WildJoker said and you're away.

You will need a form and a script.

Your form will be embedded in your HTML code and look something like this:


<FORM action="http://yourdomain.com/adduser.php" method="post">
<P>
<LABEL for="firstname">First name: </LABEL>
<INPUT type="text" name="firstname"><BR />
<LABEL for="lastname">Last name: </LABEL>
<INPUT type="text" name="lastname"><BR />
<LABEL for="email">email: </LABEL>
<INPUT type="text" name="email"><BR />
<INPUT type="radio" name="sex" value="Male"> Male<BR />
<INPUT type="radio" name="sex" value="Female"> Female<BR />
<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>
</FORM>


This will pass four parameters; firstname (text), lastname (text), email (text) and sex (text)

Then you need to create your database, with the appropriate privileges. You can do this using phpMyAdmin or from the command prompt. Choose your own names and passwords



mysql
CREATE DATABASE `FormData` ;
CREATE TABLE `UserData` (
`Record` DOUBLE UNSIGNED NOT NULL AUTO_INCREMENT ,
`EMail` CHAR( 80 ) NOT NULL ,
`First` CHAR( 30 ) NOT NULL ,
`Last` CHAR( 30 ) NOT NULL ,
`Sex` SET( 'Male', 'Female' ) NOT NULL ,
INDEX ( `Record` ) ,
UNIQUE (
`EMail`
)
) ENGINE = MYISAM ;
GRANT USAGE ON `FormData` . * TO 'userform'@'localhost' IDENTIFIED BY 'formpass'
GRANT ALL PRIVILEGES ON `FormData` . * TO 'testform'@'localhost' WITH GRANT OPTION ;


Note that I have said that in this case EMail must be unique.

Finally write your script, adduser.php, to populate the database.



<?php
echo "Run script adduser.php <br />";
$firstname="Rolling";
$lastname="Richard";
$sex="Male";
$email="test@fake.com";
//
$dbuser="testform";
$dbpass="testpass";
$dbname="FormData"; //the name of the database
$chandle = mysql_connect("localhost", $dbuser, $dbpass)
or die("Connection Failure to Database");
echo "Connected to database server <br />";
mysql_select_db($dbname, $chandle) or die ($dbname . " Database not found." . $dbuser);
echo "Database " . $dbname . " is selected <br />";
$mainsection="UserData"; //The name of the table where data is to be stored
$query1="insert into " . $mainsection . " (First, Last, Sex) values (\"" . $firstname . "\",\"" . $lastname . "\",\"" . $sex . "\")$
echo "Query " . $query1 . "<br />";
mysql_query($query1) or die("Failed Query of " . $query1);
mysql_close($chandle);
?>


You can run this script from the command line by typing php adduser.php to check that it is working. Then edit it to remove the comments and the definitions for $email, $firstname, $lastname and $sex (Just put // at the start of the line for a comment)

wildjokerdesign
11-15-2006, 07:34 AM
Richard had given you a great start Kenyan! I would like to point out that as is the example adduser.php is not yet "grabing" the information that is sent by the form. To do that you would need to use the $_POST array which is a php global variable. Something like this:


$firstname=$_POST ['firstname'];
I want to also point out that doing just that does not add any filtering to the content that is sent by the user and thus it could create a security risk to your site. The security implications are really too much to go into in a forum post but if you pick up a book on PHP more then likely it would have an example of filtering such forms that would be helpfull to you.

Your other option would be to find an existing php script that would work for you. Also the output of the example is very plain. In other words a user would simply see what comes after "echo" in the script on a plain white screen. :)

It is a good start and hopefully gives you an idea of where to go.

rolling
11-15-2006, 10:21 AM
As I said before...

You can run this script from the command line by typing php adduser.php to check that it is working. Then edit it to remove the comments and the definitions for $email, $firstname, $lastname and $sex (Just put // at the start of the line for a comment)

So you would end up with



<?php
//echo "Run script adduser.php <br />";
//$firstname="Rolling";
//$lastname="Richard";
//$sex="Male";
//$email="test@fake.com";
//
$dbuser="testform";
$dbpass="testpass";
$dbname="FormData"; //the name of the database
$chandle = mysql_connect("localhost", $dbuser, $dbpass)
or die("Connection Failure to Database");
//echo "Connected to database server <br />";
mysql_select_db($dbname, $chandle) or die ($dbname . " Database not found." . $dbuser);
//echo "Database " . $dbname . " is selected <br />";
$mainsection="UserData"; //The name of the table where data is to be stored
$query1="insert into " . $mainsection . " (First, Last, Sex) values (\"" . $firstname . "\",\"" . $lastname . "\",\"" . $sex . "\")$
//echo "Query " . $query1 . "<br />";
mysql_query($query1) or die("Failed Query of " . $query1);
mysql_close($chandle);
?>



Of course, any line beginning // can be deleted and there is more work to be done on the form (such as data validity checking and redirection), but this should get you going.

wildjokerdesign
11-15-2006, 12:17 PM
The West Host installs of php have register_globals set to OFF. In fact since PHP 4.2.0 that is how it comes packaged. http://us2.php.net/register_globals

The values will not be set for:$firstname $lastname $sex or $email unless you do something like this:


$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$sex = $_POST['sex'];
$email= $_POST['email'];


The other alternative would be to edit register_globals in your php.ini but I personally do not prefer that. :)

wildjokerdesign
11-15-2006, 12:25 PM
I just noticed something. Shouldn't this line of the code:

$query1="insert into " . $mainsection . " (First, Last, Sex) values (\"" . $firstname . "\",\"" . $lastname . "\",\"" . $sex . "\")$

be this:

$query1="insert into " . $mainsection . " (First, Last, Sex) values (\"" . $firstname . "\",\"" . $lastname . "\",\"" . $sex . "\")";


I changed the ending character ? to ";. Yours may just be a different coding style but I had not seen it before.

rolling
11-15-2006, 06:08 PM
You're right. Blame that typo on the PuTTY copy function. Anything that runs off the edge of the page is indicated by $