Jump to content

Val_0x42

Members
  • Posts

    8
  • Joined

  • Last visited

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

Val_0x42's Achievements

Newbie

Newbie (2/6)

7

Reputation

  1. Ah! Right! I'll do some safety checks right away as you suggested, I'm also restructuring the site a little so my files will change, but this will still be the core method I'll use to update my fields. Thanks for the answer!
  2. Awesome, this worked like a charm! I'm posting the code in case someone else needs it. Thank you very much @horst for taking the time to write the detailed solution (and for letting me know about the Fetch Web-API). I realize that that I'm diving head-first in some unexplored waters (AJAX and stuff) and I'll try to read some more documentation so that I won't try again an outdated solution! This is the ajax.php file: While this is the JS file with the solution provided by @horst with some slight edits (variable names):
  3. A little update: the project is going pretty well but I'm stuck on the final step of the precedure (updating the values). I implemented the AJAX call as suggested in this other post And everything works fine except I can't pass the variables i need to the php code that update fields. I think there's something wrong with how I try to collect them. To give anyone that may be interested a little context here's the JS with the AJAX call: document.addEventListener('DOMContentLoaded', function(event) { const checkboxes = document.getElementsByClassName("valucheckbox"); for (i=0; i<checkboxes.length; i++){ checkboxes[i].addEventListener('change', function(event){ var ckVal = this.value; var ckStatus = this.checked; $.ajax({ type: "GET", url: "https://valuvato.it/ajax", data: { pageid: ckVal, status: ckStatus }, success: function(){ console.log(this.url); } }) }); } As you can see I made it return the URL so that I could check the variables that are passed and it seems everything's fine, as the URL shows as follows: https://valuvato.it/ajax?pageid=1092&status=true And this is the php code that is executed by the call: <?php include("./index.php"); //$page_id = (int) wire('input')->get->pageid; //$status = (text) wire('input')->get->status; $page_id = $input->get('pageid', 'int'); $status = $input->get('status', 'text'); //$page_id = $_GET['pageid', 'int']; //$status = $_POST['status', 'text']; $p = wire('pages')->get($page_id); $user->of(false); //$user->sorceries_checked->add(1093); if($status == 'true') { $user->sorceries_checked->add($p); } else { $user->sorceries_checked->remove($p); } $user->save(); ?> As you can see I left some stuff I tried to fetch the variables as a comment. Without the variables the code is working properly, for instance the line $user->sorceries_checked->add(1093) works, but the remaining code never executes because $status and $page_id never get initialized. I'm pretty sure I'm failing to retrieve them properly, as I've tried to do the sorceries_checked->add($p) without the IF part and still doesn't work. I don't know if this can be related to the fact that I'm not properly calling the .php file but a page that has that .php file as a templeate, as was suggested in the post I linked before. Maybe there's a simpler way to do this and I've missed it. Any thoughts? As usual thanks for the help ^^
  4. Very nice! That's probably what I'll try! I've literally never used AJAX so I'll have to see how to send the info to the server but I like this implementation! I'll try to code this first and then if I manage to get it working I was thinking that the next step could be making the AJAX call only before leaving/updating/changing the page (if that's possible), maybe storing checkbox statuses in an array and then updating all of them at once so that every user will only make a call after they've finished editing and not every time they check/uncheck. I'm only familiar with "simple" JS listeners and I don't know how difficult it could be to implement this tho.
  5. That's very interesting! Thanks for the resource link, I'll definetly check it out and let you know!
  6. Hey horst, thanks for the reply! Ok I'm happy to see that the solution is pretty much what I thought except for the form part (I was using simple checkboxes). I'll try to manage checkboxes as a form and see how the whole submission mechanic works! I'd also like to make it so it would update "live" and the users wouldn't be forced to submit the checks (by save button I mean) before changing page and risk losing their data. For now I'll start simpler and build up from the easiest working solution. May I ask if there's a specific reason why you suggested to separate processwire users from the visitors? I thought that having them as standard users would have made it simpler to check who was logged in and get their selections. Also I'm wondering if there's maybe a way to trigger form submission on page change so that the user experience would flow more easily. In the meantime I'll start searching on how to code the solutions you suggested, thanks!
  7. Hi everyone, I hope this hasn't been covered anywere else but I tried to search for this topic and I couldn't seem to be able to find anything. I'm fairly new to Processwire and I'm liking it a lot but I don't understand what could be the optimal way to do a little project I'm working on, in particular how to store user-submitted data. Basically I'm trying to build a checklist website where users can flag a list of items as "collected" so that they can keep track of what they have so far. I'm wondering what could be the best way of storing this user-dependant data. For now I've organized the site as follow: Every item is a page with specific fields (name, description ecc...) Every user has a custom field that's a list of items checked, and for this I used a page reference field On page load the site should check which items result as collected and tick the corrisponding checkbox The list of items is presented as a table where each row starts with a checkbox, this is not a checkbox field since if I would mark this as "checked" this would be associated with the item and not with the user resulting in the item being checked for all of them (correct me if I'm wrong). I'm struggling on how I can collect the data from the frontend (which checkoxes are marked) and edit the user field so that I can store them and retrieve it later on page load (depending on the account who is currently watching the checklist). I was thinking of putting a javascript listener on the checkboxes who calls a php function to set the corrisponding user value but this seems a little convoluted and I'm wondering if there could be a better way to handle this, something I'm not seeing right now. If you wanna take a look you can find the checklist here: https://valuvato.it/spells/sorceries/ (yes it's a collection of Elden Ring stuff so one can mark what he has already looted and what's missing in his gameplay). Any idea would be appreciated, even if it changes the architecture of the project, I'm using this to learn Processwire after all! Thanks in advance ?
  8. @bernhard thank you very much this solution worked perfectly!
×
×
  • Create New...