Jump to content

How to set variables bases on user actions?


bramwolf
 Share

Recommended Posts

Hey Guys,

I've been working with Processwire for a while now but mostly only showed the content that 

was set in the back-end of Processwire by the site admin. Now I would like to store information

but I find it hard to figure out where to start.

The problem is dat Processwire uses PHP to get and set new values, which is only a server side

language. What i want to do is have variables set bases on user behavior.

The Case: 

I'm building a really small site that sells two items. A have a product page where people can insert

a quantity of the item in a form input field and than click add to cart.

http://www.hayplaybag.com/producten

However I have no clue how to convert that information into say, a global variable or a field on the

checkout page so that I could read it again on the shopping cart page and present the amount they

ordered.

How would you guys go about doing such things? I would love to hear your thoughts :)

Thanks!

Bram 

Link to comment
Share on other sites

There are several options here. You could use $session for this. For example:

<?php

// First process your form and then set a variable

$session->product = 1234; // 1234 is the ID of your product
$session->amount = 1; // 1 is the amount ordered

// On your other template you can call these variables

echo $session->product; // This will get the ID
echo $session->amount; // This will get the amount

Be sure to $sanitize this if you want to do something with the data (i.e. save the order).

Another option is to post the form to another page and get the data from there using $input. I've used both ways and came to a conclusion that sessions are more flexible. If you want to get fancy you can even store arrays in the $session.

  • Like 1
Link to comment
Share on other sites

Hey arjen,

Thanx for the reply! I already got how I could store that info in for example session variables, but not how I could have my page do that after processing my form. Could you maybe sketch a example of how I could have the info from that form submitted into those variables after submission?

Or maybe show me how I could post the data to a different page using the form?

Thanx for your help man :)

Link to comment
Share on other sites

No problem. Checkout the following code for more insight. All written in the browser, so it's not tested. But you get the idea.



<?php

// Create a form using the API. You can thank soma >

$form = $modules->get("InputfieldForm");
$form->action = "./";
$form->method = "post";
$form->attr("id+name", "form");

$field = $modules->get("InputfieldText");
$field->label = "Name";
$field->attr("id+name", "name");
$field->required = 1;
$form->append($field);

$field = $modules->get("InputfieldHidden"); // A hidden field to populate which product should be stored in the session
$field->attr("id+name", "product");
$field->attr("value", $product->id); // You can use the value (i.e. $product->id) to save the data
$field->required = 1;
$form->append($field);

$submit = $modules->get("InputfieldSubmit");
$submit->attr("value", "Subscribe");
$submit->attr("id+name", "submit");
$form->append($submit);

if($input->post->submit) {

$form->processInput($input->post);

if($form->getErrors()) {

$out .= $form->render();

} else {

// Always sanitize user data
$name = $sanitizer->text($input->post->name);
$product = $sanitizer->text($input->post->product);

$session->name = $name; // Using $session->name you can get this data back on another page
$product->product = $product; // Idem dito

$session->redirect($pages->get("PAGE_ID")->url); // The page (PAGE_ID) you want to send the user to (i.e. the checkout page)

}
} else {

$out .= $form->render();

}

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

Possibly not fully on-topic but I have to thank @bramwolf and @arjen for asking and answering this and showing me what appears to be incorrect info in the official docs (can't quite believe that, hence seeking opinion here).

@arjen kindly shows how to set and get a PW $session variable here. This syntax has finally stopped me going round in circles, it works (yay :))

I was going round because in the docs under $session function reference the syntax is different. For example:

// @arjen
$session->product = 1234; // set a var
echo $session->product; // read a var

// docs
$session->set($name, $value) // set a var
$session->get($name) // read a var

Am I missing something basic here (probably am (probably will be embarrassed for asking))?

Link to comment
Share on other sites

Hey Alan,

Actually, both work, although they're different. This is how you would use the doc version:

$session->set(test, "test"); 

echo $session->get(test);        // outputs: test
 
 
Maybe the thing you did wrong was not echoing the get line? I think they left that out of the docs
since you can use the variable for multiple operations and not only echo it.
 
for instance,
 
$test_works = $session->get(test) . " worked out fine";  // outputs: test worked out fine
 
 
Hope this helps :)
  • 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...