Jump to content

simple form from 2011 by Ryan still good?


wishbone
 Share

Recommended Posts

Was searching and searching for a complete code for a simple form - found this one from 2011 by Ryan, which is exactly what I need:

So ist that still State of the Art and usable?

What I need is a simple form with about 13 elements of the form:

<div>
    <label>
            <span>Name</span>
            <input type="text" placeholder="Name" name="name" size="50" maxlength="20">
    </label>
</div>

also a radio button with 3 values.

The data of the form is to be sent to two email addresses, a confirmation that the mail has been sent - and a honeypot would be great.

Since I'm a php-dunce, I need the full code...

thx for advice!

Link to comment
Share on other sites

as a non-coder designer, this is what frustrates me always with processwire. If I wouldn't like it so much for it's "fluffyness" and flexibility I'd have long given up.

Navigation is full code in the sample profile. But there is no contact from.

A simple contact form with custom markup just to insert into a template - I can't find, except the one of Ryan which is 12 years old ?

All the modules are either without custom markup, or not maintained any more, or still have issues, or are so complicated, that it's too time consuming to learn.

Maybe somebody could write the code for me for money?

Link to comment
Share on other sites

On 5/24/2022 at 5:15 AM, Gideon So said:

I fully understand your feeling. I came from a non coder background, too.

Thank you for your sympathy ?

On 5/24/2022 at 5:15 AM, Gideon So said:

If you are willing to pay for a working contact form. I suggest you to buy Ryan's FormBuilder.

far too complicated! I just need a simple form full code

Link to comment
Share on other sites

9 hours ago, wishbone said:

far too complicated! I just need a simple form full code

By definition, a form isn't "simple" as it's not composed by a simple input that we could copy paste 13 times. A simple form is simple when you have someone have already done all the hard work. Technically speaking, even if the form has 1 field, or 13, you  still need to follow some simple rules to handle the form or your future website will get hacked; And not because of the ProcessWire fault.

All that to finally say, that the solution shown above are effective, and just require you to read the documentation to follow the implemenation that the author had in mind. Because they are smart and talented, they tackled the same problem you encounter and made their modules simple to use.

You can also find some guides in the tutorials section of this forum, but I suggest you to buy without brainstorming the pro module made by Ryan if you are looking for a click / copy / paste solution.

About the 12 years old code (which should works without hassle lol thats crazzy every time ?) you will find a lot of other working samples code to copy pasta, I  still suggest you to read and understand what you are doing ?

 

 ?

 

  • Like 4
Link to comment
Share on other sites

5 hours ago, flydev ?? said:

copy paste 13 times

nobody is talking about copy/paste 13 times. But the structure is the same if 2 or 20 fields.

5 hours ago, flydev ?? said:

just require you to read the documentation

believe me, I've studied all these modules/ suggestions. Hours and hours and days and days.

Formbuilder is overloaded for my project.

Coders just can't understand non-coders, obviously.

 

5 hours ago, flydev ?? said:

About the 12 years old code (which should works without hassle lol thats crazzy every time ?)

Now that's what I needed to hear, finally! It's about security concerns also.

Though, Ryan uses php mail - I don't know if php mail can take more than the given fields?

mail($emailTo, "Contact Form", $message, "From: $form[email]");

 

5 hours ago, flydev ?? said:

  you will find a lot of other working samples code to copy pasta

besides Ryan's I found just ONE more, which is told to have security issues.

 

5 hours ago, flydev ?? said:

I  still suggest you to read and understand what you are doing

thx again for insinuating that I'm not searching and reading and trying to understand enough.

Again, coders don't understand non-coders. They think we are just lazy ?

Link to comment
Share on other sites

Hi Gideon

9 hours ago, Gideon So said:

He needs a A 13-field form and a 13-field form is not a simple contact form IMO.

if the mailer takes more than the given four fields, it should not be complicated to duplicate the structure and insert the custom fields.

mail($emailTo, "Contact Form", $message, "From: $form[email]");

Can I just add more custom fields?

Same with wiremail

