Solved

PHP Mailer Form Help

Posted on 2009-04-01
26
791 Views
Last Modified: 2013-12-13
Hi All

Having a bit of trouble with Dagon's PHP form mailer - specifically with checkbox values.

My array is below - and displays correctly.
However, I need two things:

1. I need all checkboxes to tick if the "All" checkbox is checked
2. I need the label text to be sent in the email (this currenly only works for the All label.

Can anyone help?

Danny


$form_structure = array(	
	"select|fmselect|Title|fm_title|32|100| |None|Mr|Mr|Mrs|Mrs|Miss|Miss|Ms|Ms|Dr|Dr",
	"text|fmtext|First Name|fm_name|32|100|true|none",
	"text|fmtext|Last Name|fm_lastname|32|100|true|none",
	"text|fmtext|House Number|fm_house|32|100|true|none",
	"text|fmtext|Post Code|fm_postcode|32|100|true|none",
	"text|fmtext|Telephone|fm_telephone|32|100|true|none",
	"text|fmtext|Mobile|fm_mobile|32|100|false|none",	
	"text|fmtext|Email|fm_email|32|100|false|email",
	"select|fmselect|Who is the lift for?|fm_lift|32|100|Myself|Myself|Family|Family|Friend|Friend|Other|Other",
	"select|fmselect|Stairlift type|fm_type|32|100|Curved|Curved|Straight|Straight|Not Sure|Not Sure",
	"select|fmselect|Urgency|fm_urgency|32|100|Immediate|Immediate|1-4 Weeks|1-4 Weeks|Over 1 Month|Over 1 Month",
	"checkbox|frmcheck|I would like a call back from|fm_from|All|All|false|Acorn|Acorn|false|Brooks|Brooks|false|Companion|Companion|false|Stannah|Stannah|false",
);

Open in new window

0
Comment
Question by:dannywareham
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 12
26 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 24045907
To check all the checkboxes like you describe, you need a javascript function. There's an example here:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/JavaScript_Frameworks/Q_24082521.html
0
 
LVL 26

Author Comment

by:dannywareham
ID: 24047615
Does it have to be javascript?
The form is created on the fly through an array - so referencing controls is pretty impossible...
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24054129
You don't need to do it in JS.

Follow this.

1 - You are creating the HTML dynamically.
2 - As you draw the checkboxes, create a list of the values and add them to the session. Create a unique key for this array (just in case you have multiple sets of checkboxes).
3 - Set the value for the "All" checkbox to the unique ID. This joins the "All" checkbox to the list in the session.
4 - As part of the form processing, when you get an "All", find the list in the session based upon the key. Add the session list to the $_POST. Remove the "All" entry.
5 - Process the $_POST as expected.

That should be pretty quick and easy to implement.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Author Comment

by:dannywareham
ID: 24054149
Thanks for checking by RQuadling - much appreciated.

The mailer contents are built from an array. The full code is below so you can see.

I'm not building the checkboxes manually - so I'm not sure how to group them.


<?php
session_start();
// Email address
$recipients = "onerecip|info@datamouse.biz";
 
//Form Structure
 
$form_structure = array(	
	"select|fmselect|Title|fm_title|32|100| |None|Mr|Mr|Mrs|Mrs|Miss|Miss|Ms|Ms|Dr|Dr",
	"text|fmtext|First Name|fm_name|32|100|true|none",
	"text|fmtext|Last Name|fm_lastname|32|100|true|none",
	"text|fmtext|House Number|fm_house|32|100|true|none",
	"text|fmtext|Post Code|fm_postcode|32|100|true|none",
	"text|fmtext|Telephone|fm_telephone|32|100|true|none",
	"text|fmtext|Mobile|fm_mobile|32|100|false|none",	
	"text|fmtext|Email|fm_email|32|100|false|email",
	"select|fmselect|Who is the lift for?|fm_lift|32|100|Myself|Myself|Family|Family|Friend|Friend|Other|Other",
	"select|fmselect|Stairlift type|fm_type|32|100|Curved|Curved|Straight|Straight|Not Sure|Not Sure",
	"select|fmselect|Urgency|fm_urgency|32|100|Immediate|Immediate|1-4 Weeks|1-4 Weeks|Over 1 Month|Over 1 Month",
 	"checkbox|frmcheck|I would like a call back from|fm_from|All|All|false|Acorn|Acorn|false|Brooks|Brooks|false|Companion|Companion|false|Stannah|Stannah|false",
);
 
// CAPTCHA (disabed)
$field_verification = "";
 
// Email Headers
$field_name = "fm_name";
$field_subject = "StairLiftAdvisor.co.uk Call-Back Enquiry";
$field_email = "fm_email";
$field_dropdownrecip = "fm_sendto";
 
 
// Required fields (puts an asterix next to requird fields
// if TRUE
$show_required = TRUE;
 
// Make Email
$subject = 'Website Contact: ';
$show_headers_in_message = FALSE;
$show_code_in_message = FALSE;
$wrap_messages = TRUE;
$include_ip = FALSE;
 
// Messages on Form
 
// no connection
$msg_mailserver = "No connection to the mailserver. Please try again later.";
// success
$msg_sent = "<h2>Thank you!</h2><p>Your message has been sent!</p>";
// error
$msg_error = "Error!";
// submit button
$msg_submit = "Send";
?>
 
 
<?php
// EVERYTHING BELOW HERE SHOULD NOT BE TOUCHED!!
$email_sent = FALSE;
$t_out = "";
 
 
// turn $recipients into a useable array
$recipients = explode("|", $recipients);
for ($i = 0; $i < count($recipients); $i++) {
	$recipients[$i] = trim($recipients[$i]);
}
 
// turn form fields into a useable array
for ($i = 0; $i < count($form_structure); $i++) {
	$form_structure[$i] = explode("|", $form_structure[$i]);
	for ($j = 0; $j < count($form_structure[$i]); $j++) {
		$form_structure[$i][$j] = trim($form_structure[$i][$j]);
	}
}
 
 
function is_valid_url($link) { 
// returns TRUE if url actually exists
 
	if (strpos($link, "http://") === FALSE) { $link = "http://" . $link; }
    $url_parts = @parse_url($link);
	if (empty($url_parts["host"])) return( false );
	if (!empty($url_parts["path"])) { $documentpath = $url_parts["path"]; } 
    else { $documentpath = "/"; }
	if (!empty($url_parts["query"])) { $documentpath .= "?" . $url_parts["query"]; }
	$host = $url_parts["host"];	$port = $url_parts["port"];
	if (empty($port)) $port = "80";
	$socket = @fsockopen( $host, $port, $errno, $errstr, 30 );
	if (!$socket) {	return(false); } 
    else {fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
	$http_response = fgets( $socket, 22 );
	if (ereg("200 OK", $http_response, $regs)) { return(true); fclose($socket);	} 
	else { return(false); } }
}
 
 
function is_valid_email($email) {
// returns TRUE if email address is valid
 
	$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
	$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
	$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
		'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
	$quoted_pair = '\\x5c[\\x00-\\x7f]';
	$domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
	$quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
	$domain_ref = $atom;
	$sub_domain = "($domain_ref|$domain_literal)";
	$word = "($atom|$quoted_string)";
	$domain = "$sub_domain(\\x2e$sub_domain)*";
	$local_part = "$word(\\x2e$word)*";
	$addr_spec = "$local_part\\x40$domain";
	return preg_match("!^$addr_spec$!", $email) ? 1 : 0;
}
 
 
function injection_chars($s) {
// returns TRUE if 'bad' characters are found
	return (eregi("\r", $s) || eregi("\n", $s) || eregi("%0a", $s) || eregi("%0d", $s)) ? TRUE : FALSE;
}
 
 
function generate_verification() {
	srand((double)microtime()*1000000); 
	$rand = rand(0,999999999);  
	$thecode = substr(strtoupper(md5($rand)), 2, 5); 
	$thecode = str_replace("O", "A", $thecode);
	$thecode = str_replace("0", "B", $thecode);
	$_SESSION["thecode"] = $thecode;
}
 
function strip_colons($s) {
	return str_replace(array(':', '%3a'), " ", $s);
}
 
 
$first_load = TRUE;
 
if (isset($_POST["form_submitted"])) {
// if the form has been submitted
 
	$first_load = FALSE;
 
	// prepare error list
	unset($errors);
 
 
	// prepare mail variables
	$mail_message = "";
	$mail_name = "Anonymous";
	$mail_subject = $subject;
	$mail_email = 'noone@nowhere.com';
 
 
	// process input
	foreach ($form_structure as $form_field) {
 
 
	switch ($form_field[0]) {
 
	case "text":
	case "password":
 
		$f_type = $form_field[0];
		$f_name = $form_field[2];
		$f_fmname = $form_field[3];
		$f_max = $form_field[5];
		$f_req = $form_field[6];
		$f_ver = $form_field[7];
 
		// get data
		$t = (isset($_POST[$f_fmname])) ? trim($_POST[$f_fmname]) : "";
		if (get_magic_quotes_gpc()) {
			$t = stripslashes($t);
		}
 
		// check for size limits
		if (strlen($t) > $f_max) {
			$errors[] = "There is a $f_max character limit for '$f_name'.";
		}
 
		// check for required fields
		if (($f_req == "true") && ($t == "")) {
			if ($f_fmname != $field_verification) { // has it's own check
				$errors[] = "Missing required field: '$f_name'.";
			}
		}
 
		// check for injection characters
		if (($f_type != "textarea") && (injection_chars($t))) {
			$errors[] = "Invalid input in '$f_name'!";
		}
 
		// check for valid email (if present/required)
		if (($f_ver == 'email') && ($f_req == "true" || (trim($t) != ""))) {
			if (!is_valid_email($t)) {
				$errors[] = "Invalid email address: '$f_name'";
			}
		}
		// check for valid url (if present/required)
		if (($f_ver == 'url') && ($f_req == "true" || (trim($t) != ""))) {
			if (!is_valid_url($t)) {
				$errors[] = "Invalid link: '$f_name'";
			}
		}
 
 
		// check for headers/verification fields
		if ($f_fmname == $field_name) {
 
			if ($t != "") {
				$mail_name = strip_colons($t);
				if ($show_headers_in_message)
					$mail_message .= $f_name . ' = ' . $t . "\n\n";
			}
	
		} elseif ($f_fmname == $field_subject) {
 
			if ($t != "") {
				$mail_subject = $subject . strip_colons($t);
				if ($show_headers_in_message)
					$mail_message .= $f_name . ' = ' . $t . "\n\n";
			}
 
		} elseif ($f_fmname == $field_email) {
 
			if ($t != "") {
				$mail_email = strip_colons($t);
				if ($show_headers_in_message)
					$mail_message .= $f_name . ' = ' . $t . "\n\n";
			}
 
		} elseif ($f_fmname == $field_verification) {
 
			if ($t == "") { 
				$errors[] = "Enter the verification code!";
			} else if (trim($_SESSION["thecode"]) == "") { 
				$errors[] = "No verification code generated!";
			} else if ($_SESSION["thecode"] != strtoupper($t)) { 
				$errors[] = "Invalid verification code!";
			} else {
				if ($show_code_in_message)
					$mail_message .= $f_name . ' = ' . $t . "\n\n";
			}
 
		} else {
 
			if ($t != "") {
				$mail_message .= $f_name . ' = ' . $t . "\n\n";
			}
 
		}	
 
 
		break;
 
 
	case "textarea":
 
		$f_name = $form_field[2];
		$f_fmname = $form_field[3];
		$f_req = $form_field[6];
 
		// get data
		$t = (isset($_POST[$f_fmname])) ? trim($_POST[$f_fmname]) : "";
		if (get_magic_quotes_gpc()) {
			$t = stripslashes($t);
		}
 
		// check for required fields
		if (($f_req == "true") && ($t == "")) {
			if ($f_fmname != $field_verification) { // has it's own check
				$errors[] = "Missing required field: '$f_name'.";
			}
		}
 
		if ($t != "") {
			$mail_message .= $f_name . " = \n" . $t . "\n\n";
		}
 
		break;
 
 
	case "checkbox":
 
		$f_name = $form_field[2];
 
		$t_message = $f_name . ' =';
 
		$f = FALSE;
		for ($i = 3; $i < count($form_field); $i+=3) {
			$f_fmname = $form_field[$i];
			$f_caption = $form_field[$i+1];
	
			$t = (isset($_POST[$f_fmname])) ? trim($_POST[$f_fmname]) : "";
			if ($t == 'on') {
				$t_message .= " " . $f_caption . ","; 
				$f = TRUE;
			}
		}
		
		$t_message = rtrim($t_message, ',');
 
		if ($f) {
			$mail_message .= $t_message . "\n\n";
		}
 
		break;
 
 
	case "radio":
 
		$f_name = $form_field[2];
		$f_fmname = $form_field[3];
 
		$t = (isset($_POST[$f_fmname])) ? trim($_POST[$f_fmname]) : "";
 
		$f = FALSE;
		for ($i = 5; $i < count($form_field); $i+=2) {
			if ($t == $form_field[$i]) {
				$f = TRUE;
			}
		}
 
		if ($f) {
			$mail_message .= $f_name . ' = ' . $t . "\n\n";
		}
 
		break;
 
 
	case "select":
 
		$f_name = $form_field[2];
		$f_fmname = $form_field[3];
 
		$t_message = $f_name . ' =';
 
		$t = (isset($_POST[$f_fmname])) ? $_POST[$f_fmname] : "";
 
		foreach ((array)$t as $tt) {
 
			$f = FALSE;		
			for ($i = 6; $i < count($form_field); $i++) {
				if (($form_field[$i] == $tt) && (trim($tt) != "")) {
					$f = TRUE;
				}
			}
			if ($f) {
				$t_message .= ' ' . $tt . ',';
			}
 
		}
 
 
		$t_message = rtrim($t_message, ',');
 
		if ($f) {
			$mail_message .= $t_message . "\n\n";
		}
 
		break;
 
 
	}
 
	}
 
 
	// if no errors, process
	if (empty($errors)) {
 
		$mail_message = trim($mail_message);
 
		// wrap messages if set
		if ($wrap_messages) {
			$mail_message = wordwrap($mail_message, 70);
		}
 
 
		// prepare the headers
		// \r\n seems to be the best method for most servers to handle
		$ip = $_SERVER["REMOTE_ADDR"];
		$mail_header = "";
		$mail_header .= "MIME-Version: 1.0\r\n"; 
		$mail_header .= "X-Sender-IP: $ip\r\n";
		$mail_header .= "Content-Type: text/plain\r\n";
		$mail_header .= "From: " . $mail_name . " <" . $mail_email . ">";
 
 
		if ($recipients[0] == 'onerecip') {
 
			$mail_to = $recipients[1];
 
		} elseif ($recipients[0] == 'mulrecip') {
 
			$mail_to = $recipients[2];
			for ($i = 3; $i < count($recipients); $i++) {
 
				$mail_header .= "\r\n";
				if ($recipients[1] == 'cc') {
					$mail_header .= "Cc: ";
				} else {
					$mail_header .= "Bcc: ";
				}
				$mail_header .= $recipients[$i];
			}
 
		} elseif ($recipients[0] == 'selrecip') {
 
			$recip_number = (int)$_POST[$field_dropdownrecip];
			foreach ($form_structure as $form_field) {
				if ($form_field[0] == 'selrecip') {
					$j = 1;
					for ($i = 4; $i < count($form_field); $i++) {
						if (strpos($form_field[$i], "#") === 0) {
							$i++;
						}
						$i++;
						if ($recip_number == $j) {
							$mail_to = $form_field[$i];
						}
						$j++;
					}
				}
			}
		} 
 
 
		// send mail, setting $email_sent to true or false
 
 
			if ($include_ip) {
				$mail_message .= "\n";
				$mail_message .= "\nHost: " . $_SERVER["HTTP_HOST"];
				$mail_message .= "\nIP: " . $_SERVER["REMOTE_ADDR"];
				$mail_message .= "\nBrowser: " . $_SERVER["HTTP_USER_AGENT"];
			}
 
 
		if (mail($mail_to, $mail_subject, $mail_message, $mail_header, "-f $mail_email")) {
			$email_sent = true; 
		} else {
			$errors[] = $msg_mailserver;
			$email_sent = false;
		}
 
	}
 
 
	if (isset($errors)) {
 
		// if there were errors, list them
 
		$t_out .= '<div class="mailwrapper"><div class="maildiv"><div class="fmerrorbox">';
		$t_out .= '<p class="fmerrortitle">' . $msg_error . '</p><ul>';
		
		foreach ($errors as $f)
			$t_out .= '<li>' . $f . '</li>';
 
		$t_out .= '</ul></div></div></div>';
 
	} else {
 
		// the message was sent, display message
 
 
		$t_out .= '<div class="mailwrapper"><div class="maildiv">';
 
		$t_out .= $msg_sent;
		
		$t_out .= '</div></div>';
 
		generate_verification();
 
	}
 
}
 
 
 
 
 
if (!$email_sent) {
 
// SHOW FORM
 
generate_verification();
 
if (!extension_loaded("gd")) {
   $t_out .= "<p><b>GD Support not detected!</b> GD is required for this script.</p>";
}
 
$t_out .= '<div class="mailwrapper"><div class="maildiv">';
$t_out .= '<form method="post" action="' . basename($_SERVER["PHP_SELF"]) . '" enctype="multipart/form-data">';
 
 
// go through fields
foreach ($form_structure as $form_field) {
 
 
	// process field types
	switch ($form_field[0]) {
 
 
	case "openfieldset":
 
		$f_class = $form_field[1];
 
		$t_out .= '<fieldset class="' . $f_class . '">';
		
		if (count($form_field) > 2) {
 
			$f_legend = $form_field[2];
 
			$t_out .= '<legend>' . $f_legend . '</legend><div class="legendspacing"></div>';
		}
 
		break;
 
 
	case "closefieldset":
 
		$t_out .= '</fieldset>';
 
		break;
 
 
	case "info":
 
		$f_class = $form_field[1];
		$f_text = $form_field[2];
 
		$t_out .= '<div class="' . $f_class . '">' . $f_text . '</div>';
	
		break;
 
 
	case "text":
 
		$f_class = $form_field[1];
		$f_name = $form_field[2];
		$f_fmname = $form_field[3];
		$f_size = $form_field[4];
		$f_max = $form_field[5];
		$f_req = $form_field[6];
 
		$t_out .= '<label class="sidelbl" for="' . $f_fmname . '">';
		if (($f_req == "true") && ($show_required)) 
			$t_out .= '<span class="fmrequired">* </span>';
		$t_out .= $f_name . '</label>';
 
		$t_out .= '<input type="text" class="' . $f_class . '" name="' . $f_fmname . '" 
					id="' . $f_fmname . '" size="' . $f_size . '" maxlength="' . $f_max . '"';
 
		if (isset($_POST[$f_fmname])) {
			if ($f_fmname != $field_verification) {
				$t_out .= ' value="' . htmlspecialchars($_POST[$f_fmname]) . '"';
			}
		}
		
		$t_out .= ' />';
		
		if ($f_fmname == $field_verification) {
			$t_out .= '<img class="ver_img" src="verify.php" width="60" height="20" alt="Please enter the number from this image" />';
		}
 
		$t_out .= '<br />';
 
		break;
 
 
 	case "password":
 
		$f_class = $form_field[1];
		$f_name = $form_field[2];
		$f_fmname = $form_field[3];
		$f_size = $form_field[4];
		$f_max = $form_field[5];
		$f_req = $form_field[6];
 
		$t_out .= '<label class="sidelbl" for="' . $f_fmname . '">';
		if (($f_req == "true") && ($show_required)) 
			$t_out .= '<span class="fmrequired">* </span>';
		$t_out .= $f_name . '</label>';
 
		$t_out .= '<input type="password" class="' . $f_class . '" name="' . $f_fmname . '" 
					id="' . $f_fmname . '" size="' . $f_size . '" maxlength="' . $f_max . '"';
 
		if (isset($_POST[$f_fmname])) {
			if ($f_fmname != $field_verification) {
				$t_out .= ' value="' . htmlspecialchars($_POST[$f_fmname]) . '"';
			}
		}
 
		$t_out .= ' /><br />';
 
		break;
 
 
	case "textarea":
 
		$f_class = $form_field[1];
		$f_name = $form_field[2];
		$f_fmname = $form_field[3];
		$f_rows = $form_field[4];
		$f_cols = $form_field[5];
		$f_req = $form_field[6];
 
		$t_out .= '<label class="sidelbl" for="' . $f_fmname . '">';
		if (($f_req == "true") && ($show_required)) 
			$t_out .= '<span class="fmrequired">* </span>';
		$t_out .= $f_name . '</label>';
 
		$t_out .= '<textarea class="' . $f_class . '" name="' . $f_fmname . '" 
					id="' . $f_fmname . '" rows="' . $f_rows . '" cols="' . $f_cols . '" >';
 
		if (isset($_POST[$f_fmname])) {
			if ($f_fmname != $field_verification) {
				$t_out .= htmlspecialchars($_POST[$f_fmname]);
			}
		}
		
		$t_out .= '</textarea><br />';
 
		break;
 
 
	case "checkbox":
 
		$f_class = $form_field[1];
		$f_name = $form_field[2];
 
		$t_out .= '<label class="sidelbl">' . $f_name . '</label>';
 
		for ($i = 3; $i < count($form_field)-1; $i+=3) {
			if ($i > 3) {
				$t_out .= '<label class="sidelbl">&nbsp;</label>';
			}
 
			$f_fmname = $form_field[$i];
			$f_caption = $form_field[$i+1];
			$f_checked = $form_field[$i+2];
 
			$t_out .= '<input class="' . $f_class . '" type="checkbox" name="' . $f_fmname . '" id="' . $f_fmname . '"';
 
			if ($first_load) {
				if ($f_checked == 'true') {
					$t_out .= ' checked="checked" ';
				}
			} else {
				if (isset($_POST[$f_fmname])) {
					if ($_POST[$f_fmname] == "on") {
						$t_out .= ' checked ';
					} 	
				} 
			}
 
			$t_out .= ' />';
			$t_out .= '<label for="' . $f_fmname . '">' . $f_caption . '</label> <br />';
		}
 
		break;
 
 
	case "radio":
 
		$f_class = $form_field[1];
		$f_name = $form_field[2];
		$f_fmname = $form_field[3];
		$f_def = $form_field[4];
 
		$t_out .= '<label class="sidelbl">' . $f_name . '</label>';
 
		$j = 1;
		for ($i = 5; $i < count($form_field); $i+=2) {
 
			$f_value = $form_field[$i];
			$f_caption = $form_field[$i+1];
 
			if ($i > 5) {
				$t_out .= '<label class="sidelbl">&nbsp;</label>';
			}
			$t_out .= '<input class="' . $f_class . '" type="radio" name="' . $f_fmname . '" id="' . $f_fmname . $j . '" value="' . $f_value . '"';
 
			
			if ($first_load) {
				if ((int)$f_def == $j) {
					$t_out .= ' checked="checked" ';
				}
			} else {
 
				if (isset($_POST[$f_fmname])) {
					if ($_POST[$f_fmname] == $f_value) {
						$t_out .= ' checked="checked" ';
					} 	
				} 
			}
 
			$t_out .= ' />';
			$t_out .= '<label for="' . $f_fmname . $j . '">' . $f_caption . '</label> <br />';
 
			$j++;
		}
 
		break;
 
 
	case "select":
 
		$f_class = $form_field[1];
		$f_name = $form_field[2];
		$f_fmname = $form_field[3];
		$f_mul = $form_field[4];
 
		$t_out .= '<label class="sidelbl" for="' . $f_fmname . '">' . $f_name . '</label>';
		$t_out .= '<select class="' . $f_class . '" name="' . $f_fmname;
		if ($f_mul === "true") {
			$t_out .= '[]';
		}	
		$t_out .= '" id="' . $f_fmname . '"';
		if ($f_mul === "true") {
			$t_out .= ' multiple="multiple" ';
		}
 
		$t_out .= '>';
 
		$grp_count = 0;
		for ($i = 5; $i < count($form_field)-1; $i++) {
 
			if (strpos($form_field[$i], "#") === 0) {
				
				if ($grp_count > 0) {
					$t_out .= "</optgroup>";
				}
 
				$t_out .= '<optgroup label="' . ltrim($form_field[$i], "#") . '">';
 
				$grp_count++;
				$i++;
			}
 
			$t_out .= '<option value="' . $form_field[$i] . '"';
 
			if ($f_mul == "true") {
				if (isset($_POST[$f_fmname])) {
					foreach ($_POST[$f_fmname] as $p) {
						if ($p == $form_field[$i]) {
							$t_out .= ' selected';
						} 
					}
				} 		
			} else {
				if (isset($_POST[$f_fmname])) {
					if ($_POST[$f_fmname] == $form_field[$i]) {
						$t_out .= ' selected';
					} 	
				} 		
			}
 
 
			$t_out .= ' > ' . $form_field[$i+1] . '</option>';
 
			$i++;
 
		}
 
		if ($grp_count > 0) {
			$t_out .= "</optgroup>";
		}
		$t_out .= '</select><br />';
 
		break;
 
 
	case "selrecip":
 
		$f_class = $form_field[1];
		$f_name = $form_field[2];
		$f_fmname = $form_field[3];
 
		$t_out .= '<label class="sidelbl" for="' . $f_fmname . '">' . $f_name . '</label>';
		$t_out .= '<select class="' . $f_class . '" name="' . $f_fmname . '" id="' . $f_fmname . '" >';
 
 
		$j = 1;
		$grp_count = 0;
		for ($i = 4; $i < count($form_field)-1; $i++) {
 
			if (strpos($form_field[$i], "#") === 0) {
				
				if ($grp_count > 0) {
					$t_out .= "</optgroup>";
				}
 
				$t_out .= '<optgroup label="' . ltrim($form_field[$i], "#") . '">';
 
				$grp_count++;
				$i++;
			}
 
			$t_out .= '<option value="' . $j . '"';
		
 
			if (isset($_POST[$f_fmname])) {
				if ($_POST[$f_fmname] == $j) {
					$t_out .= ' selected';
				} 	
			} 		
 
			$t_out .= ' > ' . $form_field[$i] . '</option>';
 
			$i++;
			$j++;
		}
 
		if ($grp_count > 0) {
			$t_out .= "</optgroup>";
		}
		$t_out .= '</select><br />';
 
		break;
 
	}	
}
 
$t_out .= '<div class="fmspacer"><!-- --></div>';
 
$t_out .= '<div align="center" class="fmsubmit"><input name="form_submitted" type="image" id="Submit" img src="send.gif" value="' . $msg_submit . '" /></div></div>';
 
$t_out .= '</form></div></div>';
 
}
 
echo $t_out;
 
?>

Open in new window

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24054248
Line 630 is the start of the code dealing with creating the checkboxes.

Storing the $fmnames that you want for the "All" would be needed there.

But you are essentially hacking it.

Personally, now that I see the code, I'd be using Terry's idea.
0
 
LVL 26

Author Comment

by:dannywareham
ID: 24054260
I don't mind hacking it or using JavaScript if I have to - but I'm a noob at both.
Give me a database any day!

If you can help, I have a few more points I can allocate :-)



