Jump to content

Saving page title via API fails for user creating more than one page


Xonox
 Share

Recommended Posts

Hi,

I have a site that has events where the users can make a reservation and something really weird is happening. This is the code:

$form = array(
	'name' => $sanitizer->text($input->post->name),
	'email' => $sanitizer->email($input->post->email),
	'phone' => $phone,
);

$p = new Page();
$p->template = 'reservation';
$p->parent = wire('pages')->get('/agenda/' . $sanitizer->text($input->post->page_target) . '/');
$p->name = microtime();
$p->title = $form['name'];
$p->save();

// CODE TO SEND EMAIL BELOW

The user path is:

1. User fills the form with name, email, phone and some more information.

2. Form is validated to see if every information is ok.

3. Information is saved into a new page (reservation template).

4. Send email to user.

Everything works fine except in one case:

When the same user fills the same form more than once (to add reserves for one or more friends), usually using the same number and email, the friend's name is not filled in the "title" field. This seems to be the only field that fails, it shows empty on the backoffice. However, the email sent to the user has the name (using the same $form['name'] variable).

Am I missing something?

Link to comment
Share on other sites

Hi @Xonox,

A couple of things stand out at first glance.

The naming convention is confusing to me. Specifically, the use of 'name'. For example, 'name' is a property of the input form, and any other form element, such as input fields, and it's value is what gets submitted with the form. Could the $input->post->name field be firstname, or username, or document name, or something uniquely identifying that specific field?

Then there is the use of the '$form' variable. I understand what you are doing, but it is very confusing to read. This could be $inputForm, or whatever you think is more meaningful.

Last thing is the 'parent' property of your new page. If the $input->post->page_target value turns out not to be valid, then the save function would fail. Why is a 'known' value being passed through input processing? Or is this a select field with only known pages as valid options?

Sorry for my confusion. It's my first cup of coffee. :)

 

Link to comment
Share on other sites

4 minutes ago, rick said:

Sorry for my confusion. It's my first cup of coffee. :)

Ok, let me see if I can shed some light into this.

1. The user name (Rick, for instance), is saved into the "title" field ($page->title);

2. The page name is a microtime to make sure that it is unique ($page->name = microtime());

3. As for the page_target, it's a hidden field in the form to make sure that the user reservation (Rick) is associated to the right event (Get a Cup of Coffee). ;)

Now the problem of not storing the value in the $page-title, only usually happens when the user (Rick) tries to add a friend (Xonox) to the same event (Get a Cup of Coffee) by filling the same form again with his email and phone number.

The strangest thing is that neither Rick's or Xonox' name are added to the reservation. However, when Rick gets the two emails, both names are correct.

Link to comment
Share on other sites

Ok, on my second cup of coffee now. :)

So the desired resulting structure is like so:

/agenda/
  /get-a-cup-of-coffee/
    /rick
    /xonox

$p = new Page();
$p->of(false); 
$p->template = 'reservation';
$p->parent = wire('pages')->get('/agenda/' . $sanitizer->text($input->post->page_target) . '/');
$p->name = microtime();
$p->title = $form['name'];
$p->save();
Edited by rick
accidentally saved before completed.
Link to comment
Share on other sites

Also, if I (user:Rick) am getting two emails, the first is my confirmation, and the second is for user:Xonox confirmation, then I either entered my own email both times, or your code is not reading the email field value with the second form submission.

Are you using the current user anywhere to retrieve any information, such as the email address?

Link to comment
Share on other sites

1 hour ago, rick said:

Are you using the current user anywhere to retrieve any information, such as the email address?

I am not using current user or saving any kind of session whatsoever. 

About the structure, you nailed it.

The user gave the same email for the two reservations. If Rick did both the reservations, he inserted the same email, because he doesn't know Xonox' email. So Rick did indeed receive both emails.

The variable $form['name'] has the name, or else it wouldn't show up on the sent email.

Every other information is saved, just not the $page->title.

1 hour ago, rick said:

$p->of(false);

What does this do?

Link to comment
Share on other sites

WAIT!!!!

I've got news. After cheking out the reservations closely, I found out that the title is being stored, hidden away on a different language from the default. So it's saved on the right field, but on the wrong language. How come?

Link to comment
Share on other sites

6 minutes ago, rick said:

That turns off output formatting, as described here.

Thanks I just added that to my code.

Turns out the user name ($page->title) was hiding on a different language. :huh:

So I guess I'll be using this to make sure it is stored on the default language:

$p->title->setLanguageValue('default', $form['name']);

However, is it possible for PW to automatically jump to new language?

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