Jump to content

Can Someone Explan Basic Principles Of Forms?


jckhmmr
 Share

Recommended Posts

Hi, so, my first post. Really excited about processwire. I have basic understanding of php and jquery (and I mean really basic), so I thought I understood the concept fairly well. But I hit the wall with forms or any form of collecting user input. I've read a lot of posts including Soma's post about forms. It works, but I don't understand how and why.

I think the best way to state my question is: how do you get the value from the input field and save it to another page (superuser only for example). Is it $fields or $input or something else entirely? If so how and where PW saves it for retrieval? For example if 10 unregistered users left 10 comments, how will I output them on the page? Or remove some of them?

Also I've seen render() method a lot. How does this work?

Thanks.

Link to comment
Share on other sites

Hi jckhmmr,

Any value that is posted to a page can be fetched with one of the following manners:

$input->post->fieldname;
$input->post['fieldname'];
$input->post('fieldname');

For GET values simply change post to get

$input->get->fieldname;

See: https://processwire.com/api/variables/input/

Where fieldname is the name of the form input field. So when you have a form

<form method="POST" action="<php echo $page->url; ?>">
<input type="text" name="firstname">
<input type="text" name="lastname">
<input type="submit" name="send" value="send">
</form>

You can access those fields as follow:

$input->post->firstname;
$input->post->lastname;

A good thing to do always with user input is to sanitize it like this:

$firstname = $sanitizer->text($input->post->firstname);
$lastname = $sanitizer->text($input->post->lastname);

See: https://processwire.com/api/variables/sanitizer/

and welcome to ProcessWire. :)

  • Like 7
Link to comment
Share on other sites

Hi Raymond,

thank you for the reply!

For some reason 

action="<php echo $page->url; ?>">

 doesn't render page url correctly and I'm getting sent to /<php%20echo%20$page->url;%20?> page.

Also a couple of questions:

1) Does creating fields for "firstname" and "lastname" mandatory or I can just use html markup

2) How does output get saved? I mean, for example 3 users enter some data into the fields, will

$input->post->firstname;

render all of them or just the last one? How can I work with this stored data?

Thanks.

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