Link to comment
Share on other sites

Hi Klenkes,

thx for your idea!

On 5/24/2022 at 8:24 AM, Klenkes said:

what about the module SimpleContactForm.

https://processwire.com/modules/simple-contact-form/

Wouldn‘t do that what you want?

I even can't install it. Maybe it is not compliant with php8.

And not maintained any more.

And for me very difficult to understand how to modify the thml markup.

(I wrote that already, but where has the post gone ? ? )

Link to comment
Share on other sites

@wishbone I fully understand you as non coder as we all try to build things for non-coders, but you also already know that non-coders have also to do a minimum; It wasn't my intention to feel rude (please grab this beer ? ? ) , I was trying to find again a way to say that reading the doc is what could help here. I mean the FormBuilder one, or the tutorial I linked on my previous post.

Anyway, back to to your issue. I read the old code you linked, and there is the full code. It works, and if you follow (read again ?) the whole thread lol, you will find the solution for the honeypot technique (page #2 I think posted by @kongondo).

About security concerns, there is no breach in the code and can be used as is. But again, without reading the doc, you will eventually get security issues only when you will add more input to the form. I mean here, the $sanitizing technique which give you "control" on the submitted user data.

Again, sorry to insist, the FormBuilder module is what you need as non-coder, as I think it doesn't require code to be written, or maybe one line to render the form (I can't confirm, I have to read the doc lol ? ) and give you all the security you will need to stay safe.

 

Code:

Spoiler
<?php

$sent = false;
$error = '';
$emailTo = 'nikola@company.com'; // or pull from PW page field

// sanitize form values or create empty
$form = array(
    'fullname' => $sanitizer->text($input->post->fullname),
    'email' => $sanitizer->email($input->post->email),
    'comments' => $sanitizer->textarea($input->post->comments),
  
    // ? Use $sanitizer for security
    // ? Add more fields here, and use the right sanitizer (https://processwire.com/api/ref/sanitizer/)
    
    ); 

// check if the form was submitted
if($input->post->submit) {

    // determine if any fields were ommitted or didn't validate
    // ? you can do a small logic here to avoid all field being required.
    foreach($form as $key => $value) {
        if(empty($value)) $error = "<p class='error'>Please check that you have completed all fields.</p>";
    }

    // if no errors, email the form results
    // ? you can complete the message with your other field value
    if(!$error) {
        $message = "Full name: $form[fullname]\n" . 
               "Email: $form[email]\n" . 
               "Comments: $form[comments]"; 

        mail($emailTo, "Contact Form", $message, "From: $form[email]");

        // populate body with success message, or pull it from another PW field
        $page->body = "<h2>Thank you, your message has been sent.</h2>"; 
        $sent = true;	
    }
}

if(!$sent) {

    // sanitize values for placement in markup
    foreach($form as $key => $value) {
        $form[$key] = htmlentities($value, ENT_QUOTES, "UTF-8"); 
    }

    // append form to body copy
    // ? add more field/input markup here
    $page->body .= <<< _OUT

        $error
        <form action="./" method="post">
        <p>
        <label for="fullname">Your Name</label><br />
        <input type="text" id="fullname" name="fullname" value="$form[fullname]" />
        </p>

        <p>
        <label for="email">Your Email</label><br />
        <input type="email" name="email" id="email" value="$form[email]" />
        </p>

        <p>
        <label for="comments">Comments</label><br />
        <textarea id="comments" name="comments">$form[comments]</textarea>
        </p>

        <p><input type="submit" name="submit" value="Submit" /></p>
        </form>

_OUT;

}

// include site's main template which outputs everything
include("./main.php"); 

 

 

Try something with the form, and do not hesitate to ask further help here ?

 

 

2 hours ago, wishbone said:

Again, coders don't understand non-coders. They think we are just lazy ?

Please, believe me, I am speaking by experience, we, coders, are lazy as f***, you can't imagine how ? 

 

Edit: Sorry, I didn't paid attention to your third message, you could head to the Job forum to get more attention.

  • Like 4
Link to comment
Share on other sites

On 5/23/2022 at 9:48 PM, wishbone said:

1. "Sending emails is not part of this module"

2. seems to still have issues

Hello Wishbone,

"Sending emails is not part of this module" means not that emails cannot be send by the forms createt by this module. It means that sending emails will be done fe by the WireMail class of ProcessWire and not by this module class. This module creates and validates forms - not less or more. After the validation process is finished you can do whatever you want: storing data in the DB, sending emails,.... its up to you and you have to write the code for the action after the validation by yourself.

Yes, there seems to be an issue on a Mac environment for the moment, but I was not able to reproduce the error. I only got 2 messages from users which had troubles. One problem could be solved, the other is still there, but I am working on it.

If you want to install the module maybe I could help you.

Best regards

  • Like 3
Link to comment
Share on other sites

4 hours ago, wishbone said:

believe me, I've studied all these modules/ suggestions. Hours and hours and days and days.

Don't get me wrong, but these hours would have been better spent on 74€ for a single licence of FormBuilder. No coding required. I own a DEV licence since 2016 and I never think twice about the yearly payment, not even a second! FormBuilder slogan should be: Forms made easy!

  • Like 3
Link to comment
Share on other sites

16 hours ago, Klenkes said:

Don't get me wrong, but these hours would have been better spent on 74€ for a single licence of FormBuilder. No coding required. I own a DEV licence since 2016 and I never think twice about the yearly payment, not even a second! FormBuilder slogan should be: Forms made easy!

I was thinking of this, of course, but Formbuilder is so overloaded! and for a small project expensive. I'm not doing too many pw-sites.

Anyway, through doing it myself I try to learn, php, and processwire, and all.

That's how we non-coders manage to build processwire-sites ?

Link to comment
Share on other sites

17 hours ago, Juergen said:

This module creates and validates forms - not less or more.

If you want to install the module maybe I could help you.

 

Hi Jürgen,

thank you for your help offer!

Though I don't know how to send mail via wiremail or other... that's exactly my problem.

Link to comment
Share on other sites

Hi flydev,

thank you so much for your very, very kind and elaborated answer! I feel much better now ?

On 5/27/2022 at 5:34 PM, flydev ?? said:

