PDA

View Full Version : trying to change url based on select input



?foru
09-18-2008, 03:54 AM
I am working with an anti-spam contact form and I am using the following to be able to send to more than just one user like it was originally setup.


switch ($_GET['user']) {

case "user1":
$recipient = "user1@domain.com";
break;

case "user2":
$recipient = "user2@domain.com";
break;

default:
$recipient = "info@domain.com"; //... this can be info@ or whatever general email address is used if nothing is specified in the URL
break;
}

It works like this contact.php?user=user1 in the url

Rather than having a bunch of links listed to the side of the form that change the url, I would like to have them select a user in the dropdown so that if user1 is selected the url will be contact.php?user=user1 and the above code will know who to send it to.

Another thing I thought of was that maybe $recipient can be changed into an array so it selects it that way using the following, but I haven't had any luck.


<select name="to">
<?php
foreach( $recipient as $k => $a )
if( (isset($_GET["to"])) && ( $_GET["to"] == $k) )
echo "<option value=\"$k\" selected>$a[0]</option>\n";
else
echo "<option value=\"$k\">$a[0]</option>\n";
?>
</select>

Any help would be appreciated.

Nile
09-18-2008, 11:48 AM
You'd have to make another form(if your other form is using the post method), with the get method. Looking at the above code, you want it to display after the user presses the submit button. So, try making the options tags and select tag in a form. Also please include your whole code so we can help you out more.

?foru
09-19-2008, 01:47 AM
Thank you Nile. I've shortened this up as much as I could such as: not all expoliots, bots, etc.

The existing form does use the post method and submits to itself (no select code included so you can get an idea of how it works)...

Right below <form action="#" method="post"><p> is where I was working to add my <select> tags I previously posted


<?php
function clean($data) {
$data = trim(stripslashes(strip_tags($data)));
return $data;
}

$exploits = "/(content-type|bcc:|cc:)/i";
$profanity = "/(badword1|badword2|badword13)/i";
$spamwords = "/(spamword1|spamword2|spamword13)/i";
$bots = "/(Indy|Blaiz|Java)/i";

if (preg_match($bots, $_SERVER['HTTP_USER_AGENT'])) {
exit("<p class='ect'>Known spam bots are not allowed.</p>");
}
foreach ($_POST as $key => $val) {
$c[$key] = clean($val);

if (preg_match($exploits, $val)) {
exit("<table><tr><td style='background-color:#FFFFCC;border:1px solid #FFCC00;line-height:14px;padding:5px;font-size:11px;color: #596723;'><strong>ERROR:</strong><p>Exploits/malicious scripting attributes aren't allowed.<br><br>Click your back browser button to submit the appropriate content.</p></td></tr></table>");
} elseif (preg_match($profanity, $val) || preg_match($spamwords, $val)) {
exit("<table><tr><td style='background-color:#FFFFCC;border:1px solid #FFCC00;line-height:14px;padding:5px;font-size:11px;color: #596723;'><strong>ERROR:</strong><p>That kind of language is not allowed through our form.<br><br>Click your back browser button to submit the appropriate content.</p></td></tr></table>");
}
}

$show_form = true;
$error_msg = NULL;

