2
A
回答
0
使用wordpress。 这是我想出了以下几个教程:
- SO answer
- codeforgeek
和其他一些我不记得
// Dropzone.autoDiscover = false; //I use Autodiscover Dropzone.options.myAwesomeDropzone = { // The camelized version of the ID of the form element // The configuration we've talked about above autoProcessQueue: false, uploadMultiple: true, parallelUploads: 100, maxFiles: 100, previewsContainer: '.fileupload', // The setting up of the dropzone init: function() { var myDropzone = this; // First change the button to actually tell Dropzone to process the queue. this.element.querySelector("input[type=submit]").addEventListener("click", function(e) { // Make sure that the form isn't actually being sent. e.preventDefault(); e.stopPropagation(); // myDropzone.processQueue(); var valid = true; $('[data-required]').each(function(i,ele){ if(ele.value == ''){ $(this).parent().parent().addClass('alert'); valid = false; } }); if (!valid){ e.preventDefault(); scrollTop(); return false; } // Get the recaptcha input var cap_input = grecaptcha.getResponse(); $.ajax({ url: homeUrl+'/wp-admin/admin-ajax.php', type: "POST", dataType: "JSON", data: { "action": "verifyReCaptcha", "g-recaptcha-response": cap_input }, success: function(response){ console.log(response); if(response == 'Good captcha'){ if (myDropzone.getQueuedFiles().length > 0) { myDropzone.processQueue(); } else { // Upload anyway without files if (wantToUpload == 'unknown') { $('#noFiles').modal(); e.preventDefault(); return false; } else if (wantToUpload == false) { myDropzone.uploadFiles([ ]); } else { myDropzone.uploadFiles([ ]); } } } else{ console.log('Spammer go away'); $('.g-recaptcha').addClass('alert'); } } }); }); // Listen to the sendingmultiple event. In this case, it's the sendingmultiple event instead // of the sending event because uploadMultiple is set to true. this.on("sendingmultiple", function() { // Gets triggered when the form is actually being sent. // Hide the success button or the complete form. console.log('sending'); }); this.on("successmultiple", function(files, response) { // Gets triggered when the files have successfully been sent. // Redirect user or notify of success. console.log('success'); }); this.on("errormultiple", function(files, response) { // Gets triggered when there was an error sending the files. // Maybe show form again, and notify user of error console.log('error'); }); } }
而且一些HTML/PHP的
<form name="" action="<?=get_permalink();?>" method="post" id="my-awesome-dropzone" class="dropzone" enctype="multipart/form-data">
<label class="label" for="text-435">
<?=__('Name*','gt_domain');?>
</label>
<input type="text" name="client-name" value="" size="40" class="input__field--manami" id="text-435" data-required="true">
<!-- Lots of input fields -->
<div class="col-md-8 col-xs-12 fileupload dropzone-previews dz-message">
<h2 class="text-center" id="fileuploadtext">
<?=__('Click or drag and drop your file here <br>(Max 60mb)','gt_domain');?>
</h2>
</div>
<div class="g-recaptcha" data-sitekey="MY_PUBLIC_KEY"></div>
<input class="dangerzone-submit" type="submit" value="<?=__('Request quote','gt_domain');?>" name="dangerzone-submit">
</form>
而对于AJAX验证提交表单之前:
function verifyReCaptcha(){
$email;
$comment;
$captcha = false;
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo json_encode('Please check the the captcha form.');
exit;
}
$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=MY_SUPER_SECRET_GOOGLE_RECAPTCHA_KEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
if($response['success'] == false){
echo json_encode('You are spammer ! Get the @$%K out');
}
else{
echo json_encode('Good captcha');
}
exit;
}
add_action('wp_ajax_nopriv_verifyReCaptcha', 'verifyReCaptcha');
add_action('wp_ajax_verifyReCaptcha', 'verifyReCaptcha');
而且finnaly收到形式输入和图片:
<?php
if (isset($_POST['dangerzone-submit'])) {
// print_r($_POST);
$client = $_POST['client-name'];
$email = $_POST['email'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
if ($_FILES) {
foreach ($_FILES as $file => $array) {
// print_r($array);
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK && $_FILES[$file]['error'][0] !== 0) {
echo "upload error : \n";
print_r($_FILES[$file]['error']);
print_r($_FILES);
die();
}
$uploads_dir = ABSPATH . 'wp-content/uploads/dropzone/';
$external_link = get_home_url().'/wp-content/uploads/dropzone/';
foreach ($array['name'] as $key => $val) {
print_r($key);
$name = $array['name'][$key];
$tmp_name = $array['tmp_name'][$key];
$newfilename = wp_unique_filename($uploads_dir, $name);
$movefile = move_uploaded_file($tmp_name, $uploads_dir.$newfilename);
$uploaded_files[] = $external_link.$newfilename;
}
}
}
sendMail(//My func for sending mail
$client,
$email,
$company,
$phone,
$comments,
$uploaded_files
);
}
?>
相关问题
- 1. Laravel TokenMismatchException和悬浮窗
- 2. 如何解码和验证羽毛js
- 3. 不能使用悬浮窗和multer
- 4. Laravel和悬浮窗上传图片
- 5. html5验证和JS
- 6. 悬浮窗,角,MVC5实施
- 7. 悬浮窗发送空
- 8. 验证码弹出窗口
- 9. 悬浮窗JS只上传一个文件
- 10. 试图验证窗体角js
- 11. remote-true和js验证
- 12. Js用int和selectbox验证
- 13. Modals和窗体验证
- 14. YouTube和验证码
- 15. 表单验证和验证码
- 16. 验证html颜色代码JS
- 17. 节点js passport-facebook风险验证码
- 18. 使用JS解析密码验证
- 19. Angular JS |角度验证匹配密码
- 20. 验证JS代码的API /库/工具?
- 21. 验证码验证
- 22. 验证验证码
- 23. 悬浮窗回调多个上传
- 24. 点击数上传悬浮窗的jQuery
- 25. 发送正确的头与悬浮窗
- 26. 悬浮窗的init()不执行
- 27. 简单的悬浮窗文件上传
- 28. reactjs - 悬浮窗文件上传
- 29. Javascript/jQuery - 浮动验证?
- 30. React js es6验证