NameSilo

Small php question

Spaceship Spaceship
Watch

TeviH

Established Member
Impact
2
Hello,

I am not exactly a php expert, and this is probably a very simple thing to do. I am using http://formtoemail.com for a simple php form script on my website. I am using it to submit an order form. (not secure; no payment transaction)

The question is, how can I prevent items with a value of "val0" from being sent in the form? Basically, that just means the customer doesn't want it, so, there's no need to have that sent in the email. Since there's a pretty long list of products (about 25), it can be tiresome to look for the items with values of "val2" "val3" etc. (I need to use the "val" prefix, rather than a number).

You can view it online over here: http://www.rukiscookies.com/order.php

Thanks!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
post the code that sorts the values and sends it, thats be helpful :P

but im sure a simple if statement would suffice
 
0
•••
Sure! Here:

PHP:
<?php



$my_email = "[email protected]";
$continue = "/";

$errors = array();

if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}

function recursive_array_check_header($element_value)
{

global $set;

if(!is_array($element_value)){if(preg_match("/(%0A|%0D|\n+|\r+)(content-type:|to:|cc:|bcc:)/i",$element_value)){$set = 1;}}
else
{

foreach($element_value as $value){if($set){break;} recursive_array_check_header($value);}

}

}

recursive_array_check_header($_REQUEST);

if($set){$errors[] = "You cannot send an email header";}

unset($set);

// Validate email field.

if(isset($_REQUEST['email']) && !empty($_REQUEST['email']))
{

if(preg_match("/(%0A|%0D|\n+|\r+|:)/i",$_REQUEST['email'])){$errors[] = "Email address may not contain a new line or a colon";}

$_REQUEST['email'] = trim($_REQUEST['email']);

if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("@",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}}

}

// Remove leading whitespace from all values.

function recursive_array_check(&$element_value)
{

if(!is_array($element_value)){$element_value = ltrim($element_value);}
else
{

foreach($element_value as $key => $value){$element_value[$key] = recursive_array_check($value);}

}

return $element_value;

}

recursive_array_check($_REQUEST);

// Check referrer is from same site.

if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";}

// Check for a blank form.

function recursive_array_check_blank($element_value)
{

global $set;

if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}}
else
{

foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);}

}

}

recursive_array_check_blank($_REQUEST);

if(!$set){$errors[] = "You cannot send a blank form";}

unset($set);

// Display any errors and exit if errors exist.

if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;}

if(!defined("PHP_EOL")){define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");}

// Build message.

function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}

$message = build_message($_REQUEST);

$message = $message . PHP_EOL.PHP_EOL."-- ".PHP_EOL."Thank you for using FormToEmail from http://FormToEmail.com";

$message = stripslashes($message);

$subject = "FormToEmail Comments";

$headers = "From: " . $_REQUEST['email'];

mail($my_email,$subject,$message,$headers);

?>

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

<html>

<head>
<title>Form To Email PHP script from FormToEmail.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#ffffff" text="#000000">

<div>
<center>
<b>Thank you <?php print stripslashes($_REQUEST['name']); ?></b>
<br>Your message has been sent
<p><a href="<?php print $continue; ?>">Click here to continue</a></p>

Thanks!!
 
0
•••
I have not read your script, but i think the solution is
PHP:
$message='';

$message='hello dear customer';

if (checkbox_1_is_checked) {
   $message.=' - '.append_your_item_1_here;
}

if (checkbox_2_is_checked) {
   $message.=' - '.append_your_item_2_here;
}

send_the_mail();
 
0
•••
I'm not sure Iknow what you mean, but it sounds like you would have me write out a special snippet of code for each form field... I would rather have a universal line of code that basically says, "if any form field has a value of "val0" do not bother writing out that information. We really don't care."

:)

Any other ideas?
 
0
•••
First, name your first checkbox with cb01, 2nd checkbox with cb02, ...
(your checkboxes do not have name)

now modify the build_message() section
PHP:
<?php
function build_message($request_input){
	if(!isset($message_output)){
		$message_output ="";
	}
	if(!is_array($request_input)) {
		$message_output = $request_input;
	} else { 
		foreach($request_input as $key => $value){
			if(!empty($value)){
				if(!is_numeric($key)) {
					$add=true;
					if (preg_match('/^cb([\d]+)/i',$key,$match)) {
						// if is our special checkbox
						if ($value!='on') {
							// if it is not checked, do not add
							$add=false;
						}
					}
					if ($add) {
						$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;
					}
				}else{
					$message_output .= build_message($value).", ";
				}
			}
		}
	}
	return rtrim($message_output,", ");
}
?>
 
0
•••
the checkboxes are not being sent as part of the data, and are really only used to make the form easier to use, from the user's perspective. Checking a checkbox just sets the quantity to "1" in the select box, but the information that is emailed is only from the select box; not the checkbox.

I don't know if any of this is making sense, so I will try to explain what I need, again: I would like to make some sort of declaration in the php that for any field with a value of "val0" do not email any information about that field. Not the name, and not the value.

Thanks!
 
0
•••
solved!

simple solution (isn't it always?):

instead of having the "0" value be "val0", I just left it blank... (thought I needed it there)
 
0
•••
TeviH said:
solved!

simple solution (isn't it always?):

instead of having the "0" value be "val0", I just left it blank... (thought I needed it there)
You're right. If the select box has "0" value, it does not need to be appended in the mail :lala:
 
0
•••
Dynadot — .com TransferDynadot — .com Transfer
Appraise.net

We're social

Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back