Skip to content

Instantly share code, notes, and snippets.

@dubrod
Created November 30, 2018 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dubrod/8d23eab244d72806dc3337a9e8dcc293 to your computer and use it in GitHub Desktop.
Save dubrod/8d23eab244d72806dc3337a9e8dcc293 to your computer and use it in GitHub Desktop.
collection of modx form snips
http://rtfm.modx.com/extras/revo/formit/formit.tutorials-and-examples/formit.handling-selects,-checkboxes-and-radios
[[!FormIt?
&hooks=`spam,email,redirect`
&emailTpl=`ContactEmailTpl`
&emailFrom=`[[++emailsender]]`
&emailTo=`wayne@modx.com`
&emailUseFieldForSubject=`1`
&redirectTo=`33`
&validate=`name:required,
email:email:required,
subject:required,
nospam:blank,
text:required:stripTags`
]]
[[!+fi.error_message:notempty=`<p class="error">[[!+fi.error_message]]</p>`]]
[[!+fi.validation_error_message:notempty=`<p class="error">[[!+fi.validation_error_message]]</p>`]]
<form action="[[~[[*id]]]]" method="post" class="contactForm">
<input type="hidden" name="nospam" value="" />
<div>
<label for="name">Name: [[!+fi.error.name]]</label>
<input type="text" name="name" id="name" value="[[!+fi.name]]" [[!+fi.error.name:notempty=`aria-invalid="true"`]]>
</div>
<div>
<label for="email">Email: [[!+fi.error.email]]</label>
<input type="text" name="email" id="email" value="[[!+fi.email]]">
</div>
<div>
<label for="subject">Reason for Contact: [[!+fi.error.subject]]</label>
<input type="text" name="subject" id="subject" value="[[!+fi.subject]]">
</div>
<br class="clear" />
<label for="text">Message: [[!+fi.error.text]]</label>
<textarea name="text" id="text" value="[[!+fi.text]]">[[!+fi.text]]</textarea>
<br class="clear" />
<input type="submit" value="SEND" class="button button-red">
</form>
----
Company Name: [[+name]]<br>
Address: [[+address]]<br>
Address 2: [[+address2]]<br>
City: [[+city]]<br>
State: [[+state]]<br>
Country: [[+country]]<br>
<br>
Contact: [[+first_name]] [[+last_name]]<br>
Phone: [[+phone]]<br>
Email: [[+email]]<br>
<br>
Message:
[[+text]]
----
Success Message / Newsletter
[[!FormIt?
&hooks=`email`
&emailTpl=`HpSubscribeEmail`
&emailTo=`wayne@modx.com`
&emailSubject=`New Email Subscriber`
&validate=`email:email:required`
&successMessage=`<p class="success-msg">Thank You for subscribing!</p>`
]]
[[!+fi.successMessage]]
<form class="subscription-box" id="subForm" action="[[~[[*id]]]]#subForm" method="post">
<input type="text" placeholder="Email Address" name="email" id="email" value="[[!+fi.email]]" class="hp-subscribe-input" />
<input type="submit" value="&gt;" id="hp-subscribe-submit" class="subscribe-off" />
</form>
---
[[!FormIt?
&hooks=`email`
&emailTpl=`ContactEmailTpl`
&emailTo=`wayne@modx.com`
&emailSubject=`New Website Email`
&validate=`email:email:required`
&successMessage=`<p class="text-primary">Thank You for subscribing!</p>`
]]
[[!+fi.successMessage]]
[[!+fi.error_message:notempty=`<p class="error">[[!+fi.error_message]]</p>`]]
<form class="form form-light" id="subForm" action="[[~[[*id]]]]#subForm" method="post">
<div>
<label for="fname">First Name</label>
<input type="text" name="fname" id="fname" value="[[!+fi.fname]]" class="input" />
</div>
<div>
<label for="lname">Last Name</label>
<input type="text" name="lname" id="lname" value="[[!+fi.lname]]" class="input" />
</div>
<div>
<label for="email">Email Address</label>
<input type="text" name="email" id="email" value="[[!+fi.email]]" class="input" />
</div>
<input type="submit" value="SUBMIT" class="button button-primary" />
</form>
-----
<select name="color">
<option value="blue" [[!+fi.color:FormItIsSelected=`blue`]] >Blue</option>
<option value="red" [[!+fi.color:FormItIsSelected=`red`]] >Red</option>
<option value="green" [[!+fi.color:FormItIsSelected=`green`]] >Green</option>
<!-- This would also work -->
<option value="yellow" [[!+fi.color:is=`yellow`:then=`selected`]]>Yellow</option>
</select>
----
<label>Color: [[!+fi.error.color]]</label>
<input type="checkbox" name="color[]" value="blue"> Blue
<input type="checkbox" name="color[]" value="red"> Red
<input type="checkbox" name="color[]" value="green"> Green
----
http://rtfm.modx.com/extras/revo/formit/formit.hooks/formit.hooks.formitsaveform
[[!FormIt?
&hooks=`FormItSaveForm`
&formName=`Contact Form`
&formEncrypt=`0`
&formFields=`name`
]]
[[!+fi.error_message:notempty=`<p class="error">[[!+fi.error_message]]</p>`]]
<form action="[[~[[*id]]]]" method="post" class="form-light form-stacked">
<label for="name">Name: [[!+fi.error.name]]</label>
<input type="text" name="name" id="name" value="[[!+fi.name]]" class="input" />
<br class="clear" />
<input type="submit" value="SEND" class="button button-red" />
</form>
----
<?php
$uF;
$file = $hook->getValue('fz_file');
/*
['name'] value: image.jpg
['type'] value: image/jpeg
['tmp_name'] value: /tmp/phpUe6CkA
['error'] value: 0
['size'] value: 173812
*/
if( empty($file) || $file['size'] > 5000000 ){
$modx->setPlaceholders(array(
'error' => "Please make sure your file is a .jpg, .jpeg, or .png and it is under 5MB."
),'fz_');
return false;
} else {
//VARIABLES
$uploadPath = 'assets/images/50th-photos/';
$uF = $uploadPath . $file['name'];
$tn = $file['tmp_name'];
//$modx->log(modX::LOG_LEVEL_ERROR,'file temp name: ' . $tn);
//$modx->log(modX::LOG_LEVEL_ERROR,'file name: ' . $uF);
if(is_dir($uploadPath)){
//nothing
} else {
mkdir($uploadPath, 0775);
}
if(move_uploaded_file($tn, $uF)) {
//$modx->log(modX::LOG_LEVEL_ERROR,'file moved: ' . $uF);
$hook->setValue('fz_fileName', $uF);
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment