Welcome, Guest

Using PHP variables to populate the 'value' prop
(1 viewing) (1) Guest

TOPIC: Using PHP variables to populate the 'value' prop

Using PHP variables to populate the 'value' prop 2 years, 4 months ago #11377

I am having difficulties in using php variables that have been created in Form-->Pieces Initialisation section of a form, in the form itself.

For example:

Form -> Advanced -> More Options -> Form Pieces (Before Form - Custom)

In this I have the code:

//-------------------------------------------------
$this->execPieceByName('ff_InitLib');

//Get id from URL
$recid = JRequest::getVar('recid', -1);

if ($recid > -1) {
//Signifies editing of an existing record - let's get the appropriate record using the recid parameter passed in through the URL
$sql = "SELECT * FROM #__z_pups_for_sale WHERE id = " . $recid . " ORDER by id";
$advert_details = ff_select($sql);
//We can now use this object in our form to populate the existing values...
}
//--------------------------------------------------

I have a test field with the 'value' property set to:

<?php JRequest::getVar('recid', -1); ?>

This produces the correct result so I know that the parameter recid is being passed properly from the URL.

I also have another field with the 'value' property set to

<?php echo $advert_details->cnt_name; ?>

This produces errors saying, basically, that the variable 'advert_details' does not exist.

I'm no expert; if someone would like to try and help I would be grateful.

Thanks.
  • beaureef
  • OFFLINE
  • Junior Boarder
  • Posts: 24
  • Karma: 1
Last Edit: 2 years, 4 months ago by beaureef.

Re:Using PHP variables to populate the 'value' prop 2 years, 3 months ago #11403

OK got round the error mesagges by making the variables global in the int script, but still cannot access the $advert_detail in the form... investigating
  • beaureef
  • OFFLINE
  • Junior Boarder
  • Posts: 24
  • Karma: 1

Re:Using PHP variables to populate the 'value' prop 2 years, 3 months ago #11432

Sorted - used ff_setValue() to set the value.
  • beaureef
  • OFFLINE
  • Junior Boarder
  • Posts: 24
  • Karma: 1

Re:Using PHP variables to populate the 'value' prop 2 years, 3 months ago #11612

please delete this
  • paolodp
Last Edit: 2 years, 3 months ago by .

Re:Using PHP variables to populate the 'value' prop 2 years, 3 months ago #11786

Could you post the solution that worked for you?

I'm looking for a way to edit existing DB items and replace the values via a form. Is this what your solution accomplished?
  • cebby
  • OFFLINE
  • Senior Boarder
  • Posts: 63
  • Karma: 1

Re:Using PHP variables to populate the 'value' prop 2 years, 3 months ago #11794

cebby wrote:
Could you post the solution that worked for you?

I'm looking for a way to edit existing DB items and replace the values via a form. Is this what your solution accomplished?


The functionality I wanted was to reference variables that I set as the form was being loaded (Quickmode! [highlight the form name node] --> Advanced Tab --> More options link --> Form Pieces --> Before Form).

These variables correspond to column values in a single record in a database that I wanted to show automatically in the form when it was loaded.

The first thing I tried was to reference the variable in the value property of an element on the form - this did not work at first owing to scoping problems and my lack of knowledge of PHP - after some reading I used syntax such as <?php return $GLOBALS['variable_name'] ?> in the value property.

However I ended up using an in-built function called ff_setValue(). The following code may help - it is my code to show the selected record from a database:

$this->execPieceByName('ff_InitLib');

$GLOBALS['add_id'] = trim(JRequest::getVar('advert_id', -1));

if ($GLOBALS['add_id'] > -1) {

//Signifies editing of an existing record - let's get the appropriate record using the advertid parameter passed in through the URL

$sql = "SELECT * FROM #__z_pups_for_sale WHERE id = " . $GLOBALS['add_id'] . " ORDER by id";
$advert = ff_select($sql);

//Populate global variables needed for an update.

//Visible
$GLOBALS['cnt_name'] = $advert[0]->cnt_name;
$GLOBALS['phone_1'] = $advert[0]->phone_1;
$GLOBALS['phone_2'] = $advert[0]->phone_2;
$GLOBALS['details'] = $advert[0]->details;
$GLOBALS['title'] = $advert[0]->title;
$GLOBALS['inactive'] = $advert[0]->inactive;
$GLOBALS['county'] = $advert[0]->county;
$GLOBALS['breed'] = $advert[0]->breed;

//Set field values (update)

//Visible
ff_setValue('cnt_name', $GLOBALS['cnt_name']);
ff_setValue('phone_1', $GLOBALS['phone_1']);
ff_setValue('phone_2', $GLOBALS['phone_2']);
ff_setValue('details', $GLOBALS['details']);
ff_setValue('title', $GLOBALS['title']);
ff_setChecked('inactive', $GLOBALS['inactive']);
ff_setValue('date_created', $GLOBALS['date_created']);
}
else {

// New Record

//Hidden
$GLOBALS['userid'] = JFactory::getUser()->id;
ff_setValue('userid', $GLOBALS['userid']);
}

  • beaureef
  • OFFLINE
  • Junior Boarder
  • Posts: 24
  • Karma: 1
Moderators: TheMuffinMan, ForumSupport
Time to create page: 1.80 seconds

Facebook Discount - 40% Off!

Fans of our Facebook page here are entitled to get a 1-year-subscription for the price of a 6-months!

Steps to take:

  • Become a fan on Facebook (Like) here
  • Purchase a 6-months-subscription from here
  • Write a quick email to This email address is being protected from spambots. You need JavaScript enabled to view it. with your username and prove of your like and we'll upgrade to the 1-year-subscription

Joomla!® Forms Discount - 40% Off!

Already purchased a forms extension from a different vendor but your requirements changed and now you need a forms extension that is capable of more than just the standards? We help you to keep the costs for a change under control: Get a 40% discount on our professional membership plan! -- meaning timely unlimited access to all current and future commercial extensions, breezingforms themes & apps and Joomla!® templates at crosstec.de, including 1 year of support -- without any website limitations -- Just prove that you previously purchased another forms extension for Joomla!® from a different vendor and you are qualified.

Steps to take:

  • Purchase a 1-year-subscription from here
  • Write a quick email to This email address is being protected from spambots. You need JavaScript enabled to view it. with your username and prove of the purchase for the other form vendor and we'll upgrade to the professional-subscription

About

Crosstec GmbH & Co. KG

Bergisch-Gladbacher-Str. 829

51069 Cologne, Germany

Imprint