Last active
September 17, 2021 10:06
-
-
Save 0test/9d5b5a9d057ca0d698ab149b442b23b9 to your computer and use it in GitHub Desktop.
ajax Formlister
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('MODX_API_MODE', true); | |
include_once("index.php"); | |
$modx->db->connect(); | |
if (empty ($modx->config)){ | |
$modx->getSettings(); | |
} | |
switch($_REQUEST['q']){ | |
case 'myform': | |
$result = $modx->runSnippet('FormLister', array( | |
'formid'=> 'myform', | |
'protectSubmit'=> '0', | |
'submitLimit'=> '0', | |
'attachments'=> 'ufile', | |
'fileRules'=> '{ | |
"ufile":{ | |
"required":"Фото забыл", | |
"allowed":{ | |
"params": [ ["jpg","jpeg"] ], | |
"message": "Разрешены только фотки с титьками" | |
} | |
} | |
}', | |
'formTpl'=> 'fl_ajax_form', | |
'noemail'=> '1', | |
'prepareAfterProcess' => array(function($modx, $data, $FormLister, $name){ | |
$files = $FormLister->getFormData('files'); | |
if (isset($files['ufile']) && $files['ufile']['error'] === 0) { | |
$dir = 'assets/images/1/'; | |
$filename = $FormLister->fs->takeFileName($files['ufile']['name']); | |
$ext = $FormLister->fs->takeFileExt($files['ufile']['name']); | |
$filename = $modx->stripAlias($filename).'.'.$ext; | |
$filename = $FormLister->fs->getInexistantFilename($dir.$filename,true); | |
if ($FormLister->fs->makeDir($dir) && move_uploaded_file($files['ufile']['tmp_name'],$filename)) { | |
return $data; | |
} | |
} | |
}), | |
'errorClass' => ' has-error', | |
'requiredClass' => ' has-warning', | |
'messagesOuterTpl'=> '@CODE:<div class="alert alert-danger" role="alert">[+messages+]</div>', | |
'errorTpl'=> '@CODE:<span class="help-block">[+message+]</span>', | |
) | |
); | |
echo $modx->parseDocumentSource($result); | |
exit(); | |
break; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="myform_wrapper"> | |
{{fl_ajax_form}} | |
</div> | |
<script> | |
$(document).ready(function(){ | |
$(document).on('submit', '#myform',function(e){ | |
var form = $('#myform')[0]; | |
var data = new FormData(form); | |
$.ajax({ | |
type: 'post', | |
url: '/ajax.php?q=myform', | |
data: data, | |
success: function(data){ | |
$('#myform_wrapper').html(data); | |
}, | |
contentType: false, | |
processData: false, | |
cache:false, | |
}); | |
e.preventDefault(); | |
}); | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form class="form-horizontal" method="post" enctype="multipart/form-data" id="myform"> | |
[+form.messages+] | |
<input type="hidden" name="formid" value="myform"> | |
<div class="form-group[+ufile.errorClass+][+ufile.requiredClass+]"> | |
<label for="first" class="col-sm-2 control-label">* Приложите фоточку</label> | |
<div class="col-sm-10"> | |
<input type="file" id="ufile" class="form-control" name="ufile" multiple="multiple"> | |
[+ufile.error+] | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-offset-2 col-sm-10"> | |
<button type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-envelope"></i> Отправить</button> | |
</div> | |
</div> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment