How to do it with jQuery:
, js, - , ( ), , .
Useless requests HTTP- (form submit), , , .
$(document).ready(function() {
$('form').submit(function() {
if ($('#checkme').is(':checked')) {
return true;
} else {
alert('You must check the Approve checkbox to continue!');
return false;
}
});
});
. <form> , PHP.
, tomhallam, tomhallam, , , , jQuery.
PHP:
, PHP . , , Javascript , .
<?php
function POST( $key ) {
if ( isset( $_POST[$key] )) return $_POST[$key];
else return '';
}
if ( isset( $_POST['checkme'] )) {
mySql_insert_formdata( $_POST );
$_POST['error'] = 'Form processed, thanks for your data';
} elseif ( isset($_POST['submitted'])) {
$_POST['error'] = 'Checkme must be cheked first!';
}
echo POST('error');
?>
<form action="forms.php" method="post">
<!-- Hidden field to check at server if form is submitted -->
<input type="hidden" name="submitted" value="anything" />
<!-- Some fields for user to fill, POST() inserts previous value if any -->
<label for="email">eMail</label>
<input type="email" id="email" name="email" value="<?php echo POST('email'); ?>" /><br/>
<!-- Checkbox that must be checked -->
<input type="checkbox" id="checkme" name="checkme" />
<label for="checkme">Approve</label><br/>
<!-- And submit button -->
<input type="submit" value="Submit" />
</form>
:
Javascript ( ) , javascript.
PHP ( ) , / .
, , , , Javascript, .
JS, , , , "try again".