External form fun. Calling custom methods into your form.
Permalink
So, total noob here with concrete 5. So far I have only had to use the built in editing functions of conc5 but now I need to make some forms that will make use of another database.
Initially my first stumbling block is trying to populate a select field with database data.
I have two files:
concrete/blocks/external_form/form/new_single_form.php
concrete/blocks/external_form/form/controller/new_single_form.php
I have this function in my controller:
So this works fine if added to the form directly. But when added as a method to the controller I cant seem to call it. I have found many posts but they all seem pretty old and the suggestions result in various errors.
Again please be gentle my php and concrete5 knowledge is new and minimal.
I have tried the following:
Result: Call to undefined method Concrete\Block\ExternalForm\Controller::select_model()
Next
Result: Call to undefined method Concrete\Block\ExternalForm\Controller::select_model()
Result: Call to undefined method Concrete\Core\Block\View\BlockView::select_model()
Result: Call to undefined method Concrete\Core\Block\View\BlockView::select_model()
The top of the controller is:
So I have probably missed something obvious here. A lot of the info seems pre 5.7 but then again maybe 5.7 didnt change this area much.
Any help would be greatly appreciated.
Thank you
Initially my first stumbling block is trying to populate a select field with database data.
I have two files:
concrete/blocks/external_form/form/new_single_form.php
concrete/blocks/external_form/form/controller/new_single_form.php
I have this function in my controller:
public function select_model() { $conn = new MySQLi("IP", "DB User", "DB Pass", "DB")or die("MySQL Connection Error"); $sql= "SELECT model FROM models"; $result = $conn->query($sql); echo " <div class='form-group field field-select '> <label class='control-label'>Car Model </label> <select class='form-control' name='c_model' id='q9' > <option value='$car_model'>---</option>"; while($row = $result->fetch_assoc()) {$car_model = $row['model']; echo "<option value='$car_model'>$car_model</option>";} echo "</select></div>"; }
So this works fine if added to the form directly. But when added as a method to the controller I cant seem to call it. I have found many posts but they all seem pretty old and the suggestions result in various errors.
Again please be gentle my php and concrete5 knowledge is new and minimal.
I have tried the following:
echo $controller->select_model();
Result: Call to undefined method Concrete\Block\ExternalForm\Controller::select_model()
Next
$controller->select_model();
Result: Call to undefined method Concrete\Block\ExternalForm\Controller::select_model()
$this->select_model();
Result: Call to undefined method Concrete\Core\Block\View\BlockView::select_model()
$this->select_model();
Result: Call to undefined method Concrete\Core\Block\View\BlockView::select_model()
The top of the controller is:
namespace Concrete\Block\ExternalForm\Form\Controller; use Concrete\Core\Controller\AbstractController; class NewSingleForm extends AbstractController
So I have probably missed something obvious here. A lot of the info seems pre 5.7 but then again maybe 5.7 didnt change this area much.
Any help would be greatly appreciated.
Thank you
Thank you so much for taking the time to help me and the quick response.
Your suggestion definitely has helped me resolve the calling of methods within the default external form controller.
Jay
Your suggestion definitely has helped me resolve the calling of methods within the default external form controller.
Jay
Hi GreymanUK,
I believe it is best practice to use concrete5 methods for accessing databases.
"Accessing the Database to Make Queries"
http://documentation.concrete5.org/developers/database-management/a...
"Connecting to Multiple Databases"
http://documentation.concrete5.org/developers/database-management/c...
I believe it is best practice to use concrete5 methods for accessing databases.
"Accessing the Database to Make Queries"
http://documentation.concrete5.org/developers/database-management/a...
"Connecting to Multiple Databases"
http://documentation.concrete5.org/developers/database-management/c...
In the controller file, add this function that fires when the form is viewed.
and then call your controller functions with $ctrl->select_model()
'ctrl' can be anything you want but NOT 'controller' which was usable in 5.6