I am beginner to Joomla! development and have created a very simple module.
How do I create a form with 3 text fields and then save the entered values into a database table?
Try this example:
We will Post a user's first and last name to a table.
create a table in your database. Note it should have the prefix "jos_"
We will call this form, "names". So we will name our table "jos_names"
At the SQL line in PHPMyAdmin (or whatever tool you use..), do this query to create a new table:
CREATE TABLE `databasename`.`jos_names` (`id` int(11) NOT NULL auto_increment, `firstname` VARCHAR(100), `lastname` VARCHAR(100), PRIMARY KEY (`id`) )
To simplify things, we will post the results to the same page.. Let's build the form:
<?php
/** post form to db module **/
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
//--POST YOUR FORM DATA HERE-->
$fname = $_POST['fname'];
$lname = $_POST['lname'];
//--END POST YOUR FORM DATA---|
//--build the form------------>
?>
<form name="names" id="names" action="<?php echo JURI::current(); ?>" method="post">
<p><input type="text" name="fname" id="fname" value="" /></p>
<p><input type="text" name="lname" id="lname" value="" /></p>
<p><input id="submit" name="submit" type="submit" value="Submit Names" /></p>
</form>
//--END BUILD THE FORM--------|
<?
if( (isset($lname)) || (isset($fname)) ) {
//first name or last name set, continue-->
$data =new stdClass();
$data->id = NULL;
$data->firstname = $fname;
$data->lastname = $lname;
$db = JFactory::getDBO();
$db->insertObject('#__names', $data, id);
} else {
echo '<h4>One Field Is Required!</h4>';
}
?>
That should do it. If you are writing a traditional Joomla module, this should be your helper.php file.
NOTES:
Only include the "die" script once in a joomla document.. (defined( '_JEXEC' )..
JURI::current() automatically reads the current page URL. If you call echo JURI::current(); on a page with the url http://www.example.com/names, then it will display the same link.
It is important that the action="" points to the exact Url where you will publish this module.
Furthermore, it is considered bad practice to post data to 'SELF', but you are kindof limited with a module, so unless you build a component or a plugin, you should post your form to 'SELF' as done with this example. (JURI::current();)
When in the Joomla framework, it isn't necessary to declare your database name, username or password as Joomla is already "logged in".. So instead of querying databasename.jos__tablename, in joomla you can replace the query with this: #__tablename. In fact this is the best practice when dealing with db queries and Joomla because users do not have to use the default jos_ prefix, joomla automatically replaces "#" with whatever the prefix. In my case "#" equals "jos"
Take note when querying the sql to create the table.. make sure you replace databasename with the actual name of your database..
That should do it.
If for some reason you are unable to post data:
1) Make sure the form doesn't redirect to a different page when you click submit. If it does, change the form action"" to the absolute url to where this page is published.. then go from there.
2) Sometimes the $data =new method doesn't work. This depends on how you set up your module, functions and classes.
Here is an alternative:
$db =& JFactory::getDBO();
$query = "INSERT INTO `#__names` (`fname`, `lname`)
VALUES ($fname, $lname);";
$db->setQuery( $query );
$db->query();
Try something like http://www.chronoengine.com/ - Chronoforms
You may run a custom query to get a result on tmpl file which is simple
<?php // no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
$category = $_REQUEST['category'];
if(isset($category))
{
$db = JFactory::getDbo();
$db->setQuery("SELECT machine_id FROM j25_machinefinder_products WHERE category = '$category'");
// Load the row.
$result = $db->loadRowList();
//your result will return here
print_r($result);
}
?>
<form action="" method="get" name="usedequipment">
<select name="category">
<?php foreach($hello as $category)
{
?><option value="<?php echo $category[0]; ?>"> <?php echo $category[0]; ?></option><?php
} ?>
</select>
<input type="submit" />
</form>
Related
I know this question is probably going to get downvoted and I will probably get into trouble but I am hoping someone may be able to help me with my situation.
On my site I use json to download data from an external source, and then I style it beautifully.
Within the json data is an individual ID for each data set.
What I want to accomplish is to have a database where I can insert the ID and a url link.
I have created the table within the wordpress database via phpMyAdmin, but I want to create a page within the admin section where I can simply add the data in.
For displaying the json data I use a php insert addon, within that php clip i want to do a piece of code that checks the database for the id within my custom database and displays the link.
I will be honest I don't know where to start on this, even if its just a link to a source that shows me how to create an admin page and submit data to the database within wordpress dashboard.
I really appreciate any help given and like I say I know I should try harder, but when ever I do a search all I get is 100's of references to add an admin to the database manually.
Thanks,
Adam
Edit I just realized I never put any table information in my question.
The table name within wordpress is called: wp_home_tickets
within that are 3 fields: id (auto increasement), gameid (numeric) and ticketlink (text)
thanks.
For adding a custom settings page in your admin, use the Settings API https://codex.wordpress.org/Settings_API
Here is a nice tutorial using it https://deliciousbrains.com/create-wordpress-plugin-settings-page/#wp-settings-api
To fetch data from your custom table, use the wpdb class https://developer.wordpress.org/reference/classes/wpdb/. More specifically, you can use wpdb::get_results if you will have multiple rows sharing the same id https://developer.wordpress.org/reference/classes/wpdb/get_results/
Or wpdb::get_row if you will ever only have one https://developer.wordpress.org/reference/classes/wpdb/get_row/
Hope this helps you out!
For anyone wishing to see how it was done, here is how I did it.
I created a file in my theme called db_admin_menu.php and added the following to it:
<?php
function ticket_admin_menu() {
global $team_page;
add_menu_page( __( 'Tickets', 'sports-bench' ), __( 'Tickets', 'sports-bench' ), 'edit_posts', 'add_data', 'ticket_page_handler', 'dashicons-groups', 6 ) ;
}
add_action( 'admin_menu', 'ticket_admin_menu' );
function ticket_page_handler() {
$table_name = 'wp_home_tickets';
global $wpdb;
echo '<form method="POST" action="?page=add_data">
<label>Team ID: </label><input type="text" name="gameid" /><br />
<label>Ticket Link: </label><input type="text" name="ticketlink" /><br />
<input type="submit" value="submit" />
</form>';
$default = array(
'gameid' => '',
'ticketlink' => '',
);
$item = shortcode_atts( $default, $_REQUEST );
$gameid = $item['gameid'];
if ($wpdb->get_var("SELECT * FROM wp_home_tickets WHERE gameid = $gameid ")) { echo 'Ticket already exists for this game.'; goto skip; }
if (!empty($_POST)) { $wpdb->insert( $table_name, $item ); }
skip:
}
?>
I then put this code in my script that fetches and displays the json:
$matchid = $match['id'];
$ticket_url = $wpdb->get_var("SELECT ticketlink FROM wp_home_tickets WHERE gameid = '$matchid' ");
if ($ticket_url) { echo 'Get Tickets'; }
I hope someone does find it of use, i did have to use a wordpress plugin called `Insert PHP Code Snippet' by xyzscripts to be able to snippet the php to a shortcode, but that is not the purpose of this post.
Thanks again for your help.
I have a helper which generates a custom form input.
Helper (simplifed code)
public function customInput($field, array $options = array()) {
$defaultOptions = array(
'class' => 'custom-input',
'label' => false
);
$options = array_merge($defaultOptions, $options);
return $this->Form->input($field, $options);
}
Now how can I modify the name attribute of the input by prefixing it with another 'model'. For example, the input will by default have the following name attribute:
<input type="text" name="data[MyModel][field]" />
But I want it to be:
<input type="text" name="data[_custom][MyModel][field]" />
Mainly, what seems tricky is that I don't know how to get the model name that will be used by default. Also, I need something that works if the default model hierarchy is more complicated, like:
<input type="text" name="data[MyModel][AssociatedModel][field]" />
Would need to be modified to:
<input type="text" name="data[_custom][MyModel][AssociatedModel][field]" />
You want name
echo $this->Form->input('whatever', array('name' => 'data[_custom][MyModel][field]'));
There is nothing like data[_custom][MyModel][AssociatedModel][field] in cakes form helper. Your options as far as automation go is:
field // normal, use current model
Model.field // used with not default model / relations
Model.$i.field // User hasMany Post would be Post.$i.field
For the input helper, CakePHP uses $this->model() to get the name of the current model.
You can see it inside lib\Cake\view\FormHelper, or directly from the online API:
http://api20.cakephp.org/view_source/form-helper#line-942
$modelKey = $this->model();
Maybe that helps.
well you can do: $this->Form->input('_custom.MyModel.field'); to create an input in the format you require.
It becomes a case of passing the appropriate model name and associated model with it.
I don't know how you could do this automatically as obviously each relation is different/may have multiple associations.
So using your helper: echo $this->YourHelper->CustomInput('_custom.MyModel.MyAssociation.field', $options) might do the trick.
Please i need a big help here. AM developing a yii application where i have to loop through my form and do a batch insert. I found bacth update in yii but ive not been able to see how to do batch insert and validation. Please help.
Here is my View:
<?php for($i=0;$i< $this->getDisplayArchModel();$i++) {?>
<FIELDSET class="radios">
<div class="row">
<?php echo $form->labelEx($model,'competency_type'); ?>
<?php echo $form->textField($model,'competency_type'); ?>
<?php echo $form->error($model,'competency_type'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'definition'); ?>
<?php echo $form->textArea($model,'definition'); ?>
<?php echo $form->error($model,'definition'); ?>
</div>
</fieldset>
<?php } ?>
Controller ::
public function actionDisplayArchModel()
{
$validateCat = $this->getDisplayArchModel();
if($validateCat == NULL)
$this->redirect(array('architecture'));
$model = new CompetencyType;
if(isset($_POST['CompetencyType']))
{
$model->attributes = $_POST['CompetencyType'];
if($model->validate()){
foreach($_POST['CompetencyType'] as $value)
{
echo $value;
}
}
}
$this->render('displayArchModel' ,array('model'=>$model));
}
Sometimes we want to collect user input in a batch mode. That is, the user can enter the information for multiple model instances and submit them all at once. We call this tabular input because the input fields are often presented in an HTML table.
To work with tabular input, we first need to create or populate an array of model instances, depending on whether we are inserting or updating the data. We then retrieve the user input data from the $_POST variable and assign it to each model. A slight difference from single model input is that we retrieve the input data using $_POST['ModelClass'][$i] instead of $_POST['ModelClass'].
more on collecting Tabular Input
I have a user profile update page, which pre-populates the text fields with the users information gathered from $this->request->data. Everything works fine, except the password field is pre-populated with what appears to be the encrypted password from the database, so on save, it fails the validation as the password exceeds the number of characters allowed, and even if it stores, it will then replace the users actual password with the encrypted same password, if that makes sense.
What would be the best work-around for this?
Controller:
public function profile() {
if($this->request->is('post') || $this->request->is('put')) {
if($this->Auth->user("id") == $this->request->data['User']['id']) {
$this->request->data['User']['user_status_id'] = $this->Auth->user("user_status_id");
$this->request->data['User']['user_type_id'] = $this->Auth->user("user_type_id");
if($this->User->save($this->request->data)) {
$this->Session->setFlash('Your profile has been updated','default',array('class'=>'success'));
} else {
$this->Session->setFlash("An error has occured updating your profile.");
}
} else {
$this->Session->setFlash("Unable to save this result as there is an ID mismatch.");
}
} else {
$this->request->data = $this->User->read(null,$this->Auth->user("id"));
}
}
View:
<h1>Profile</h1>
<h3>Update your profile</h3>
<div class="left">
<div class="formContainer">
<?php
echo $this->Form->create('User');
echo $this->Form->input('username',array("id" => "profileUsername"));
echo $this->Form->input('password',array("id" => "profilePassword"));
echo $this->Form->input('company');
echo $this->Form->input('first_name');
echo $this->Form->input('last_name');
echo $this->Form->input('telephone');
echo $this->Form->input('fax');
echo $this->Form->input('id',array('type' => 'hidden'));
echo $this->Form->end(__('Update'));
?>
</div>
</div>
The problem you have is, if you have stored your password as you should (with a one way hash), you can't reverse it to show the plain text version so you could simply set the password data to null (leaving the password field empty):
$this->request->data['User']['password'] = NULL;
You then have two options:
Require the users password to update their data (a nice little security measure)
Only update the users password when the user has entered a new password
The 2nd option may break your validation if you have set password validation rules to allowEmpty => false, in this case you could use multiple validation sets which you can do by following either of these (all work in cakephp 2.x) multivalidateable behaviour, multiple validation sets
read this and use this behavior:
http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp
the password is a one-way ticket - due to the technical details of hashs.
basically you never pass the password hash back into the view. always display empty fields (like you should have seen on pretty much every other website in the www).
you then ignore and posted field which is empty. only if the password field is freshly set you actually trigger the validation.
<?php echo $this->Form->input('password', array('label' => false,'type'=>'password','required'=>'false')); ?>
Output
<input name="data[User][password]" type="password" id="UserPassword">
Be careful! use this only on the edit part
$this->Form->input('password',array('value'=>null));
I am very new to CakePHP. Could you please explain me the steps needed to populate a select drop-down with values from the database. Please also suggest me some links to the reference.
Simple, if it's a related model in your controller you pass 'list' into the find(); an cake will make an id => value array for you, and the form helper will know exactly what to do with it.
For example say you want to get the list of categories for a product model, this is in your contoller:
$categories = $this->Product->Categories->find('list');
$this->set(compact('categories'));
Then in your view using the form helper, simply create the select element how you normally would any input:
$form->input('category_id');
The form helper will automatically load the $categories variable we set with $this->set().
You make a find in the db and then set the variable via $this->set(yourvariable) in the controller.
In the view you use the "yourvariable" in the select tag
Fill select 1
Fill select 2
Fill select 3
There is a cakeish way to do it with very less effort
$this->set('arrMain',$this->Post->find('list',
array(
'fields'=>array('id','title')
)));
Will give output as ==>
<select id="UserAge" name="data[User][Age]">
<option value="1">The title</option>
<option value="2">A title once again</option>
<option value="3">Title strikes back</option>
in the controller you do:
$this->loadModel('MyModel'); //if it's not already loaded
$options = $this->MyModel->find('all'); //or whatever conditions you want
$this->set('options',$options);
and in the view
<select...>
<?php foreach ($options as $option): ?>
<option value="<?php echo $option['MyModel']['id']; ?>"><?php echo $option['MyModel']['field']; ?></option>
</select>
Example
in your controller Class
$categories = $this->Articles->find('list')->select(['id', 'title'])->toArray();
$this->set(compact('categories')); // pass result dataset to the view
In Your view file
<?php echo $this->Form->select('articles_categories_id', $categories); ?>
Reading Source
http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html