FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


php dropdown lists with if statements or switch





asiddle423
hi can anyone help me with my php email form, it is to change the email address when selected from a dropdown menu

to view what i have been testing go onto http://www.jarcodesigns.co.uk/contactform.php

currently i have this code

//from line 139
if ($_POST['Submit'])
{
//set variables
$name = $_POST['name'];
$email = $_POST['email'];
$ip = $_SERVER['REMOTE_ADDR'];
$message = $_POST['message'];

//if-else statements
if ($department[$key] == 1)
{
$emailaddress = 'xxxx1@jarcodesigns.co.uk';
}
else if ($department[$key] == 2)
{
$emailaddress = 'xxxx2@jarcodesigns.co.uk';
}
else if ($department[$key] == 3)
{
$emailaddress = 'xxxx3@jarcodesigns.co.uk';
}
else if ($department[$key] == 4)
{
$emailaddress = 'xxxx4@jarcodesigns.co.uk';
}
else if ($department[$key] == 5)
{
$emailaddress = 'xxxx5@jarcodesigns.co.uk';
}


$subject = "Contact JARCO";
mail($emailaddress, $subject,"Senders Name: $name <p>Senders Mail: $email </p><p>Their Ip: $ip </p><p>Their Message: $message</p>");

echo $emailaddress;
}
else
{
?>


//this then goes through my form layout and reachs my array code
//line 193
<?
$department = array(
1 => "Web Design (Ryan Norton)",
2 => "Multimedia (Joe Groves)",
3 => "Photography (Oliver Brayford)",
4 =>"Scripting (Andrew Siddle)",
5 => "Logo Design (Andrew Figg)",
);
$department = str_replace(" ", " ", $department);

echo '<select name=department>';
foreach($department as $key => $value)
{
echo '<option value='.$value.'> '.$value.'';
}
echo '</select>';
?>


//then the rest is the layout and submitting part of the form

thanks asiddle423
SirDuncan
hi,

the easier solution is put the emails into the select in the html form:

Department:
<select name="department">
<option value="x1@jarcodesigns.co.uk">Web Design (Ryan Norton)</option>
<option value="x2@jarcodesigns.co.uk">Multimedia (Joe Groves)</option>
<option value="x3@jarcodesigns.co.uk">Photography (Oliver Brayford)</option>
<option value="x4@jarcodesigns.co.uk">Scripting (Andrew Siddle)</option>
<option value="x5@jarcodesigns.co.uk">Logo Design (Andrew Figg)</option>
</select>

and in the php side read it directly : $email = $_REQUEST['department']

but if you don't want the visitors to see the emails in the html code, you may use a switch sentence instead
if.

Smile
asiddle423
i have sorted out the code i just needed to set the name of the option to equal the $key out of the array

eg.
ORIGINAL CODE
echo '<select name=department>';
foreach($department as $key => $value)
{
echo '<option value='.$value.'> '.$value.'';
}
echo '</select>';
?>


NEW CODE
echo '<select name=department>';
foreach($department as $key => $value)
{
echo '<option value='.$key.'> '.$value.''; //changes on this line
}
echo '</select>';
?>
Related topics
Option to choose language when doing a search
phpBB - Installation Tutorial
myBBoard - Installation Tutorial
Duplicate content and home page
Joomla! - Installation Tutorial
{$variablel} on phpbb
Microsoft with yahoo
PHP Dropdown Selection
Problem with PHP if else statements.
PHP how to refresh to some URL according to *.php?id=xxxx
Joomla Support
I could use some help with dropdown lists in php
Get alternate table color with php
IE8 not allowing new random image on each page (PHP and CSS)
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.