ProcessWire - Formulare: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
Zeile 1: Zeile 1:
 
== Links ==
 
== Links ==
 
+
https://processwire.com/talk/topic/2089-create-simple-forms-using-api/ von Soma
 
  https://processwire.com/talk/topic/14206-contact-form-tutorial/ Mit Google ReCaptcha
 
  https://processwire.com/talk/topic/14206-contact-form-tutorial/ Mit Google ReCaptcha
 
== Beispiele ==
 
== Beispiele ==

Version vom 10. Oktober 2018, 13:18 Uhr

Links

https://processwire.com/talk/topic/2089-create-simple-forms-using-api/ von Soma
https://processwire.com/talk/topic/14206-contact-form-tutorial/ Mit Google ReCaptcha

Beispiele

Einfaches Formular (ohne Spamschutz)

<?php namespace ProcessWire;

function renderContactForm($backend_email, $subject) {
  $out = '';

  $modules = wire('modules'); // get access to pw modules object
  $input = wire('input');
  $sanitizer = wire('sanitizer');

  // Create the new <form>
  $form = $modules->get("InputfieldForm"); // get InputfieldForm Object
  $form->action = "./";
  $form->method = "post";
  $form->attr("id+name",'contact-form');

	// Set markup for form elements
  $form->setMarkup(array(
    'list' => "<div {attrs}>{out}</div>",
    'item' => "<div {attrs}>{out}</div>",
    //'item_label' => "<label for='{for}'>{out}</label>",
    'item_label' => "", // no label markup for this form
    'item_content' => "{out}",
    'item_error' => "<p class=\"error\">{out}</p>",
    'item_description' => "<p>{out}</p>",
    'item_head' => "<h2>{out}</h2>",
    'item_notes' => "<p class='notes'>{out}</p>",
  ));

	// Set classes for form elements
  $form->setClasses(array(
    'list' => 'form-list',
    'list_clearfix' => '',
    'item' => '{class}',
    'item_required' => 'required',
    'item_error' => '',
    'item_collapsed' => '',
    'item_column_width' => '',
    'item_column_width_first' => ''
  ));

	// New field: First name
  $field = $modules->get("InputfieldText");
  $field->label = __("Name");
  $field->attr([
    'id+name' => 'name',
    'placeholder' => 'Ihr Name',
    'class' => 'text'
    ]);
  $field->required = 1;
  $form->append($field);

	// New field: E-Mail
  $field = $modules->get("InputfieldEmail");
  $field->label = __("E-mail");
  $field->attr([
    'id+name' => 'email',
    'placeholder' => 'Ihre E-Mail Adresse',
    'class' => 'text'
    ]);
  $field->required = 1;
  $form->append($field);

	// New field: Phone
  $field = $modules->get("InputfieldText");
  $field->label = __("Telefon");
  $field->attr([
    'id+name' => 'phone',
    'placeholder' => 'Ihre Telefonnummer',
    'class' => 'text'
    ]);
  $field->required = 1;
  $form->append($field);

	// New field: Message
  $field = $modules->get("InputfieldTextarea");
  $field->label = __("Nachricht");
  $field->attr([
    'id+name' => 'message',
    'placeholder' => 'Ihre Nachricht',
    'class' => 'textarea'
    ]);
  $field->required = 1;
  $form->append($field);


  // SUBMIT button!
  $submit = $modules->get("InputfieldSubmit");
  $submit->attr("id+name", "submit");
  $submit->attr("class", "submit");
  $form->append($submit);

  // POST request, process the form
  if($input->post->submit) {
      $form->processInput($input->post);

      $name = $sanitizer->text($input->post->firstname);
      $email = $sanitizer->email($input->post->email);
      $message = $sanitizer->text($input->post->message);
      $phone = $sanitizer->text($input->post->phone);

      // ERRORS...
      if($form->getErrors()) {
        $out .= $form->render();
      } else {
        // FORM OK...
      	// Process the form here!

        $mail = wireMail();
        $mail->to($backend_email)->from($email);
        $mail->subject($subject);


        $body = "Name: " . $firstname . "\n";
        $body .= "E-Mail: " . $email . "\n";
        $body .= "Telefon: " . $phone . "\n\n";
        $body .= "Nachricht:\n" . $message;

        $mail->body($body);

        echo '<pre>'.$body.'</pre>';
        $numSent = $mail->send();

        $out .= '<p>'.__("Vielen Dank für Ihre Nachricht! Wir melden uns so bald wie möglich.").'</p>';

      }
  } else {
      // GET request, simply show the form
      $out .= $form->render();
  }

  return $out;
}