Jump to content
Sign in to follow this  

Form to pages: Functions or Classes

Recommended Posts

So I have a form, once completed, will create new pages. All in all, this is eazy-peezy for me now. I guess I need a bit of guidance on how to actually structure the rest of my code.  I thought I could just write a function (_func.php) and pass the fields to the function and let it do its' thing. However, I am kinda hitting a road block when I do it this way. 

I currently am passing first name, last name, city, state (options field), and making pages based on the first/last names. I guess where I run into some issues is I am trying to check to see if the "page" already exists, and if it does, throw out an error:

In the home template:

if(isset( $_POST['submit'])) {
	$firstName =Trim (stripslashes($_POST['firstname']));
	$lastName = Trim(stripslashes($_POST['lastname']));
	$fullName =  $firstName . $lastName;
	$city = Trim(stripslashes($_POST['city']));
	$state = Trim(stripslashes($_POST['state']));
	$lowerCaseName = strtolower($fullName);
	$people = $pages->find("template=person");
	foreach ($people as $person) {
		$checkFirstName = $person->first_name;
		$checkLastName = $person->last_name;
		$checkFullName = $checkFirstName . $checkLastName;
		if ($checkFullName === $lowerCaseName) {
				echo "<p>" . "This person has already created a page. Please choose a different name." . "</p>";
			else {
				echo "hey";

		} // end foreach

In _func.php:

function processNewPerson($list) {
	$u = new Page();
	$u->template = "person";
	$u->parent = wire('pages')->get("/people/");
	$u->title = ;
	$u->first_name = ;
	$u->last_name = ;
	$u->state = ;
	$u->city = ;


I am a little unsure of how to actually pass all the information to the template, as well as if this is even the best approach to do this. Would it make more sense to do this in a class, or keep it the way it is?

Share this post

Link to post
Share on other sites
Just now, breezer said:

You should download the LoginRegister module, it has all the code you need.

I also just found these members gist which has a huge amount of samples you might find useful.



Thanks @breezer, I will definitly look into that. I do need to clarify that I am not really making a "login/register" site or really updating actual registered users on the site. Basically, as it stand right now, a registered user will fill out the form with a persons information that they will be able to keep track of certain information. I guess think of this as a glorified rollodex of contacts. I will be adding more fields to the form down the road as this evolves.

Share this post

Link to post
Share on other sites

Pass it to an associative array (not tested):

<?php namespace ProcessWire;

function processNewPerson(WireArray $data) {
    $u = new Page();    
    $u->template = "person";
    $u->parent = $pages->get("/people/");
    $u->first_name = $data["first_name"];
    $u->last_name  = $data["last_name"];
    $u->full_name = $data["first_name"] . " " . $data["last_name"];
    $u->city  = $data["city"];
    $u->name = $sanitizer->pageName($u->full_name, true);

if($input->post["submit"]) {
    $data = WireArray::new([
        "first_name" => $sanitizer->text($input->post['first_name']),
        "last_name" => $sanitizer->text($input->post['last_name']),
        "city" => $sanitizer->text($input->post['city'])
    $people = $pages->find("template=person, first_name=$data['first_name'], last_name=$data['last_name']");
    if($people->count === 0) processNewPerson($data);


Share this post

Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By marcus
      wireshell 1.0.0 is out    
      See Bea's post

      -------- Original post -----------
        Now this one could be a rather long post about only an experimental niche tool, but maybe a helpful one for some, so stay with me   Intention Do you guys know "Artisan" (Laravel) or "Drush" (Drupal)? If not: These are command line companions for said systems, and very useful for running certain (e.g. maintenance, installation) task quickly - without having to use the Admin Interface, first and foremost when dealing with local ProcessWire installations. And since it has a powerful API and an easy way of being bootstrapped into CLIs like this, I think such a tool has a certain potential in the PW universe.    It's totally not the first approach of this kind. But: this one should be easily extendable - and is based on PHP (specifically: the Console component of the Symfony Framework). Every command is tidily wrapped in its own class, dependencies are clearly visible, and so on.   ( Here was the outdated documentation. Please visit wireshell.pw for the current one )
    • By quickjeff
      Hi Guys, 
      I have been debugging a site for the last 2 hours and cannot solve the issue. 
      I have a site running on 3.0.148. 
      I installed the Kongondo Blog module and was updating the templates to include the website style. 
      Once everything was set and done, I checked the page tree to see an error appear. 
      Template must be assigned a name before 'filename' can be accessed
      The same error appears in templates. 
      Debugging Steps
      I checked the templates in the server to ensure I didnt accidentally delete the namespace.  Deleted cache in browser and server under assets Still no go. 
      Any help is appreciated. 
    • By Spyros
      I'm having a strange issue with the $page->find(), for some reason I'm missing some of the pages from the results. I found then that I was missing all the pages with the same "PAGE NAME". Is it a bug or am I missing something?
      If I change the "PAGE NAME" of one of the missing ones then I'm retrieving the page without any problem.
      Thank you
    • By benbyf
      Hi, Looking to create form elements on a page–some input with a colection of form inputs and the appropriate labels and variables for that input. I've used ProForms in the past and rolled out my own when creating simply one off forms, but I wonder if anyone has found a good way of allowing form creation on page editing so that clients can adhocly make and edit forms?
    • By snobjorn
      I'd like to set up a LazyCron function that fires every hour on each page that has a template named "post". So far I have gotten the function running. But when it has fired once on "Page with Title A", it takes and hour before it is fired again on any other pages. It makes sense, hooking the function with wire()->addHook().
      What I want to do, is to get the function to be fired with LazyCron individually for each page. So that "Page with Title A" fires every hour (if it has pageviews), and "Page with Title B" is fired individually and possibly during the same timeline, if it has pageviews.
      Is this possible? Or might there be another way to utilize a sort of LazyCron on individual pages?
  • Create New...