Skip to content

Commit c6bc3de

Browse files
committed
First commit
0 parents  commit c6bc3de

File tree

4 files changed

+255
-0
lines changed

4 files changed

+255
-0
lines changed

LICENSE.md

Whitespace-only changes.

README.md

Whitespace-only changes.

index.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2+
<html>
3+
<head>
4+
<title>jquery-maxsubmit plugin demo</title>
5+
<meta name="Generator" content="EditPlus">
6+
<meta name="Author" content="Jason Judge">
7+
<meta name="Keywords" content="jquery forms get post http php">
8+
<meta name="Description" content="">
9+
10+
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
11+
<script type="text/javascript" src="jquery.maxsubmit.js"></script>
12+
<script type="text/javascript">
13+
jQuery(document).ready(function($) {
14+
$('form#form1').maxSubmit({
15+
max_count: 2,
16+
max_exceeded_message: "This form has too many fields.\n\n"
17+
+ " Found {form_count} fields, so with a maximum of {max_count} supported by the server, some data will be lost.\n\n"
18+
+ " You may continue and submit, or cancel."
19+
});
20+
});
21+
</script>
22+
</head>
23+
24+
<?php
25+
$input = array(
26+
'text1' => 'Text 1',
27+
'text2' => 'Text 2',
28+
'checkbox1' => 'on',
29+
'checkbox2' => '',
30+
);
31+
32+
foreach($input as $key => $value) {
33+
$input[$key] = (isset($_POST[$key]) ? $_POST[$key] : '' );
34+
}
35+
36+
$input = array_merge(
37+
$input,
38+
array(
39+
'select1' =>
40+
array(
41+
'value1' => '',
42+
'value2' => '',
43+
'value3' => '',
44+
),
45+
)
46+
);
47+
48+
if (!empty($_POST['select1'])) {
49+
foreach($_POST['select1'] as $key => $value) {
50+
$input['select1'][$value] = $value;
51+
}
52+
}
53+
54+
/**
55+
* Get the submission limit.
56+
* Returns the lowest limit or false if no limit can be found.
57+
* An alternate default can be provided if required.
58+
* CHECKME: do we need to separate GET and POST limits, as they may apply
59+
* to different forms. The larger number of parameters is like to only
60+
* apply to POST forms, so POST is important. The REQUEST max vars is
61+
* another thing to consider, as it will be the sum of GET and POST parameters.
62+
*/
63+
function getFormSubmissionLimit($default = false)
64+
{
65+
// All these ini settings will affect the number of parameters that can be
66+
// processed. Check them all to find the lowest.
67+
$ini = array();
68+
$ini[] = ini_get('max_input_vars');
69+
$ini[] = ini_get('suhosin.get.max_vars');
70+
$ini[] = ini_get('suhosin.post.max_vars');
71+
$ini[] = ini_get('suhosin.request.max_vars');
72+
73+
$ini = array_filter($ini, 'is_numeric');
74+
75+
$lowest_limit = min($ini);
76+
77+
return ($lowest_limit === false ? $default : $lowest_limit);
78+
}
79+
?>
80+
81+
<body>
82+
<h1>Max Submit</h1>
83+
84+
<p>
85+
The real server form submission parameter limit is <?php echo getFormSubmissionLimit('{not defined}'); ?>.
86+
For these tests, we will set the limit to 2, so the confirm message is always shown.
87+
</p>
88+
89+
<form method="post" id="form1">
90+
<h2>Mandatory form items: will count as one submitted parameter each</h2>
91+
92+
<p>
93+
<input type="text" name="text1" value="<?php echo $input['text1']; ?>" />
94+
</p>
95+
96+
<p>
97+
<input type="text" name="text2" value="<?php echo $input['text2']; ?>" />
98+
</p>
99+
100+
<p>
101+
<select name="select2">
102+
<option value="value1">Value 1</option>
103+
</select>
104+
</p>
105+
106+
<p>
107+
<input type="radio" name="radio1" value="value1" checked />
108+
<input type="radio" name="radio1" value="value2" />
109+
<input type="radio" name="radio1" value="value3" />
110+
Radio 1
111+
</p>
112+
113+
<p>
114+
<input type="radio" name="radio2" value="value1" checked />
115+
<input type="radio" name="radio2" value="value2" />
116+
<input type="radio" name="radio2" value="value3" />
117+
Radio 2
118+
</p>
119+
120+
<hr />
121+
122+
<h2>Optional form items: will count as zero, one or more parameters</h2>
123+
124+
<p>
125+
<label><input type="checkbox" name="checkbox1" <?php echo ($input['checkbox1'] == 'on' ? 'checked="checked"' : ''); ?> /> Checkbox 1</label>
126+
</p>
127+
128+
<p>
129+
<label><input type="checkbox" name="checkbox2" <?php echo ($input['checkbox2'] == 'on' ? 'checked="checked"' : ''); ?> /> Checkbox 2</label>
130+
</p>
131+
132+
<p>
133+
<select name="select1[]" multiple="multiple">
134+
<?php foreach($input['select1'] as $key => $value) { ?>
135+
<option value="<?php echo "$key"; ?>" <?php echo ($value ? "selected='selected'" : "") ?>><?php echo $key; ?></option>
136+
<?php } ?>
137+
</select> (counts as up to three parameters)
138+
</p>
139+
140+
<p>
141+
<input type="submit" value="Submit" /> (also a mandatory submitted parameter)
142+
</p>
143+
</form>
144+
</body>
145+
</html>

