Skip to content

Instantly share code, notes, and snippets.

@kas-cor
Last active October 6, 2015 09:16
Show Gist options
  • Save kas-cor/ff252f61014f9815d159 to your computer and use it in GitHub Desktop.
Save kas-cor/ff252f61014f9815d159 to your computer and use it in GitHub Desktop.
Check form
<form action="server.php" method="POST" id="contact_form">
<input type="hidden" name="check_filelds" value="email,name,text" />
<input type="hidden" name="check_form" value="" />
<input type="text" name="email" value="" />
<input type="text" name="name" value="" />
<textarea name="text" rows="4" cols="20"></textarea>
<input type="button" name="send" value="send" onclick="checkAndSendForm($('#contact_form'))"/>
</form>
function checkAndSendForm(objform) {
$.post("/data.php", "act=check_form&" + $(objform).serialize(), function (res) {
$("input[name=check_form]").val(res);
$(objform).submit();
});
}
function genCheckForm($data) {
$ret = "";
if (!empty($data['check_filelds'])) {
foreach (explode(",", $data['check_filelds']) as $field) {
$ret.=$data[$field];
}
} else {
return md5(uniqid(time()));
}
return md5("sdgf4fhdfhw4sdeg" . $ret);
}
// Generate code
if ($_POST['act'] == "check_form") {
echo genCheckForm($_POST);
exit();
}
// Check form
if ($_POST['check_form'] != genCheckForm($_POST)) {
echo "Invalid form!";
exit();
} else {
echo "Valid form!";
exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment