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?
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?
You may run a custom query to get a result on tmpl file which is simple
Try something like http://www.chronoengine.com/ - Chronoforms
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:
To simplify things, we will post the results to the same page.. Let's build the form:
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: