Reply to Using switch() to process a set of forms

Your name:

Reply:


Posted by Greg Dotts on 03/08/05 00:47

Hi All,

I'm new to PHP, but have read my "Beginning PHP 5 and MySQL" manual and
have searched the net for a solution to my (presumably simple) problem.

I have a series of HTML forms that need to be processed. I am tring to
build a "process.php" file which will evaluate the FORM variable and
select the proper code from a SWITCH statement. I have processed these
forms using a different construct with IF ELSE statments, but it's
getting messy and SWITCH looked like a good solution. The problem is
that it doesn't work - isn't that always the problem! I can't find
anything but simple examples of SWITCH usage on the net. Would someone
be good enough to evaluate this script? Much thanks!!

Below is the "process.php" script and the form tags go something like this:

<FORM action="process.php" method="post" name="form" value="addcontact">
Some fields go here...
</FORM>

<FORM action="process.php method="post" name="form" value="addletter">
More fields go here...
</FORM>

<?php
//Include database access info
include("setup.php");
//Connect to the database server
@mysql_connect($dbhost, $dbuser, $dbpass)or die(mysql_error());
@mysql_select_db ($db) or die(mysql_error());

//<------ Begin Main Script ------>
$x = $_POST['form'];
//Evaluate which form we are receiving and process the results
switch ($x) {
case "addcontact":
//Get the form variables
$cgroup = $_POST['cgroup'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$phone = $_POST['phone'];
$email = $_POST['email'];
//Insert form data into the database
$query = "INSERT INTO contacts SET cgroup='$cgroup', fname='$fname',
lname='$lname', address='$address', city='$city', state='$state',
zipcode='$zipcode', phone='$phone', email='$email'";
$result = mysql_query($query) or die (mysql_error());
break;

case "addletter":
//Get the form variables
$lgroup = $_POST['lgroup'];
$lname = $_POST['lname'];
$content = $_POST['lcontent'];
//Insert form data into the database
$query = "INSERT INTO letters SET lgroup='$lgroup', lname='$lname',
lcontent='$lcontent'";
$result = mysql_query($query) or die (mysql_error());
break;
}
mysql_close();
?>
//END SCRIPT


--
Best regards,
Greg Dotts

If quitters never win, and winners never quit,
what fool came up with, "Quit while you're ahead"?

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация