Forums

Set value to field with PHP

Gatsman 14 Nov, 2024

Does this work in CF8

$this->data("FIELD_NAME", "SOME VALUE", true);

In CF7 it will set the value of FIELD_NAME to: SOME VALUE

Max_admin 14 Nov, 2024
Answer
1 Likes

no, this works:

$this->data["name"] = "value";
// or use a var
$this->set("name", "value");
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Gatsman 14 Nov, 2024

Max in my case this works only if the field is empty. If it has any value (in my case it's session data when i do a reload on a multipage form) it does not work.

*Actually it sets the value in the data (i can see it if i activate debug) but does not set the value in the field.

Also the clear session i used in CF7 does not work for me here.

unset($_SESSION['gcore']['testform']);

Max_admin 14 Nov, 2024
1 Likes

what's the order of your action and view ? you must run the code BEFORE the field is displayed, if you use a PHP action then it must be placed ABOVE the field

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Gatsman 15 Nov, 2024

Good to know Max, i always had a php on top on my forms to handle any field value, i will move it last.

Any advise about the unset($_SESSION['gcore']['testform']); for CF8

My goal is to clear the form and it used to work for CF7

Max_admin 15 Nov, 2024

having PHP on top is correct

When do you want to clear the form ?

in my case it's session data when i do a reload on a multipage form

What do you mean by a reload here ? how does your form work ?

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Gatsman 25 Nov, 2024

You are right Max i misunderstood. Placing the php at the very top works for setting variables, thank you.

Now for the form clear, I am testing a multipage form so here is an example:

Page 01 has some fields Field 01, Field 02, Field 03 (might have other irrelevant fields) and Next Button

Page 02 has a php check

// get values of Field 01 & Field 02
if (empty(Field 01) && empty(Field 02)) {//redirect to Page 01}

if both fields are empty it redirects to form start (Page 01) and displays a message, or if you just refresh F5 when you are at Page 02 it will redirect/reload Page 01 *(and show a message Your session has timed out or your tried to access a wrong page.)

In both cases the form will be filled with the previous data, if entered, in CF7 i could clear the form before the redirect with:

unset($_SESSION['gcore']['FORMNAMEHERE']);

Is there a way to do that with CF8?

Max_admin 25 Nov, 2024

try to use this PHP code at the top of page 1:

$this->data = [];

Does it solve the issue ?

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Gatsman 25 Nov, 2024

Yes it does thank you Max

Max_admin 25 Nov, 2024

you are welcome, let me know if you find later that it breaks something else

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
You need to login to be able to post a reply.