the solution for the honeypot technique (page #2 I think posted by @kongondo).

of course, I've read all that, and more about the honeypot technique outside pw. But again, I still have the problem to implement.

 

On 5/27/2022 at 5:34 PM, flydev ?? said:
  Reveal hidden contents
<?php

$sent = false;
$error = '';
$emailTo = 'nikola@company.com'; // or pull from PW page field

// sanitize form values or create empty
$form = array(
    'fullname' => $sanitizer->text($input->post->fullname),
    'email' => $sanitizer->email($input->post->email),
    'comments' => $sanitizer->textarea($input->post->comments),
  
    // ? Use $sanitizer for security
    // ? Add more fields here, and use the right sanitizer (https://processwire.com/api/ref/sanitizer/)
    
    ); 

// check if the form was submitted
if($input->post->submit) {

    // determine if any fields were ommitted or didn't validate
    // ? you can do a small logic here to avoid all field being required.
    foreach($form as $key => $value) {
        if(empty($value)) $error = "<p class='error'>Please check that you have completed all fields.</p>";
    }

    // if no errors, email the form results
    // ? you can complete the message with your other field value
    if(!$error) {
        $message = "Full name: $form[fullname]\n" . 
               "Email: $form[email]\n" . 
               "Comments: $form[comments]"; 

        mail($emailTo, "Contact Form", $message, "From: $form[email]");

        // populate body with success message, or pull it from another PW field
        $page->body = "<h2>Thank you, your message has been sent.</h2>"; 
        $sent = true;	
    }
}

if(!$sent) {

    // sanitize values for placement in markup
    foreach($form as $key => $value) {
        $form[$key] = htmlentities($value, ENT_QUOTES, "UTF-8"); 
    }

    // append form to body copy
    // ? add more field/input markup here
    $page->body .= <<< _OUT

        $error
        <form action="./" method="post">
        <p>
        <label for="fullname">Your Name</label><br />
        <input type="text" id="fullname" name="fullname" value="$form[fullname]" />
        </p>

        <p>
        <label for="email">Your Email</label><br />
        <input type="email" name="email" id="email" value="$form[email]" />
        </p>

        <p>
        <label for="comments">Comments</label><br />
        <textarea id="comments" name="comments">$form[comments]</textarea>
        </p>

        <p><input type="submit" name="submit" value="Submit" /></p>
        </form>

_OUT;

}

// include site's main template which outputs everything
include("./main.php"); 

 

what is this? can't open

 

On 5/27/2022 at 5:34 PM, flydev ?? said:

when you will add more input to the form. I mean here, the $sanitizing technique which give you "control" on the submitted user data.

Try something with the form, and do not hesitate to ask further help here ?

 

Edit: found your very helpful remarks only recently, in the hidden content which I couldn't open at frist
Does it mean, I can't just extend the form by duplicating the structure of the input fields, sanitizing them and put them in the mail call?

Anyway, I will try Ryan's solution and come back to your kind help offer if I have problems.

 

Thank you again so much for the time you have taken on this! I really really want to learn more processwire, that's why I'm trying to do it myself. Also, I prefer raw code over modules, because they give me the freedom.

Maybe once I know more about how the code works, modules can make it faster. But for now, I want to learn ?.

 

 

Edited by wishbone
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
On 5/28/2022 at 12:32 PM, wishbone said:

Edit: found your very helpful remarks only recently, in the hidden content which I couldn't open at frist
Does it mean, I can't just extend the form by duplicating the structure of the input fields, sanitizing them and put them in the mail call?

Yes, I used the "hidden field" feature of the forum to avoid a wall of text here in the thread. 

As you saw later, you can add more fields by following my comments, just a small note about the last portion of code (the input html markup), she is called in "Heredoc" and you can write inside it HTML code as you are used to and call basic PHP variable from there. If you encounter some issue, just think about the PHP version you are using and check the doc, as there is some subtilities since php 7.3; That's it, have fun 

On 5/28/2022 at 12:32 PM, wishbone said:

I really really want to learn more processwire, that's why I'm trying to do it myself. Also, I prefer raw code over modules, because they give me the freedom.

? When you say you prefer raw code over module, I think you are confusing a bit things. Modules are "raw code", for example, I think like other devs, I always end up writing my code in a module, then call a single method from the module to render the "module's context", which let focusing on the code of the template and not the logic behind, without speaking maintainability and dozens of other reasons. Be ready to receive more "RTFM ?" answer in the future ?

  • Like 1
Link to comment
Share on other sites

Thank you!

On 6/7/2022 at 3:46 PM, flydev ?? said:

Modules are "raw code"

like snippets in modX, where I came from?

Again, I read the f** docs, but they don't help much because I'm not a coder.

Link to comment
Share on other sites

After thinking about the DSGVO, I need to not make all fields mandatory, only as few as possible, to mark with *required.

The current code is:

    // sanitize values for placement in markup
    foreach($form as $key => $value) {
        $form[$key] = htmlentities($value, ENT_QUOTES, "UTF-8");
    }

I rtfm and this is what I came up with:

$bool = $inputfield->isEmpty();

but don't know how to use it ?

Link to comment
Share on other sites

this works:

if (empty($input->post->email)) $error = "<p class='error'>Bitte eine E-Mail-Adresse angeben</p>";

The form starts with:

$sent = false;
$error = '';

error is called here:

    echo <<< _OUT

        $error
        <form action="./" method="post">

 

Edited by wishbone
more code
Link to comment
Share on other sites

6 hours ago, wishbone said:

The form starts with:

$sent = false;
$error = '';

This doesn’t happen after you set the error, by any chance?

If that’s not it, try posting the whole code up until the point where the error should be displayed.

Also, I would probably prefer to access the email field like this

$input->post('email')

because $input->post->email() is also the name of a sanitizer method.

  • Like 1
Link to comment
Share on other sites

thankyou thank you for looking into this!

18 hours ago, Jan Romero said:

This doesn’t happen after you set the error, by any chance?

no, it's the first lines.

this is Ryan's code (with more fields), error message doesn't show:

$sent = false;
$error = '';
$emailTo = 'abc@gmx.de'; // or pull from PW page field

// sanitize form values or create empty
$form = array(
    'vorname' => $sanitizer->text($input->post->vorname),
    'name' => $sanitizer->text($input->post->name),
    'orga' => $sanitizer->text($input->post->orga),
    'funktion' => $sanitizer->text($input->post->funktion),
    'gruppe' => $sanitizer->text($input->post->gruppe),
    'anzahl' => $sanitizer->text($input->post->anzahl),
    'termin1' => $sanitizer->date($input->post->termin1, null, ['returnFormat' => 'd.m.Y']),
    'termin2' => $sanitizer->date($input->post->termin2, null, ['returnFormat' => 'd.m.Y']),
    'zeit' => $sanitizer->text($input->post->zeit),
    'telefon' => $sanitizer->text($input->post->telefon),
    'mobil' => $sanitizer->text($input->post->mobil),
    'email' => $sanitizer->email($input->post->email),
    'comments' => $sanitizer->textarea($input->post->comments),
    ); 

// check if the form was submitted
if($input->post->submit) {

    // determine if any fields were ommitted or didn't validate - Ryan, alle Felder müssen ausgefüllt werden
    /*foreach($form as $key => $value) {
        if(empty($value)) $error = "<p class='error'>Please check that you have completed all fields.</p>";
    }*/
	
	// nur bestimmte required/ DSGVO
	if (empty($input->post->email)) $error = "<p class='error'>Bitte eine E-Mail-Adresse angeben</p>";

    // if no errors, email the form results
    if(!$error) {
        $msg =
			"Vorname: $form[vorname]\n" . 
			"Name: $form[name]\n" . 
			"Organisation: $form[orga]\n" .  
			"Funktion: $form[funktion]\n" .
			"Für wen: $form[gruppe]\n" .
			"Anzahl Personen: $form[anzahl]\n" .
			"Wunschtermin: $form[termin1]\n" .
			"Alternativtermin: $form[termin2]\n" .
			"Wunschzeit: $form[zeit]\n" .
			"Telefon: $form[telefon]\n" .
			"Telefon mobil: $form[mobil]\n" .
            "E-Mail: $form[email]\n" . 
            "Zusätzliche Angaben: $form[comments]";
            // "Kontaktart: $form[kontaktart]";  

        mail($emailTo, "Neue Anfrage von $form[vorname] $form[name], $form[orga]", $msg, "From: $form[email]");

        // populate body with success message, or pull it from another PW field
       echo $page->section_text = "<h2>Vielen Dank, Ihre Anfrage wurde gesendet.</h2>"; 
        $sent = true;	
    }
}

if(!$sent) {

    // sanitize values for placement in markup
    foreach($form as $key => $value) {
        $form[$key] = htmlentities($value, ENT_QUOTES, "UTF-8"); 
    }

    // append form
	if($page->section_title) {
							echo "<h3>{$page->section_title}</h3>";
							}
							 if($page->section_text) {
							echo "{$page->section_text}";
							}
    echo <<< _OUT

        $error
        <form action="./" method="post">
	<div>
        <label>
			<span>Vorname</span>
        	<input type="text" id="vorname" name="vorname" value="$form[vorname]">
		</label>
	</div>
	...
	...

 

18 hours ago, Jan Romero said:

because $input->post->email() is also the name of a sanitizer method

Ryan also checks with this method for input that doesn't validate. I also need that, don't I ?

Link to comment
Share on other sites

error output works without

class='error'

!

Why? need that format

 

also, your correction

On 6/13/2022 at 8:37 PM, Jan Romero said:
$input->post('email')

only works without single quotes :

$input->post->email

something wrong with those single quotes?

Link to comment
Share on other sites

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...