Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ProcessWire form with "regular" fields and file uploads. The first two files (contact-form and contact-success) are for the contact form and the success page with normal fields. This works successfully. The third and fourth files (contact-form2 and contact-success2) are the same, but with the file-upload field added. These are currently not work…
<form action="/customer-service/contact/contact-success/" method="post">
<p><label for="contactname">Name:</label></p>
<p><input type="text" name="contactname"></p>
<p><label for="email">E-Mail:</label></p>
<p><input type="email" name="email"></p>
<p><label for="comments">Comments:</label></p>
<p><textarea name="comments" cols="25" rows="6"></textarea></p>
<button type="submit">Submit</button>
</form>
<form action="/customer-service/contact/contact-success/" method="post" enctype="multipart/form-data">
<p><label for="contactname">Name:</label></p>
<p><input type="text" name="contactname"></p>
<p><label for="email">E-Mail:</label></p>
<p><input type="email" name="email"></p>
<p><label for="comments">Comments:</label></p>
<p><textarea name="comments" cols="25" rows="6"></textarea></p>
<p>Click the "Select Files" button below to upload your photo.</p><input type="file" name="contact_photo" />
<button type="submit">Submit</button>
</form>
<?php
// First, confirm that a submission has been made
if ($input->post->contactname)
{
// Save in the ProcessWire page tree; map submission to the template fields
$np = new Page(); // create new page object
$np->template = $templates->get("contact_submission");
$np->parent = $pages->get("/customer-service/contact-us/contact-submission-listing/");
// Send all form submissions through ProcessWire sanitization
$title = $sanitizer->text($input->post->contactname);
$name = $sanitizer->text($input->post->contactname);
$contactname = $sanitizer->text($input->post->contactname);
$email = $sanitizer->email($input->post->email);
$comments = $sanitizer->textarea($input->post->comments);
// Match up the sanitized inputs we just got with the template fields
$np->of(false);
$np->title = $contactname;
$np->name = $contactname;
$np->contactname = $contactname;
$np->email = $email;
$np->comments = $comments;
// Save/create the page
$np->save();
}
?>
<?php
if ($input->post->submit) // Test that the form was submitted successfully, so we can start processing the entry.
{
// Handle the photo upload of the entry in a separate routine from the rest of the fields
$message = '';
// Set a temporary upload folder where the files are stored during form processing
$upload_path = $config->paths->assets . "files/temp/";
// New wire upload
$contact_photo = new WireUpload('contact_photo'); // References the name of the field in the HTML form that uploads the photo
$contact_photo->setMaxFiles(5);
$contact_photo->setOverwrite(false);
$contact_photo->setDestinationPath($upload_path);
$contact_photo->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif'));
// execute upload and check for errors
$files = $contact_photo->execute();
if(!$contact_photo->getErrors())
{
// Send all the form's $_POST submissions through ProcessWire's sanitization and/or make the sanitized value equal to a variable with the same name as the template fields we'll be populating
$title = $sanitizer->text($input->post->contactname);
$name = $sanitizer->text($input->post->contactname);
$contactname = $sanitizer->text($input->post->contactname);
$email = $sanitizer->email($input->post->email);
$comments = $sanitizer->textarea($input->post->comments);
// Do an initial save in the ProcessWire page tree; set the necessary information (template, parent, title, and name)
$np = new Page(); // create new page object
$np->template = $templates->get("contact_submission"); // set the template that applies to pages created from form submissions
$np->parent = $pages->get("/customer-service/contact-us/contact-submission-listing/"); // set the parent for the page being created here
$np->title = $contact_name;
$np->name = $contact_name;
$np->save();
// Match up the sanitized inputs from above with the fields in the template, so they can actually fill the fields for the page we're creating
$np->of(false);
$np->contactname = $contactname;
$np->email = $email;
$np->comments = $comments;
// Run photo upload
foreach($files as $filename)
{
$np->images = $upload_path . $filename;
}
// save page again
$np->save();
// remove all tmp files uploaded
foreach($files as $filename)
unlink($upload_path . $filename);
$message .= "<p class='message'>Files uploaded!</p>";
}
else
{
// Remove files from the temporary folder, where they were placed above
foreach($files as $filename) unlink($upload_path . $filename);
// Get the errors
foreach($contact_photo->getErrors() as $error) $message .= "<p class='error'>$error</p>";
}
} // End of photo-upload routine
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.