ZionBludd Posted May 17, 2016 Share Posted May 17, 2016 I've been trawling through the forums and can't seem to find a working example to fix my problem. I have a field called "lp_item" that is displayed on a page, and I want the user to be able to edit this field from the frontend. I've seen Soma's example and also Frendi, but quite like the idea of been able to use the API to do this. Here is my form code <form class="form form-control" accept-charset="utf-8" action="./" method="post"> <label for="lp_item">Lost Property item <input type="text" name="lp_item"/> <input type="submit" name="form_submit" value="Submit"/> </form> And I have this below the form <? if($input->post->form_submit) { $u->save(lp_item); $u->save(); $session->redirect('./'); } else { echo "test"; } ?> Keep getting a Error: Call to a member function save() on a non-object Any ideas, please? Link to comment Share on other sites More sharing options...
kongondo Posted May 17, 2016 Share Posted May 17, 2016 What is $u? The eror says $u is not an object...I am guessing you is for user? You also need to sanitize inputs before saving them, e.g. the 'lp_item' 1 Link to comment Share on other sites More sharing options...
adrian Posted May 17, 2016 Share Posted May 17, 2016 What is $u meant to be? Is that the current user? Assuming it is and lp_item is a field on the user template, then try this: $user->lp_item = $sanitizer->text($input->post->lp_item); // change the sanitizer type as appropriate: https://processwire.com/api/variables/sanitizer/ $user->save('lp_item'); $session->redirect('./'); 1 Link to comment Share on other sites More sharing options...
ZionBludd Posted May 17, 2016 Author Share Posted May 17, 2016 What is $u meant to be? Is that the current user? Assuming it is and lp_item is a field on the user template, then try this: $user->lp_item = $sanitizer->text($input->post->lp_item); // change the sanitizer type as appropriate: https://processwire.com/api/variables/sanitizer/ $user->save('lp_item'); $session->redirect('./'); Thanks alot for your reply. I've seen your work around the forums alot! I picked up $u from a code example in the forums. If I am wanting to just simply save the data that is coming from my form, what do I need to put in the first ($) field. Link to comment Share on other sites More sharing options...
adrian Posted May 17, 2016 Share Posted May 17, 2016 You have to choose a page to save data to. The template for the page has to have the lp_item field in it. You could create a new page for each form submission - maybe like this: $p = new Page(); $p->template = "my-form-template" //use the name of the template that has the lp_item and other required fields in it. $p->parent = $page //save as child of the current page, or choose another page here $p->lp_item = $sanitizer->text($input->post->lp_item); // change the sanitizer type as appropriate: https://processwire.com/api/variables/sanitizer/$p->save('lp_item'); $session->redirect('./'); 1 Link to comment Share on other sites More sharing options...
ZionBludd Posted May 17, 2016 Author Share Posted May 17, 2016 You have to choose a page to save data to. The template for the page has to have the lp_item field in it. You could create a new page for each form submission - maybe like this: $p = new Page(); $p->template = "my-form-template" //use the name of the template that has the lp_item and other required fields in it. $p->parent = $page //save as child of the current page, or choose another page here $p->lp_item = $sanitizer->text($input->post->lp_item); // change the sanitizer type as appropriate: https://processwire.com/api/variables/sanitizer/ $p->save('lp_item'); $session->redirect('./'); This a great example. In my case, I am building a lost property register that all the staff will have access to. I'm wanting them to be able to goto a page and be able to edit/update the contents of that specific page - e.g. Specific fields - via a form on that page - not create more pages (In this case) Link to comment Share on other sites More sharing options...
adrian Posted May 17, 2016 Share Posted May 17, 2016 I'm wanting them to be able to goto a page and be able to edit/update the contents of that specific page - e.g. Specific fields - not create more pages (In this case) Ok, so just save the field on the current page: $page->of(false); //sorry, I forgot this in the above examples $page->lp_item = $sanitizer->text($input->post->lp_item); $page->save('lp_item'); You can make it even shorter with: $page->setAndSave('lp_item', $sanitizer->text($input->post->lp_item)); That's it! Don't forget though if you have multiple users editing the same field on the same page you may want to deal with possible conflicts if they are editing at the same time. 1 Link to comment Share on other sites More sharing options...
ZionBludd Posted May 17, 2016 Author Share Posted May 17, 2016 Ok, so just save the field on the current page: $page->of(false); //sorry, I forgot this in the above examples $page->lp_item = $sanitizer->text($input->post->lp_item); $page->save('lp_item'); You can make it even shorter with: $page->setAndSave('lp_item', $sanitizer->text($input->post->lp_item)); That's it! Don't forget though if you have multiple users editing the same field on the same page you may want to deal with possible conflicts if they are editing at the same time. Thanks a lot for this - It has helped immensely. Also with my overall understanding of PW now. One thing I am curious of - Is it possible to display "Saved" and the then redirect to another page. Each time I try a $session->redirect, it seems to pick it up regardless if anything has been passed thru the form. Link to comment Share on other sites More sharing options...
kongondo Posted May 17, 2016 Share Posted May 17, 2016 (edited) $message('your message');// or $error('error message'); $session->redirect('./') You may want to consider handling situations where the page was not successfully saved or other form errors occurred... Edited May 17, 2016 by kongondo edit 1 Link to comment Share on other sites More sharing options...
modifiedcontent Posted May 19, 2017 Share Posted May 19, 2017 I keep running into $p = new Page(); and $u = new (); , but how do you update an existing page or user? 'current Page()'? Do you have to define $p = current page? What is the proper process? Edit: Nevermind. I guess this is the basic process to update an existing user? Spoiler ?php if($input->post->updatename) { $user->of(false); $user->fullname = $sanitizer->text($input->post->fullname); $user->save(); } ?> <form method='post'> <label>Full Name</label> <input type="text" name="fullname" value="<?php echo $user->fullname; ?>" /> <button type="submit" name="updatename" value="Send">Update Profile</button> </form> Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now