if (isset($c['submit'])) {
if (empty($c['name']) || empty($c['email']) || empty($c['comments'])) {
$error_msg .= "Name, Email, and Question / Comment are required fields. \n";
} elseif (strlen($c['name']) > 25) {
$error_msg .= "The name field is limited at 25 characters. Your first name will do! \n";
} elseif (!ereg("^[A-Za-z' -]", $c['name'])) {
$error_msg .= "The name field must not contain special characters. \n";
} elseif (!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$",strtolower($c['email']))) {
$error_msg .= "That is not a valid e-mail address. \n";
}

if ($error_msg == NULL) {
$show_form = false;

if (!empty($c['url']) && !ereg("^(http|https)", $c['url'])) {
$c['url'] = "http://" . $c['url'];
}

$subject = "Contact from your website";
//////// Change top email message line here
$message = "You received this message through your website: \n\n";
foreach ($c as $key => $val) {
$message .= ucwords($key) . ": $val \n";
}
$message .= "IP: {$_SERVER['REMOTE_ADDR']} \n";
$message .= "Browser: {$_SERVER['HTTP_USER_AGENT']}";

if (strstr($_SERVER['SERVER_SOFTWARE'], "Win")) {
$headers = "From: info@yoursite.com \n";
$headers .= "Reply-To: {$c['email']}";
} else {
$headers = "From: $name <$email> \n";
$headers .= "Reply-To: {$c['email']}";
}
##########################################################################################################################################
# THIS WILL BE USED AS form.php?user=XXXX - this portion grabs the user in the URL and sends the results to the appropriate email address.
##########################################################################################################################################
switch ($_GET['user']) {
case "user1":
$recipient = "user1@yoursite.com";
break;

case "user2":
$recipient = "user2@yoursite.com";
break;

default:
$recipient = "info@yoursite.com"; //... this can be info@ or whatever general email address is used if nothing is specified in the URL
break;
}
################################################################################
################################################################################
if (mail($recipient,$subject,$message,$headers)) {
echo "<table><tr><td style='background-color:#FFFFCC;border:1px solid #FFCC00;line-height:14px;padding:5px;font-size:11px;color: #596723;'><strong>SUCCESS: </strong><p>Your message was successfully sent.</p></td></tr></table>";
} else {
echo "<table><tr><td style='background-color:#FFFFCC;border:1px solid #FFCC00;line-height:14px;padding:5px;font-size:11px;color: #596723;'><strong>FAILURE: </strong><p>Your message could not be sent this time. Please try again later.</p></td></tr></table>";
}
}
}
if (!isset($c['submit']) || $show_form == true) {
function get_data($var) {
global $c;
if (isset($c[$var])) {
echo $c[$var];
}
}

if ($error_msg != NULL) {
echo "<table><tr><td style='background-color:#FFFFCC;border:1px solid #FFCC00;line-height:14px;padding:5px;font-size:11px;color: #596723;'><strong>ERROR: </strong><p>";
echo nl2br($error_msg) . "</p></td></tr></table>";
}
?>
<form action="#" method="post"><p>
<label>Name:<br /><input name="name" type="text" id="name" value="<?php get_data("name"); ?>" size="60" />
</label><br />
<label>Email:<br /><input name="email" type="text" id="email" value="<?php get_data("email"); ?>" size="60" />
</label><br />
<label>Question / Comment:<br /><textarea name="comments" cols="60" rows="10" id="comments"><?php get_data("comments"); ?></textarea>
</label><br />
<input type="submit" name="submit" id="submit" value="Send" />
*All fields are required.</p></form>
<?php
}
?>

This has worked great on many sites, but now I am wanting to add the ability of the dropdown now that I modified it to be able to send to a certain user who is in the "user" switch statement.

I experimented a little more last night and tried to use the following with the foreach( $recipient as $k => $a ) code posted previously which might be better than the contact.php?user=user1 method but I didn't have any luck...

$recipient[] = array( 'user1', 'user1@yoursite.com' );
$recipient[] = array( 'user2', 'user2@yoursite.com' );

Thank you again.

?foru
09-19-2008, 03:22 AM
Nile,

This worked ok if I had a link that would take you to to the contact page with string appended to the end. What you mentioned also makes since in that it shouldn't matter if it's passed after the submit button is pressed.

I found this code and it helps to show your point with the string being passed after the submit button is pressed.


<?php

/* this is the form handling code */
$user = '';
if ( isset( $_POST['user'] ) ) { $myselect = $_POST['user'] ; }
if ( isset( $_GET['user'] ) ) { $myselect = $_GET['user'] ; }

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Form Select Handler</title>
</head>

<body>
<form action="<?php $_SERVER['PHP_SELF']; ?>" name="myform" id="myform" method="GET">
<select name="user" id="user">
<option value="user1" <?php print ($user==user1) ? 'SELECTED' : ''; ?>>option1</option>
<option value="user2" <?php print ($user==user2) ? 'SELECTED' : ''; ?>>option2</option>
</select>
<input type="submit">
</form>
</body>
</html>

This looks like it would fit a little better into the existing already long code without having to re-write a bunch of code. Let me know what you think...or if anyone else has any thoughts feel free to contribute.

