Update Page On frontend


#1

I know how to create and/or a page on the front end. But it updates and writes over the original data. How can i continually add data to a field?

I want to be able to submit a number on the front end and it be continually added to a list in the backend.


#2

What type of field are we talking about? You can always get the current content of the field and then add the new content to it.


#3

Whichever field would be best really. Not sure. I just want users to be able to submit number from a list of pre-defined ones and then i want to be able to get a total of all of those numbers.


#4

Then get the current value, add the posted value, and update the field with the sum.


#5

Brilliant thank you :slight_smile:


#6

How can I update, on the front end, a field that’s page is determined by a form on the front end?

 try {
 page('countries')->children()->page($data['country'])->update(array('days' => $data['days'],));
 redirect::home();
 } catch(Exception $e) {
 echo $e->getMessage();
 }

This is what I have, but doesn’t work.


#7

Is $data['country'] the page you want to update?


if($page = page('countries')->children()->find($data['country']) {
  $fieldValue = $page->days()->toInt();
  $newValue = $fieldValue + intval($data['days']);
  try {
    $page->update(
      [
        'days' => $newValue
      ]);
  } catch(Exception $e) {
 echo $e->getMessage();
 }
}

(provided $data['country'] is the id of the page you want to update)


#8

Hiya, thanks for the reply. Unfortunately this doesn’t seem to work. Nothing happens when I try to update data.


#9

Well, do some. dumps to check where it goes wrong, maybe the page doesn’t. exist or you don’t get the right data from your post, without knowing what exactly. you are posting etc., it’s a. bit hard and you have to figure it out.


#10

Thanks for this, played about a bit and fixed :slight_smile: Much appreciated for your time and patience.


#11

Hi

I’m having trouble with the updating and adding of days. It constantly returns different values and never just adds up the numbers added.
https://arrivals.melodyjane.com/ - please try.

My code is as follows…

<?php 

return function($site, $pages, $page) {
$error = null;
if(r::method() === 'POST') {

$data = array(
'country' => get('country'),
'days' => get('days')
);
$fieldValue = $page->days()->toInt();
$newValue = $fieldValue + intval($data['days']);
// Validate the data
if($data) $error = 'Something is invalid.';

try {
$page = page('countries')->children($data['country'])->create(str::slug($data['country']) . '/' . substr( sha1( time() ), 0, 8 ) , 'info', $data);
$page = page('countries')->children()->find($data['country'])->update(['days' => $newValue]);
} catch(Exception $e) {
echo $e->getMessage();
}

}

return compact('error');

};

#12

What is that line doing there? I think I wrote before that the children() method doesn’t accept a parameter.

And this line:

if($data) $error = 'Something is invalid.';

doesn’t seem to make sense, either… What do you want to achieve here? In your comment says you validate the data, but in fact you fill the error variable if $data is true, hm…

Also, if you want to validate your data, you should do that before you use the data, not after. for example, check if days is numeric and in the range of allowed values, and check if the value for country is in the array of allowed countries. Anyone can easily modify the data that is sent to the. server.

And one more thing: If you want to get the average arrival time, it doesn’t make sense to add the values up. You would have to calculate an average…


#13

Hi

$page = page(‘countries’)-&gt;children($data[‘country’])-&gt;create(str::slug($data[‘country’]) . ‘/’ . substr( sha1( time() ), 0, 8 ) , ‘info’, $data);

This actually creates a folder underneath the country as a new “entry” on arrival time. It does actually work okay…

I do need to look into how to validate data, I’m a total novice to all this unfortunately.

And one more thing: If you want to get the average arrival time, it doesn’t make sense to add the values up. You would have to calculate an average…

I was going to use the total count of days divided by how many entries are under the country. So a count of the folders under “Australia” for example.


#14

Yes, because the parameter you pass to the children() method is just ignored. This would achieve exactly the same:

$page = page('countries')->children()->create(str::slug($data['country']) . '/' . substr( sha1( time() ), 0, 8 ) , 'info', $data);

If you store the data in those subpages anyway, you might as well retrieve it from there instead of also updating the main page itself.

But it would be better to only update the page with the new information without creating all those subpages that just clatter the folder for nothing. For example, you use a new counter field for the number of submissions (I,e, increase by 1 for each new submission, and you add up the days in the days field. Then you can easily display your average in the template.