Re: Using switch() to process a set of forms

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Sure enough Kirk! That was it. Seems strange that you can set a name/value pair on the <form> tag, but they aren't used. Guess they were just kidding ;-)

Greg



Greg Dotts wrote:

Sure enough Kirk! That was it. Seems strange that you can set a name/value pair on the <form> tag, but they aren't used. Guess they were just kidding ;-)

Greg


kjohnson@xxxxxxxxxxx wrote:

I think the problem is that the name and value attributes of the <form> tag aren't posted with the rest of the data, i.e., $_POST['form'] isn't defined.

You will need to code the form identifier a different way, e.g., either a <hidden> field, or, unique name and value attributes in a <submit> button.

Kirk


Greg Dotts <listmember@xxxxxxxxxxxxxxx> wrote on 03/07/2005 03:47:55 PM:



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"?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php







-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux