Project

General

Profile

« Previous | Next » 

Revision 760

Added by Matthias over 16 years ago

Updated form modul for valid output, removed all javascript (Big thanks to BerndJM)

View differences:

trunk/CHANGELOG
10 10
# = Bugfix
11 11
! = Update/Change
12 12

  
13
------------------------------------- 2.7.0 -------------------------------------
14
17-Mar-2008 Thomas Hornik
15
#	fixed an issue with utf8_check() (too slow with many ~100KB pages)
16
15-Mar-2008 Thomas Hornik
17
#	menulink: link can be menulink or subpage of menulink, too
18
#	call_captcha: added parameter 'style', fixed some issue with parameter 'image' and text-style captchas
13
------------------------------------- 2.7.0 -------------------------------------
14
23-Mar-2008 Matthias Gallas
15
! 	Updated form modul for valid output, removed all javascript (Big thanks to BerndJM)
16
17-Mar-2008 Thomas Hornik
17
#	fixed an issue with utf8_check() (too slow with many ~100KB pages)
18
15-Mar-2008 Thomas Hornik
19
#	menulink: link can be menulink or subpage of menulink, too
20
#	call_captcha: added parameter 'style', fixed some issue with parameter 'image' and text-style captchas
19 21
13-Mar-2008 Christian Sommer
20 22
! 	adjusted error/warning templates to the updated backend layout
21 23
13-Mar-2008 Matthias Gallas
trunk/wb/modules/form/view.php
51 51
	} elseif ($n == ']') {
52 52
		$n = '</optgroup>';
53 53
	} else {
54
		if(in_array($n, $values))
54
		if(in_array($n, $values)) {
55 55
			$n = '<option selected="selected" value="'.$n.'">'.$n.'</option>';
56
		else
56
		} else {
57 57
			$n = '<option value="'.$n.'">'.$n.'</option>';
58
		}
58 59
	}
59 60
}
60 61
}
......
63 64
function make_checkbox(&$n, $idx, $params) {
64 65
	$field_id = $params[0][0];
65 66
	$seperator = $params[0][1];
66
	//$n = '<input class="field_checkbox" type="checkbox" id="'.$n.'" name="field'.$field_id.'" value="'.$n.'">'.'<font class="checkbox_label" onclick="javascript: document.getElementById(\''.$n.'\').checked = !document.getElementById(\''.$n.'\').checked;">'.$n.'</font>'.$seperator;
67
	if(in_array($n, $params[1]))
68
		$n = '<input class="field_checkbox" type="checkbox" id="'.$n.'" name="field'.$field_id.'['.$idx.']" value="'.$n.'" checked="checked">'.'<font class="checkbox_label" onclick="javascript: document.getElementById(\''.$n.'\').checked = !document.getElementById(\''.$n.'\').checked;">'.$n.'</font>'.$seperator;
69
	else
70
		$n = '<input class="field_checkbox" type="checkbox" id="'.$n.'" name="field'.$field_id.'['.$idx.']" value="'.$n.'">'.'<font class="checkbox_label" onclick="javascript: document.getElementById(\''.$n.'\').checked = !document.getElementById(\''.$n.'\').checked;">'.$n.'</font>'.$seperator;
67
	$label_id = 'wb_'.str_replace(" ", "_", $n);
68
	if(in_array($n, $params[1])) {
69
		$n = '<input class="field_checkbox" type="checkbox" id="'.$label_id.'" name="field'.$field_id.'['.$idx.']" value="'.$n.'" checked="checked" />'.'<label for="'.$label_id.'" class="checkbox_label">'.$n.'</lable>'.$seperator;
70
	} else {
71
		$n = '<input class="field_checkbox" type="checkbox" id="'.$label_id.'" name="field'.$field_id.'['.$idx.']" value="'.$n.'" />'.'<label for="'.$label_id.'" class="checkbox_label">'.$n.'</label>'.$seperator;
72
	}	
71 73
}
72 74
}
73 75
// Function for generating a radio button
......
76 78
	$field_id = $params[0];
77 79
	$group = $params[1];
78 80
	$seperator = $params[2];
79
	if($n == $params[3])
80
		$n = '<input class="field_radio" type="radio" id="'.$n.'" name="field'.$field_id.'" value="'.$n.'" checked="checked">'.'<font class="radio_label" onclick="javascript: document.getElementById(\''.$n.'\').checked = true;">'.$n.'</font>'.$seperator;
81
	else
