Jump to content

How to i add a contact form in my website


Citytech Tester
 Share

Recommended Posts

Hello, 

for me worked the tutorial from Soma https://processwire.com/talk/topic/2089-create-simple-forms-using-api/ 

in most cases fine. (I HATE TO CODE FORMS BUT ITS NECESSARY :( )

This is for the PHP Code. For Example you can Put it in the contact-template.php

<?php 
$out = '';

// create a new form field (also field wrapper)
$form = $modules->get("InputfieldForm");
$form->action = "./";
$form->method = "post";
$form->attr("id+name",'subscribe-form');  //generate the Form with <form id="subscribe-form" ...

// create a text input
$field = $modules->get("InputfieldText");
$field->label = "Name";
$field->attr('id+name','name');
$field->required = 1;
$field->attr("class" , $field->attr("class") . " form-control");  // generate <input id="name" class=" form-control ..... "
$form->append($field); // append the field to the form

 
// create a text input
$field = $modules->get("InputfieldText");
$field->label = "Adresse";
$field->attr('id+name','adresse');
$field->required = 1;
$field->attr("class" , $field->attr("class") . " form-control");
$form->append($field); // append the field to the form

// create a text input
$field = $modules->get("InputfieldText");
$field->label = "Plz/Ort";
$field->attr('id+name','plz');
$field->required = 1;
$field->attr("class" , $field->attr("class") . " form-control");
$form->append($field); // append the field to the form

// create email field
$field = $modules->get("InputfieldEmail");
$field->label = "E-Mail";
$field->attr('id+name','email');
$field->required = 1;
$field->attr("class" , $field->attr("class") . " form-control");
$form->append($field); // append the field

// create a text input
$field = $modules->get("InputfieldTextarea");
$field->label = "Nachricht";
$field->attr('id+name','text');
$field->attr("class" , $field->attr("class") . " form-control");
$field->required = 1;
$form->append($field); // append the field to the form


// oh a submit button!
$submit = $modules->get("InputfieldSubmit");
$submit->attr("value","Absenden");
$submit->attr("class"," btn btn-lg btn-primary");

$submit->attr("id+name","submit");
$form->append($submit);

// form was submitted so we process the form
if($input->post->submit) {

    // user submitted the form, process it and check for errors
    $form->processInput($input->post);

    // here is a good point for extra/custom validation and manipulate fields
    $email = $form->get("email");
    if($email && (strpos($email->value,'@hotmail') !== FALSE)){
        // attach an error to the field
        // and it will get displayed along the field
        $email->error("Bitte Email eingeben.");
    }

    if($form->getErrors()) {
        // the form is processed and populated
        // but contains errors
        $out .= $form->render();
    } else {

        // do with the form what you like, create and save it as page
        // or send emails. to get the values you can use
        // $email = $form->get("email")->value;
        // $name = $form->get("name")->value;
        // $text = $form->get("text")->value;
        //
        // to sanitize input
          $name = $sanitizer->text($input->post->name);
          $adresse = $form->get("adresse")->value;
          $plz = $form->get("plz")->value;
      
        $message = $form->get("text")->value;

        
        $subject = "Web Anfrage";
       // $emailFrom = $form->get('email')->value; 
         $emailFrom = $sanitizer->email($form->get("email")->value);
        $emailTo = 'youremail@domain.com';  
        $body = "Die Email ist von ".$name."\r\n"."\r\n".$adresse."\r\n".$plz."\r\n".$message;

        mail($emailTo, $subject, $body, "From: $emailFrom"); 

        $out .= "<h2>".$name."</h2><h2> Ihre nachricht wurde versendet.</h2>";
    }

} else {
    // render out form without processing
    $out .= $form->render();
}

?>

and at the place where you will have your form you can use 

<?php echo $out; ?>

Its not 100% perfect solution but as I was new to processwire and also PHP  it works for me. 

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...