Jump to content

Create page from frontend by anonymous user


webhoes
 Share

Recommended Posts

Hello,

I want to make a from that allows a site visitor to add a sighting. They should enter location (name of the page),  a acountry (from select), a subspecie (from select), a remark (text). Also but not implementend a location with leaflet module and a image. Not sure how to do that yet.

This is what I have. I already creates a new page on refresh and also only takes the correct template. pagename and other fields are not passed.

Please help me to correct this.

  <h4>Add a sighting</h4>
  <form  method="post">
      Location <input type="text" name="name"><br>
      Country <select name="country">
  <?php foreach($pages->get(1022)->children as $country)
  {
      echo '<option value="' . $country->id . '>' . $country->title . '</option>';
  } ?>
</select>
      Subspecie <select name="subspecie">
          <?php foreach($pages->get(1027)->children as $subspecie)
          {
              echo '<option value="' . $subspecie->id . '>' . $subspecie->title . '</option>';
          } ?>
      </select>
  Remark <input type="text" name="body">
      E-mail: <input type="text" name="email"><br>
      <input type="submit">
  </form>

  <?php

  $p = new Page();
  $p->setOutputFormatting(false);
  $p->parent = $sanitizer->selectorField($_POST['country']); 
  $p->template = 'population'; // example template

  $p->title = $sanitizer->pageName($_POST['name']); 
  $p->name = $sanitizer->pageName($_POST['name']); 
  $p->body = $sanitizer->text($_POST['body']);
  $p->save();
  echo "page ID {$p->id} created!<br>"; ?>
Link to comment
Share on other sites

Hi Webhoes,

Hope you are doing good :)

Please find the below corrected piece of code.

 

<?php

namespace ProcessWire;

/**
 * @global Sanitizer  $sanitizer
 * @global Inputfield $input
 * @global Page       $page
 * @global Page       $pages
 */

//if user has send a form you can use $input rather that $_POST
if ($input->post->aFormFieldName) {
    //create page if form submitted
    $p = new Page();
    $p->setOutputFormatting(false);
    $p->parent   = $sanitizer->selectorField($_POST['country']);//Country should exists in the page tree
    $p->template = 'population'; // example template

    $p->title = $sanitizer->pageName($input->post->name);
    $p->name  = $sanitizer->pageName($input->post->name);
    $p->email  = $sanitizer->pageName($input->post->email);
    $p->body  = $sanitizer->text($input->post->body);
    $p->save();

    echo "page ID {$p->id} created!<br>";

    return;  // stop with template code here
}

?>
    <h4>Add a sighting</h4>
    <form method="post" action='<?php echo $page->url; ?>'>
        Location <input type="text" name="name"><br>
        Country <select name="country">
            <?php foreach ($pages->get(1022)->children as $country) {
                echo '<option value="' . $country->id . '>' . $country->title . '</option>';
            } ?>
        </select>
        Subspecie <select name="subspecie">
            <?php foreach ($pages->get(1027)->children as $subspecie) {
                echo '<option value="' . $subspecie->id . '>' . $subspecie->title . '</option>';
            } ?>
        </select>
        Remark <input type="text" name="body">
        E-mail: <input type="text" name="email"><br>
        <input type="submit" name="aFormFieldName" />
    </form>

Please check if it helps.

  • Like 2
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...