82
		$n = '<input class="field_radio" type="radio" id="'.$n.'" name="field'.$field_id.'" value="'.$n.'">'.'<font class="radio_label" onclick="javascript: document.getElementById(\''.$n.'\').checked = true;">'.$n.'</font>'.$seperator;
81
	$label_id = 'wb_'.str_replace(" ", "_", $n);
82
	if($n == $params[3]) { 
83
		$n = '<input class="field_radio" type="radio" id="'.$label_id.'" name="field'.$field_id.'" value="'.$n.'" checked="checked" />'.'<label for="'.$label_id.'" class="radio_label">'.$n.'</label>'.$seperator;
84
	} else {
85
		$n = '<input class="field_radio" type="radio" id="'.$label_id.'" name="field'.$field_id.'" value="'.$n.'" />'.'<label for="'.$label_id.'" class="radio_label">'.$n.'</label>'.$seperator;
86
	}
83 87
}
84 88
}
85 89
// Generate temp submission id
......
117 121
	$footer = '';
118 122
}
119 123

  
120
$java_fields = '';
121
$java_titles = '';
122
$java_tween = ''; // I know kinda stupid, anyone better idea?
123
$java_mailcheck = '';
124

  
125
// Add form starter code
126 124
?>
127
<form name="form" onsubmit="return formCheck(this);" action="<?php echo htmlspecialchars(strip_tags($_SERVER['PHP_SELF'])); ?>" method="post">
125
<form name="form" action="<?php echo htmlspecialchars(strip_tags($_SERVER['PHP_SELF'])); ?>" method="post">
128 126
<input type="hidden" name="submission_id" value="<?php echo $_SESSION['form_submission_id']; ?>" />
129 127
<?php
130

  
131
// Print header
132
echo $header;
133

  
134 128
if(ENABLED_ASP) { // first add some honeypot-fields
135 129
?>
136 130
<input type="hidden" name="submitted_when" value="<?php $t=time(); echo $t; $_SESSION['submitted_when']=$t; ?>" />
......
146 140
<input id="url" name="url" size="61" value="" /><br />
147 141
Comment:
148 142
<label for="comment">Enter not your comment here:</label>
149
<textarea name="comment" cols="50" rows="10"></textarea><br />
143
<textarea id="comment" name="comment" cols="50" rows="10"></textarea><br />
150 144
</p>
145

  
151 146
<?php }
152 147

  
148
// Print header
149
echo $header;
150

  
153 151
// Get list of fields
154 152
$query_fields = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' ORDER BY position ASC");
153

  
155 154
if($query_fields->numRows() > 0) {
156 155
	while($field = $query_fields->fetchRow()) {
157 156
		// Set field values
......
159 158
		$value = $field['value'];
160 159
		// Print field_loop after replacing vars with values
161 160
		$vars = array('{TITLE}', '{REQUIRED}');
162
		$values = array($field['title']);
163
		if($field['required'] == 1) {
164
			$values[] = '<font class="required">*</font>';
165
			$java_fields .= $java_tween.'"field'.$field_id.'"';
166
			$java_titles .= $java_tween.'"'.$field['title'].'"';
167
			$java_tween = ', ';
161
		if (($field['type'] == "radio") || ($field['type'] == "checkbox")) {
162
			$field_title = $field['title'];
168 163
		} else {
164
			$field_title = '<label for="field'.$field_id.'">'.$field['title'].'</label>';
165
		}
166
		$values = array($field_title);
167
		if ($field['required'] == 1) {
168
			$values[] = '<span class="required">*</span>';
169
		} else {
169 170
			$values[] = '';
170 171
		}
171 172
		if($field['type'] == 'textfield') {
......
173 174
			$values[] = '<input type="text" name="field'.$field_id.'" id="field'.$field_id.'" maxlength="'.$field['extra'].'" value="'.(isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:$value).'" class="textfield" />';
174 175
		} elseif($field['type'] == 'textarea') {
175 176
			$vars[] = '{FIELD}';
176
			$values[] = '<textarea name="field'.$field_id.'" id="field'.$field_id.'" class="textarea">'.(isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:$value).'</textarea>';
177
			$values[] = '<textarea name="field'.$field_id.'" id="field'.$field_id.'" class="textarea" cols="25" rows="5">'.(isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:$value).'</textarea>';
177 178
		} elseif($field['type'] == 'select') {
178 179
			$vars[] = '{FIELD}';
179 180
			$options = explode(',', $value);
180 181
			array_walk($options, 'make_option', (isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:array()));
181
			$field['extra'] = explode(',',$field['extra']); 
182
			$values[] = '<select name="field'.$field_id.'[]" id="field'.$field_id.'" size="'.$field['extra'][0].'" '.$field['extra'][1].' class="select">'.implode($options).'</select>';
182
			$field['extra'] = explode(',',$field['extra']);
183
			$values[] = '<select name="field'.$field_id.'[]" id="field'.$field_id.'" size="'.$field['extra'][0].'" '.$field['extra'][1].'="'.$field['extra'][1].'" class="select">'.implode($options).'</select>';
183 184
		} elseif($field['type'] == 'heading') {
184 185
			$vars[] = '{FIELD}';
185 186
			$values[] = '<input type="hidden" name="field'.$field_id.'" id="field'.$field_id.'" value="===['.$field['title'].']===" />';
......
199 200
			$values[] = implode($options);
200 201
		} elseif($field['type'] == 'email') {
201 202
			$vars[] = '{FIELD}';
202
			$values[] = '<input type="text" name="field'.$field_id.'" onChange="return checkmail(this.form.field'.$field_id.')"  id="field'.$field_id.'" value="'.(isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:'').'" maxlength="'.$field['extra'].'" class="email" />';
203
			$java_mailcheck .= 'onChange="return checkmail(this.form'.$field_id.'" ';
203
			$values[] = '<input type="text" name="field'.$field_id.'" id="field'.$field_id.'" value="'.(isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:'').'" maxlength="'.$field['extra'].'" class="email" />';
204 204
		}
205 205
		if(isset($_SESSION['field'.$field_id])) unset($_SESSION['field'.$field_id]);
206 206
		if($field['type'] != '') {
......
218 218
	</tr>
219 219
	<?php
220 220
}
221
echo '
222
<script language="JavaScript">
223
<!--
224 221

  
225
/***********************************************
226
* Required field(s) validation v1.10- By NavSurf
227
* Visit Nav Surf at http://navsurf.com
228
* Visit http://www.dynamicdrive.com/ for full source code
229
***********************************************/
230

  
231
function formCheck(formobj){
232
	// Enter name of mandatory fields
233
	var fieldRequired = Array('.$java_fields.');
234
	// Enter field description to appear in the dialog box
235
	var fieldDescription = Array('.$java_titles.');
236
	// dialog message
237
	var alertMsg = "'.$MESSAGE['MOD_FORM']['REQUIRED_FIELDS'].':\n";
238
	
239
	var l_Msg = alertMsg.length;
240
	
241
	for (var i = 0; i < fieldRequired.length; i++){
242
		var obj = formobj.elements[fieldRequired[i]];
243
		if (obj){
244
			switch(obj.type){
245
			case "select-one":
246
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
247
					alertMsg += " - " + fieldDescription[i] + "\n";
248
				}
249
				break;
250
			case "select-multiple":
251
				if (obj.selectedIndex == -1){
252
					alertMsg += " - " + fieldDescription[i] + "\n";
253
				}
254
				break;
255
			case "text":
256
			case "textarea":
257
				if (obj.value == "" || obj.value == null){
258
					alertMsg += " - " + fieldDescription[i] + "\n";
259
				}
260
				break;
261
			default:
262
			}
263
			if (obj.type == undefined){
264
				var blnchecked = false;
265
				for (var j = 0; j < obj.length; j++){
266
					if (obj[j].checked){
267
						blnchecked = true;
268
					}
269
				}
270
				if (!blnchecked){
271
					alertMsg += " - " + fieldDescription[i] + "\n";
272
				}
273
			}
274
		}
275
	}
276

  
277
	if (alertMsg.length == l_Msg){
278
		return true;
279
	}else{
280
		alert(alertMsg);
281
		return false;
282
	}
283
}
284
/***********************************************
285
* Email Validation script- ? Dynamic Drive (www.dynamicdrive.com)
286
* This notice must stay intact for legal use.
287
* Visit http://www.dynamicdrive.com/ for full source code
288
***********************************************/
289

  
290
var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
291

  
292
function checkmail(e){
293
var returnval=emailfilter.test(e.value);
294
if (returnval==false){
295
alert("Please enter a valid email address.");
296
e.select();
297
}
298
return returnval;
299
}
300
-->
301

  
302
</script>';
303

  
304

  
305 222
// Print footer
306 223
echo $footer;
307 224

  
......
384 301
				// Add to message body
385 302
				if($field['type'] != '') {
386 303
					if(!empty($_POST['field'.$field['field_id']])) {
387
						if(isset($captcha_error)) $_SESSION['field'.$field['field_id']] = htmlspecialchars($_POST['field'.$field['field_id']]);
304
						if (is_array($_POST['field'.$field['field_id']])) {
305
							$_SESSION['field'.$field['field_id']] = $_POST['field'.$field['field_id']];
306
						} else {
307
							$_SESSION['field'.$field['field_id']] = htmlspecialchars($_POST['field'.$field['field_id']]);
308
						}
388 309
						if($field['type'] == 'email' AND $admin->validate_email($_POST['field'.$field['field_id']]) == false) {
389 310
							$email_error = $MESSAGE['USERS']['INVALID_EMAIL'];
390 311
						}
......
405 326
				}
406 327
			}
407 328
		}
408

  
409
		// Addslashes to email body - proposed by Icheb in topic=1170.0
410
		// $email_body = $wb->add_slashes($email_body);
411
		
329
	
412 330
		// Check if the user forgot to enter values into all the required fields
413 331
		if($required != array()) {
414 332
			if(!isset($MESSAGE['MOD_FORM']['REQUIRED_FIELDS'])) {
......
420 338
			foreach($required AS $field_title) {
421 339
				echo '<li>'.$field_title;
422 340
			}
423
			if(isset($email_error)) { echo '<li>'.$email_error.'</li>'; }
424
			if(isset($captcha_error)) { echo '<li>'.$captcha_error.'</li>'; }
425
			echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
426
			
341
			if(isset($email_error)) {
342
				echo '<li>'.$email_error.'</li>';
343
			}
344
			if(isset($captcha_error)) {
345
				echo '<li>'.$captcha_error.'</li>';
346
			}
347
			echo '</ul><a href="'.htmlspecialchars(strip_tags($_SERVER['PHP_SELF'])).'">'.$TEXT['BACK'].'</a>';
427 348
		} else {
428
			
429 349
			if(isset($email_error)) {
430 350
				echo '<br /><ul>';
431 351
				echo '<li>'.$email_error.'</li>';
432
				echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
352
				echo '</ul><a href="'.htmlspecialchars(strip_tags($_SERVER['PHP_SELF'])).'">'.$TEXT['BACK'].'</a>';
433 353
			} elseif(isset($captcha_error)) {
434 354
				echo '<br /><ul>';
435 355
				echo '<li>'.$captcha_error.'</li>';
436
				echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
356
				echo '</ul><a href="'.htmlspecialchars(strip_tags($_SERVER['PHP_SELF'])).'">'.$TEXT['BACK'].'</a>';
437 357
			} else {
438
				
439 358
				// Check how many times form has been submitted in last hour
440 359
				$last_hour = time()-3600;
441 360
				$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions WHERE submitted_when >= '$last_hour'");
......
500 419
	
501 420
	// Now check if the email was sent successfully
502 421
	if(isset($success) AND $success == true) {
503
	    if ($success_page=='none') {
422
	   if ($success_page=='none') {
504 423
			echo str_replace("\n","<br />",$success_email_text);
505 424
  		} else {
506 425
			$query_menu = $database->query("SELECT link,target FROM ".TABLE_PREFIX."pages WHERE `page_id` = '$success_page'");
507 426
			if($query_menu->numRows() > 0) {
508
  	         	$fetch_settings = $query_menu->fetchRow();
509
			    $link = WB_URL.PAGES_DIRECTORY.$fetch_settings['link'].PAGE_EXTENSION;
510
			    echo "<script type='text/javascript'>location.href='".$link."';</script>";
427
  	        	$fetch_settings = $query_menu->fetchRow();
428
			   $link = WB_URL.PAGES_DIRECTORY.$fetch_settings['link'].PAGE_EXTENSION;
429
			   echo "<script type='text/javascript'>location.href='".$link."';</script>";
511 430
			}    
512 431
		}
432
		// clearing session on success
433
		$query_fields = $database->query("SELECT field_id FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' AND required = 1");
434
		while($field = $query_fields->fetchRow()) {
435
			$field_id = $field[0];
436
			if(isset($_SESSION['field'.$field_id])) unset($_SESSION['field'.$field_id]);
437
		}
513 438
	} else {
514
		echo '<br />'.$TEXT['ERROR'];
515
	}
516
	
439
		if(isset($success) AND $success == false) {
440
			echo $TEXT['ERROR'];
441
		}
442
	}	
517 443
}
518 444

  
519
?>
445
?>

Also available in: Unified diff