Nile
09-19-2008, 09:48 PM
Does the above work? If so, use the code.

?foru
09-20-2008, 01:35 AM
When I tried to implement that last code I posted into the form it didn't work. I noticed the "GET" method in the example code, but it looked like it would work with both.

Here is what I added to the existing form, but on "submit" it doesn't pass user=user1 into the URL like example I found.

<?php
/* this is the form handling code */
$user = '';
if ( isset( $_POST['user'] ) ) { $user = $_POST['user'] ; }
if ( isset( $_GET['user'] ) ) { $user = $_GET['user'] ; }
?>
<form action="#" method="post"><p>
<select name="user" id="user">
<option value="user1" <?php print ($user==user1) ? 'SELECTED' : ''; ?>>User 1</option>
<option value="user2" <?php print ($user==user2) ? 'SELECTED' : ''; ?>>User 2</option>
</select><br />
<label>Name:<br /><input name="name" type="text" id="name" value="<?php get_data("name"); ?>" size="60" />
</label><br />
<label>Email:<br /><input name="email" type="text" id="email" value="<?php get_data("email"); ?>" size="60" />
</label><br />
<label>Question / Comment:<br /><textarea name="comments" cols="60" rows="10" id="comments"><?php get_data("comments"); ?></textarea>
</label><br />
<input type="submit" name="submit" id="submit" value="Send" />
*All fields are required.</p></form>

My guess is that since the switch code is using "user" also it doesn't know how to send, and I keep getting the failure message. I also tried to manually add ?user=user1 in the URL and it wouldn't send it that way either.

Nile
09-21-2008, 02:03 AM
Here:

<option value="user1" <?php print ($user==user1) ? 'SELECTED' : ''; ?>>User 1</option>
<option value="user2" <?php print ($user==user2) ? 'SELECTED' : ''; ?>>User 2</option>
You should replace the just '' with null, and when checking the value of $user, use single or double quotes, don't leave it commando.

Can you also show me the get_data() function.
And here:


action="#"

Leave it blank, or use PHP to get the current location, don't use #.

?foru
09-21-2008, 03:36 AM
Yeah in post #4 that was the exact example script as I saw it, but I wondered why $user was blank like $user = ''; I hadn't quite figured out how it would all adapt yet so I'm sure that was part of the problem.

get_data function

function get_data($var) {
global $c;
if (isset($c[$var])) {
echo $c[$var];
}
}

Thanks for catching that...not sure why I had "#" in this version, in a previous version I had

action="<?php echo $_SERVER['PHP_SELF']; ?>"

I started to wonder if $user appeared too much in the script, and I think I found a way that makes it work and may be a little more efficient. Posting so anyone else can take a look...

CODE THAT WORKS
Select is simply...

<select name="recipient">
<option value="">Choose a recipient</option>
<option value="Sales">Sales</option>
<option value="Support">Support</option>
<option value="Service">Service</option>
</select>

Right above my php mail() function I used...

$recipients = array(

'Sales' => 'sales@yourdomain.com',
'Support' => 'support@yourdomain.com',
'Service' => 'service@yourdomain.com'

);

$my_email = $recipients[$_REQUEST['recipient']];

The if statment that includes the mail() function, so you can see how $my_email relates...

if (mail($my_email,$subject,$message,$headers)) {

Some of the other select code seemed a little quirky, and using the GET method for user was ok because the url wasn't that long, but I wanted to try to avoid it for the whole form since the url passing everything could get a little messy. Your posts got the wheels turning and I appreciate the feedback.

Nile
09-21-2008, 03:46 AM
Nice, simple way to do it. I would of suggested that, but I didn't completely understand what you wanted. :P Btw, don't use request. Use get.

?foru
09-21-2008, 02:13 PM
No problem :D Yeah, the switch statement worked ok but I would have had to link to that specific user in the url so the form would have known who to send it to before anything was input. Then I thought maybe the selection in the dropdown could refresh the page and add the user in the url based on what was selected...but after some additional thought that seemed like the wrong way to do it.

Changing from REQUEST to GET now, and thanks again for your input.