Jump to content

Create a Page via a form from frontend


louisstephens
 Share

Recommended Posts

As the title states, I am trying to process a form and save the form's data as a new page. I have looked at Soma's post, but it seems (as least to me), that it tackles submission forms etc. I know what I am trying to do is very very similar.

 

The Form:

    <form role="form" method="post" action="./" >
          <div class="form-group">
            <label for="code">Enter 4-Digit Code:</label>
            <input type="code" class="form-control" id="code">
          </div>
          <button type="submit" name="submit" class="btn btn-default">Submit</button>
        </form>

And the processing:

if($input->post->submit) {
  $code = Trim(stripslashes($_POST['code']));
  $p = new Page();
  $p->setOutputFormatting(false);
  $p->template = 'singular'; // example template
  $p->parent = wire('pages')->get('/'); // example parent
  $p->name = $code; // example name
  $p->title = "Test Title";
  $p->save();
  echo "page ID {$p->id} created!<br>";


  }
  else {
    // output contact form
    echo "Negative Ghost Rider";
  }
?>

I have been scratching my head as to why this doesn't seem to work for about an hour. I am sure I must be missing something, or trying to go about this the wrong way. The form seems like it submits, but there is no page added to the backend.

Link to comment
Share on other sites

Your code seems to be right. Only things I would recommend:

  • If you want to use a number as input, there is an input type number for that.
  • You could get and sanitize the input using the API: 
  • $code = $sanitizer->pageName($input-post('code'));

Have you tried your code with static values without the form? If it works this way, it may be your form.

Regards, Andreas

Link to comment
Share on other sites

Hey AndyZyk, it appears to be an issue with form. Ill try to get down to the issue with it and post back in a while.

Annnd, I got it.. A simple if(isset( $_POST['submit'])) fixed everything. I should have thought of that sooner.

Edited by louisstephens
Corrected the if statement and all works accordingly
Link to comment
Share on other sites

I don't see where are you declaring $input->post->submit , from the form code, I would say that you need to add to the input inside the form (the one that is type="code") a name attribute which is what is actually passed as a post/get variable. And then you should rather be checking for $input->post->name (or maybe $input->post works?). Also, check your browser's Network log to see if it is actually a POST and not a GET.

  • Like 1
Link to comment
Share on other sites

Ah sorry, elabx, I didnt quite update the code in the post:

if(isset( $_POST['submit'])) {
  $code = Trim(stripslashes($_POST['code']));
  $p = new Page();
  $p->setOutputFormatting(false);
  $p->template = 'singular'; // example template
  $p->parent = wire('pages')->get('/'); // example parent
  $p->name = $code; // example name
  $p->title = "Test Title";
  $p->save();
  echo "page ID {$p->id} created!<br>";


  }
  else {
    // output contact form
    echo "Negative Ghost Rider";
  }
?>

However, how would someone upload a pdf via a field? I tried this already, but and the file looks like it has been sent through to the field, but it is showing up as "0kb"

Link to comment
Share on other sites

Your if-statement probably has not worked, because you don't have an input type submit in your form. Instead you have an button with the type submit. Then your original if-statement should work.

if ($input->post->submit) {
	// Form was submitted
} else {
	// Form wasn't submitted
}

Also I would recommend again to get and sanitize the input from code like mentioned above. Especially if you want to use it as a page name, the corresponding sanitizer is meant for this.

36 minutes ago, louisstephens said:

However, how would assign a form input to a field on the page? 

You can set a new value for an existing field using the API like this:

$p->of(false); // Shortcut for setOutputFormatting(false)
$p->set("title", $sanitizer->text($input->post->title));
$p->save();

 

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