0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24054404
I use Prototype (http://prototypejs.org).

So, add this to your <head>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"></script>
<script type="text/javascript">
document.observe('dom:loaded', function(){
 $$('input[type="checkbox"][name="All"]').invoke('click', function(ev){
  $$('input[type="checkbox"]').each(function(cb){
   cb.checked = true;
  });
 $$('input[type="checkbox"][name="All"]').checked = false;
});
</script>


I think. UNTESTED
0
 
LVL 26

Author Comment

by:dannywareham
ID: 24054462
No joy :-(
You can see it at http://www.datamouse.biz/client_work/stairlift/index2.php if that helps?
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24054478
In English ...


Include the Prototype library from the Google AJAX libraries. Has all the caching logi to make sure the client only needs to download it once no matter how many different sites use it.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"></script>


Your JS.

<script type="text/javascript">



We don't want to interact with the DOM until it has finished loading.

document.observe
      (
      'dom:loaded',
      function()
            {


Find the checkbox with the name of All and attach a click event observer to it.

            $$('input[type="checkbox"][name="All"]')
                  .invoke
                        (
                        'click',
                        function(ev)
                              {


Stop processing the click event which could mess things up.

                              ev.stop();


Look for all checkboxes and iterate them.

                              $$('input[type="checkbox"]')
                                    .each
                                          (
                                          function(cb)
                                                {


Set the checked property on the checkbox.
                                                cb.checked = true;
                                                }
                                          );

Turn off the All check. Looks tidy I think.
      
                              $$('input[type="checkbox"][name="All"]')
                                    .checked = false;
                              }
                        );
            }
      );
</script>
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24054488
Try this.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"></script>
<script type="text/javascript">
document.observe
  (
  'dom:loaded',
  function()
    {
    $$('input[type="checkbox"][name="All"]')
      .invoke
        (
        'click',
        function(ev)
          {
          ev.stop();
          $$('input[type="checkbox"]')
            .each
              (
              function(cb)
                {
                cb.checked = true;
                }
              );
          $$('input[type="checkbox"][name="All"]')
            .checked = false;
          }
        );
    }
  );
</script>

Open in new window

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24054523
Ha. I did say untested. Just using Firefox + Firebug and got the error ...

missing } after function body
});\n


which is expected. The snippet above SHOULD be better.
0
 
LVL 26

Author Comment

by:dannywareham
ID: 24054585
Still no joy.

As a test, I changed the name to "Acorn" - no joy with that either :-(

Thanks for your help - I really do appreciate it
0
 
LVL 26

Author Comment

by:dannywareham
ID: 24054590
Points doubled
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24054634
Ha. The checkbox for All isn't called All.

It's called fm_from

Change the "All" to "fm_from" in the JS.
0
 
LVL 26

Author Comment

by:dannywareham
ID: 24054652
Nope...
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24054691
Try this.

I forgot what I was invoking. Sorry.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"></script>
<script type="text/javascript">
document.observe
  (
  'dom:loaded',
  function()
    {
    $$('input[type="checkbox"][name="fm_from"]')
      .invoke
        (
        'observe',
        'click',
        function(ev)
          {
          ev.stop();
          $$('input[type="checkbox"]')
            .each
              (
              function(cb)
                {
                cb.checked = true;
                }
              );
          $$('input[type="checkbox"][name="fm_from"]')
            .checked = false;
          }
        );
    }
  );
</script>

Open in new window

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24054719
Finally...
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"></script>
<script type="text/javascript">
document.observe
  (
  'dom:loaded',
  function()
    {
    $$('input[type="checkbox"][name="fm_from"]')
      .invoke
        (
        'observe',
        'click',
        function(ev)
          {
          ev.stop();
          $$('input[type="checkbox"]')
            .each
              (
              function(cb)
                {
                cb.checked = (cb.name != "fm_from");
                }
              );
          }
        );
    }
  );
</script>

Open in new window

0
 
LVL 26

Author Comment

by:dannywareham
ID: 24054742
Almost there!

When the ALL is clicked, all the others check (as planned).
However, the ALL check box is still unticked.
Can we have this ticked too? Is it possible?
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24054785
Yes. But All is the others isn't it?

If you are sure, change ...

  cb.checked = (cb.name != "fm_from");

to ...

  cb.checked = true;

0
 
LVL 26

Author Comment

by:dannywareham
ID: 24054821
Thanks Richard.
It doesn't check it - but I get your point...
:-)
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 100 total points
ID: 24054898
Ha. Of course. Sorry.



<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"></script>
<script type="text/javascript">
document.observe
  (
  'dom:loaded',
  function()
    {
    $$('input[type="checkbox"][name="fm_from"]')
      .invoke
        (
        'observe',
        'click',
        function(ev)
          {
          ev.stop();
          $$('input[type="checkbox"]')
            .each
              (
              function(cb)
                {
                if (cb.name != "fm_from")
                  {
                  cb.checked = true;
                  }
                }
              );
          }
        );
    }
  );
</script>

Open in new window

0
 
LVL 26

Author Comment

by:dannywareham
ID: 24054932
Nope - still the same :-)
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24054961
Drop the ev.stop();
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24055113
Remove the line ...

ev.stop();


0
 
LVL 26

Author Comment

by:dannywareham
ID: 24055149
Perfick
0
 
LVL 26

Author Closing Comment

by:dannywareham
ID: 31565297
Superb. Thanks for staying with it. You've saved my hair!
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question