Jump to content

Minimum form example


modifiedcontent
 Share

Recommended Posts

I am trying to wrap my head around how to store data from a form into the database in Processwire - late Tuesday afternoon, groggy...

I know there are lots of examples of 'simple forms using the API' and ' simple contact forms' and front-end edit forms 'with file upload' etc.

Soma's original post from 2012 goes on with a nine page thread. It is hard to trace back what basic steps I should follow for PW3 in 2017.

Let's say I want to collect the following data from visitors to my site:

 

favorite color: ... 

spirit animal:  ...

submit button

 

How would that work in Processwire? What are the basic steps? No mailing, no file upload, just storing data in a database. What is the PW way to do that? Minimal example?

I have installed the FormHelper module. When I try to use that with $fh->create(); it echoes the word 'InputfieldForm' in the page. Is that another module?

I'll report back with the solution when I figure it out.

Link to comment
Share on other sites

@modifiedcontent Say you have the following form 

<form method="post">
  <input type="text" name="name">
  <input type="textarea" name="message">
  <input type="submit" name="submit">
</form>

You can save it to a page like this

<?php

if($input->post->submit) {
	$p = new Page();
	$p->template = "template_to_save_form";
	$p->parent = $pages->get("/parent-page/");
	$p->title = $input->post->name . " - " . date("Y-m-d");
	$p->submitted_by = $input->post->name;
	$p->message = $input->post->message;
	$p->save();
}

You'll have to create the template, its fields and the parent page first.

Also before saving the page make sure to validate/sanitize the input.

<?php
$p->submitted_by = $sanitizer->text($input->post->name);
// instead of just 
$p->submitted_by = $input->post->name;

https://processwire.com/api/ref/sanitizer/

  • Like 6
  • Thanks 1
Link to comment
Share on other sites

Perfect! This outline makes sense to me. This is just what I needed. Thank you so much fbg13!

I created one template 'form', with a few test fields. Then created one page called 'survey', put this in:

Spoiler

<form method="post">
  <input type="text" name="fullname" placeholder=fullname>
   <input type="text" name="random" placeholder="random test field">
  <textarea type="textarea" name="message" placeholder=message></textarea>
  <input type="submit" name="submit">
</form>

<?php
if($input->post->submit) {
	$p = new Page();
	$p->template = "form";
	$p->parent = $pages->get("/survey/");
	$p->title = $sanitizer->text($input->post->fullname);
	$p->Random_Field = $sanitizer->text($input->post->random);
	$p->message = $sanitizer->text($input->post->message);
	$p->save();
}

foreach($page->children() as $feedback) {
	echo $feedback->title . ',';
	echo $feedback->Random_Field . ',';
	echo $feedback->message . '<br>';
}
?>

 

That works as basic demo. Here is a version with a success message and confirmation email:

Spoiler

<form method="post">
  <input type="text" name="fullname" placeholder=fullname>
   <input type="email" name="email" placeholder="email">
  <input type="submit" name="submit">
</form>

<?php
if($input->post->submit) {
	$p = new Page();
	$p->template = "signup";
	$p->parent = $pages->get("/signup/");
	$p->title = $sanitizer->text($input->post->fullname);
	$p->fullname = $sanitizer->text($input->post->fullname);
	$p->email = $sanitizer->text($input->post->email);
	$p->save();
	
	echo '<p>Data successfully submitted</p>';
	$mail->send($p->email, $from, 'Subject line from Signups', 'Thank you for signing up');
}

 

 

Link to comment
Share on other sites

  • 4 years later...
On 3/7/2017 at 11:50 PM, fbg13 said:

@modifiedcontent Say you have the following form 

<form method="post">
  <input type="text" name="name">
  <input type="textarea" name="message">
  <input type="submit" name="submit">
</form>

You can save it to a page like this

<?php

if($input->post->submit) {
	$p = new Page();
	$p->template = "template_to_save_form";
	$p->parent = $pages->get("/parent-page/");
	$p->title = $input->post->name . " - " . date("Y-m-d");
	$p->submitted_by = $input->post->name;
	$p->message = $input->post->message;
	$p->save();
}

You'll have to create the template, its fields and the parent page first.

Also before saving the page make sure to validate/sanitize the input.

<?php
$p->submitted_by = $sanitizer->text($input->post->name);
// instead of just 
$p->submitted_by = $input->post->name;

https://processwire.com/api/ref/sanitizer/

This means I have to save form data as a page?

Link to comment
Share on other sites

  • 1 month later...

Things that I came across about creating/editing pages with the api:

1) Set output formatting state off, for page manipulation
$page->of(false);

2) First save page in preparation for adding files e.g. an image
$p->save();

So allow me to add 1) here to improve the above example code:

<?php

if($input->post->submit) {
	$p = new Page();
    $p->of(false);
	$p->template = "template_to_save_form";
	$p->parent = $pages->get("/parent-page/");
	$p->title = $input->post->name . " - " . date("Y-m-d");
	$p->submitted_by = $input->post->name;
	$p->message = $input->post->message;
	$p->save();
}

 

  • Like 1
Link to comment
Share on other sites

With fbg13's jump start code, now it is easy to add some extra code and formatting
and make a working form example out of it.

1) Add <?php namespace ProcessWire; on the top of your template to prevent error Class "Page" not found
2) Add action="./" to the form
3) Add labels and a <textarea> section to the form
4) Change the default named submit button into something custom e.g. Send

<?php
namespace ProcessWire;

if($input->post->submit) {
	$p = new Page();
    $p->of(false);
	$p->template = "test";  // template_to_save_form
	$p->parent = $pages->get("/test/");
	$p->title = $input->post->name . " - " . date("Y-m-d");
	$p->fullname_txt = $input->post->name;
	$p->message_txa = $input->post->message;
	$p->save();
}

?>

And the form

<form action="./" method="post">
  <label for="fname">Full name:</label><br>
  <input type="text" name="name"><br>
  <label for="message">Message:</label><br>
  <textarea name="message" rows="5" cols="20"></textarea><br>
  <input type="submit" name="submit" value="Send">
</form>

 

Enter a name, a message and hit the send button a few times
and see magically grow the number of child pages holding
the entered form data.

Now you can further process the form data like showing a success page
and emailing the form data.

 

 

 

 

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

×
×
  • Create New...