Using switch() to process a set of forms

From: Date: Mon, 07 Mar 2005 22:47:55 +0000
Subject: Using switch() to process a set of forms
Groups: php.general 
Request: Send a blank email to php-general+get-210189@lists.php.net to get a copy of this message
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"?

« previous php.general (#210189) next »