Jump to content

Form with multiple fields


gple

Recommended Posts

The PHP_SELF is a shorthand way of referring to the server you're working on.

 

The code doesn't have the SQL in it to actually put the data into the DB (I don't think, anyway). You need to write some SQL to INSERT, apply it to a variable, and shoot the variable into the DB using the OnClick command or something,

 

See this:

 

<?php

if ($_POST['submit1']) {
	$email=trim($_POST['email']);
	$query="INSERT INTO email (email_address) VALUES ('$email')";
	$result=mysql_query($query);
}
?>

		<table width="100%">
		<tr>
		  <td width="100%" align="center"><form id="form1" name="form1" method="post" action="<?=$_SERVER['PHP_SELF']?>">
			  <label></label>
			  <input name="email" type="text" id="email" onfocus="this.value = ''; this.onfocus = function () {}" value="E-mail address" size="15" />
			  </label>
			  <input type="submit" name="submit1" value="GO!" onclick="return validate(form1)" />
		  </form></td>
		</tr>
		</table>

JAVASCRIPT
function validate(formCheck)
{
var mail = formCheck.email.value
if (mail.indexOf("@") == -1)
{
alert("Please type a valid email address");
formCheck.email.focus();
return false;
}
alert("Thanks! Your information is being registered");
return true;
}

Here is something thats pretty much what you are asking.

 

I use this form at work for our testers to enter tested equipment into the inventory.

 

 <?php // Connection information Normally in a seperate php file and called with an include. 

$con = mysql_connect("localhost","user_name","pass_word"); // change these to what you use.
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("DB_NAME", $con);

//format date This is for the 2 date fields, it will allow the users to enter a date in mm-dd-yy or mm/dd/yy format.
$string=$_POST['date_rec'];
$dash  = '-';
$pos      = strripos($string, $dash);

if ($pos === false) {
$inputTime=explode("/", $string);
if (substr_count($string, '/') < 2)
{die('ERROR: Improper Date');}
$time=date("Y-m-d", mktime(0,0,0,$inputTime[0],$inputTime[1],$inputTime[2]));
} else {
$inputTime=explode("-", $string);
if (substr_count($string, '-') < 2)
{die('ERROR: Improper Date');}
$time=date("Y-m-d", mktime(0,0,0,$inputTime[0],$inputTime[1],$inputTime[2]));
}


$string2=$_POST['date_clear'];
$pos2      = strripos($string2, $dash);

if ($pos2 === false) {
$inputTime2=explode("/", $string2);
$time2=date("Y-m-d", mktime(0,0,0,$inputTime2[0],$inputTime2[1],$inputTime2[2]));
} else {
$inputTime2=explode("-", $string2);
$time2=date("Y-m-d", mktime(0,0,0,$inputTime2[0],$inputTime2[1],$inputTime2[2]));
}

//post data to the database.
$sql="INSERT INTO Inventory (`Inventory ID`, `Owner ID`, `Date Received`, `Date Cleared`, `Status`, `Equipment Type`, `Make`, `Model`, `Serial Number`, `Processor`, `Speed`, `HD`, `RAM`, `Optical Drive`, `Tag #`, `Comments`, `Tested By`)
VALUES
('$_POST[inventoryID]','$_POST[ownerID]','$time','$time2','$_POST[status]','$_POST[type]','$_POST[make]','$_POST[model]','$_POST[serial]','$_POST[processor]','$_POST[speed]','$_POST[hd]','$_POST[ram]','$_POST[cd]','$_POST[tag]','$_POST[comments]','$_POST[tester]')";

// I use $_POST here instead of SESSION, I have the form set to reload the same page.  

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added", '<br>';
echo $time2;
mysql_close($con) ?>

 

 

Then to make sure everything is entered as it should be, you may want to do a form validation.  something like this snippet.

 

 
$valid_form = true;
     
	if ($_POST['inventoryID'] == "")
	{echo " -- Enter Inventory ID -- " , '<br>'; $valid_form = false;}

	if ($_POST['ownerID'] == "")
	{echo " -- Enter Owner ID -- ", '<br>'; $valid_form = false;}

	if ($_POST['date_rec'] == "")
	{echo " -- Enter Date Received -- ", '<br>'; $valid_form = false;}

                 if($valid_form == false)
	{die('<strong>'. '<font color="#990000">'. ' -- ERROR: Missing Data Fields! -- '.'</font>'. '</strong>');}
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.