jquery.maxsubmit.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Copyright 2013 Academe Computing Ltd
3+
* Released under the MIT license
4+
* Author: Jason Judge <[email protected]>
5+
*/
6+
/**
7+
* jquery.maxsubmit.js
8+
*
9+
* Checks how many parameters a form is going to submit, and
10+
* gives the user a chance to cancel if it exceeds a set number.
11+
* PHP5.3+ has limits set by default on the number of POST parameters
12+
* that will be accepted. Parameters beyond that number, usually 1000,
13+
* will be silently discarded. This can have nasty side-effects in some
14+
* applications, such as editiong shop products with many variations
15+
* against a product, which can result in well over 1000 submitted
16+
* parameters (looking at you WooCommerce). This aims to provide some
17+
* level of protection.
18+
*
19+
* TODO: we should look at GET variables on the form URL, because they also
20+
* get counted on the "suhosin.request.max_vars" setting, if used.
21+
*/
22+
23+
(function($) {
24+
$.fn.maxSubmit = function(options) {
25+
// this.each() is the wrapper for each selected group of checkboxes.
26+
return this.each(function() {
27+
28+
var settings = $.extend({
29+
// The maximum number of parameters the form will be allowed to submit
30+
// before the user is issued a confirm (OK/Cancel) dialogue.
31+
32+
max_count: 1000,
33+
34+
// The message given to the user to confirm they want to submit anyway.
35+
// Can use {max_count} as a placeholder for the permitted maximum
36+
// and {form_count} for the counted form items.
37+
38+
max_exceeded_message: 'This form has too many fields for the server to accept.\n'
39+
+ ' Data may be lost if you submit. Are you sure you want to go ahead?',
40+
41+
// The function that will display the confirm message.
42+
// Replace this with something fancy such as jquery.ui if you wish.
43+
44+
confirm_display: function(form_count = '') {
45+
return confirm(
46+
settings
47+
.max_exceeded_message
48+
.replace("{max_count}", settings.max_count)
49+
.replace("{form_count}", form_count)
50+
);
51+
}
52+
}, options);
53+
54+
// Form elements will be passed in, so we need to trigger on
55+
// an attempt to submit that form.
56+
57+
// First check we do have a form.
58+
if ($(this).is("form")) {
59+
$(this).on('submit', function(e) {
60+
// We have a form, so count up the form items that will be
61+
// submitted to the server.
62+
63+
// Text fields and submit buttons will all post one parameter.
64+
var form_count = $('input:text, input:submit, input:password, textarea', this).length;
65+
66+
// Checkboxes will post only if checked.
67+
$('input:checkbox', this).each(function() {
68+
if (this.checked) form_count++;
69+
});
70+
71+
// Single-select lists will always post one value.
72+
$('select:not([multiple])', this).each(function() {
73+
form_count++;
74+
});
75+
76+
// Multi-select lists will post one parameter for each selected item.
77+
$('select[multiple]', this).each(function() {
78+
// The select item value is null if no options are selected.
79+
var select = $(this).val();
80+
if (select !== null) form_count += select.length;
81+
});
82+
83+
// Each radio button group will post one parameter.
84+
// Count the radio groups
85+
var rgroups = [];
86+
$('input:radio').each(function(index, el) {
87+
var i;
88+
for(i = 0; i < rgroups.length; i++) {
89+
if (rgroups[i] == $(el).attr('name')) return;
90+
}
91+
rgroups.push($(el).attr('name'));
92+
});
93+
form_count += rgroups.length;
94+
95+
if (form_count > settings.max_count) {
96+
// If the user cancels, then abort the form submit.
97+
if (!settings.confirm_display(form_count)) return false;
98+
}
99+
100+
// Allow the submit to go ahead.
101+
return true;
102+
});
103+
}
104+
105+
// Support chaining.
106+
return this;
107+
});
108+
};
109+
}(jQuery));
110+

0 commit comments

Comments
 (0)