Jump to content


Photo

HTML Form to MySQL Database


  • Please log in to reply
3 replies to this topic

#1 vigiw

vigiw
  • Members
  • PipPipPip
  • Advanced Member
  • 100 posts

Posted 07 August 2006 - 06:46 PM

Hi there,

I have an HTML form coded in a PHP file; the form sends data to my MySQL database automatically, and it is working wonderfully!

I just need to know how to change my form to include alternative fields, such as radio buttons, and textarea boxes (the big ones, not the small text fields).

Here is my submit.php file:

<?
include("db.php");

$field1 = $_POST['field1'];
$field2 = $_POST['field2'];
$field3 = $_POST['field3'];
if($_POST['sub'] == "yes") {
if($field1 == "") { $error .= "You did not put any information for field1<BR>"; } //checks to see if the user filled in field1, or if they put in too much information, and defines the variable error if they did
if(strlen($field1) > 30) { $error .= "Field1 is too long.<BR>"; }

if($field2 == "") { $error .= "You did not put any information for field2<BR>"; } //checks to see if the user filled in field1, or if they put in too much information, and defines the variable error if they did
if(strlen($field2) > 256) { $error .= "Field2 is too long.<BR>"; }

if($field3 == "") { $error .= "You did not put any information for field3<BR>"; } //checks to see if the user filled in field1, or if they put in too much information, and defines the variable error if they did
if(strlen($field3) > 256) { $error .= "Field3 is too long<BR>."; }
}
if($_GET['success'] != "yes") { ?>
<form action="submit.php" method=POST>
Field1: <input type="text" maxlength="30" name="field1" <? if(isset($field1)) { echo "value=\"" . $field1 . "\""; } ?>><BR>
Field2: <input type="text" maxlength="256" name="field2" <? if(isset($field2)) { echo "value=\"" . $field2 . "\""; } ?>><BR>
Field3: <input type="text" maxlength="256" name="field3" <? if(isset($field3)) { echo "value=\"" . $field3 . "\""; } ?>><BR>
<input type="hidden" name="sub" value="yes">
<input type="submit" value="Submit!">
</form>
<?
}
/* 
note: .= defines a variable or adds onto it if it already exists... for example
$var1 = "hello";
$var1 .= " my name is corbin";
echo $var1;
would out put hello my name is corbin
*/
if(isset($error)) {
echo "There was one or more error!<BR>";
echo $error;
}
else {
if(($_GET['success'] != "yes") && ($_POST['sub'] == "yes")) { // checks to see if $error is set of if the GET variable of success does not equal yes
$field1 = htmlspecialchars($field1);// if $field does not return an error it replaces any thing that could be interpetted as a browser as html
$field2 = htmlspecialchars($field2);
$field3 = htmlspecialchars($field3);
$q = mysql_query("INSERT INTO `" . $table_name . "` (`field1` , `field2` , `field3`) VALUES ('" . $field1 . "', '" . $field2 . "', '" . $field3 ."')");
if($q) { //what to do if the mysql query is successful 
$error = NULL;
?>
<meta http-equiv="refresh" content="0;url=<? echo $_SERVER['HTTP_SELF'] . "?success=yes"; ?>" ?>">
<?
//by forwarding to its self the page will clear its post values so if a user hits refresh its not inserted into the database again...
}  //the meta tag has it forward it to this page cept with the $_GET['success'] tag set
}
elseif($_GET['success'] == "yes") { //what to do if $_GET['success'] does equal yes
echo "Information successfully put in the database!<BR> View it <a href=\"view.php\">Here</a>.";
}
}
mysql_close(); //closes the link to mysql}
?>

Please let me know the changes I need to make; as in form code changes in the file above, and/or changes in my database field types, etc.

Thanks in advance!! ;D

#2 alecjw

alecjw
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts
  • LocationHatfield, Herts, UK

Posted 07 August 2006 - 08:45 PM

For text areas insert this into your web page:
<textarea name="name_of_textarea">This Text will be shown within the textarea</teatarea>
You may want to edit the SQL field for it and make it data type LONGTEXT.

For radio buttons:
What's your favourite colour?<br />
<input type="radio" name="radio1" value="black" />Black<br />
<input type="radio" name="radio1" value="grey" />Very Dark Grey<br />
If Black is selected, $_POST["radio1"] will be "black"
If Very Dark Grey is selected, $_POST["radio1"] will be "grey"
You will probabl want to make the SQL data type ENUM('black','grey')


For checkboxes:
Do you like pizza?<input type="checkbox" name="pizza" /><br />
Do you like beer?<input type="checkbox" name="beer" checked="checked" />
checked="checked" means that the checkbox will be ticked by default when the page loads.
If pizza is selected, $_POST["pizza"] will be "on" otherwise, it will be "" (empty).
There are two ways of storing this data in the mysql database, either:
Store it as is with ENUM('on','')
OR:
Convert it to a 1 or 0 like this:
$pizza=$_POST["pizza"]
if($pizza=="on")
{
$pizza=1;
}else{
$pizza=0;
}
Then insert $pizza into the database (datatype: BIT(1))


For a drop-down list:
<select name="dropdownlist">
<option value="option1">The first option</option>
<option>Hello</option>
<option value="pie">Apple Pie</option>
</select>
If "The first option" is selected, option1 will be returned as the value.
If "Hello" is selected, Hello will be returned as the value, because if the value is not set, it will be the text between <option>and</option>
You will probably want SQL data type ENUM('option1','Hello','pie') for this.

#3 vigiw

vigiw
  • Members
  • PipPipPip
  • Advanced Member
  • 100 posts

Posted 07 August 2006 - 09:05 PM

Thank you!

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 07 August 2006 - 10:31 PM

Sample code
<?php
// processing the form data
if (isset($_GET['submit'])) {

	echo "Your favourite colour is {$_GET['colour']}<br><br>";
	echo "Your sex is {$_GET['sex']}<br><br>";
	echo 'Your chosen subjects are<br><ul>';
	foreach ($_GET['subject'] as $subj) {
	    echo "<li>$subj</li>";
	}
	echo '</ul>';
	
}

?>
<hr>
<FORM method='get'>
<p>This form has two groups of radio buttons. In the first group, one of the buttons is "checked" by default. 
In the second group, none are checked by default so a hidden field is used 
so that a value is sent for processing even if the user does not select a button</p>
Fave colour<br>
<input type="radio" name="colour" value="red" checked>red<br>
<input type="radio" name="colour" value="green">green<br>
<input type="radio" name="colour" value="blue">blue<br>
<input type="radio" name="colour" value="yellow">yellow<br><br>
Sex<br>
<input type="hidden" name="sex" value="not specified">
<input type="radio" name="sex" value="M" >Male<br>
<input type="radio" name="sex" value="F">Female<br>
<p>For checkboxes you can have multiple values, so pass them in an array by adding "[]" to the name. 
Only checked values are sent</p>
Chosen subjects <br>
<input type="checkbox" name="subject[]" value="Maths">Maths<br>
<input type="checkbox" name="subject[]" value="Biology">Biology<br>
<input type="checkbox" name="subject[]" value="History">History<br>
<input type="checkbox" name="subject[]" value="Politics">Politics<br><br>
<input type="submit" name="submit" value="Submit"> <hr>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users