I'm starting for Joomla! Developed and created a very simple module.
How can I create a form with 3 text fields and then save the values recorded in a database table? Try this example:
Try this example:
We first of a user in a table And will post the last name.
Create a table Note in your database that its prefix should be "jos _"
We call this form "name". Therefore, we will name our table "jos_names"
To query a SQL table (or whatever you use ..) in PHPMyAdmin, make this query to create a new table:
Name the table 'database' name `` jos_names`` (`id`inte (11) null auto_ignment, 'firstname', varar (100), 'lastname', varar (100), primary (` id`)) .
To simplify things, we will post results on the same page. Let's create the form:
& lt ;? Php / ** Post Form for DB module * * / / No direct access defined ('_JEXEC') or die ('restricted access'); // - Post your form data here - & gt; $ Fname = $ _POST ['fname']; $ Lname = $ _POST ['lname']; // - Post your postal data --- | // - Form creation ------------ & gt; ? & Gt; & Lt; Form name = "name" id = "name" action = "& lt ;? php echo jury :: current () ;? & gt;" Method = "post" & gt; & Lt; P & gt; & Lt; Input type = "text" name = "fname" id = "fname" value = "" /> gt; & Lt; / P & gt; & Lt; P & gt; & Lt; Input type = "article" name = "lname" id = "lname" value = "" /> gt; & Lt; / P & gt; & Lt; P & gt; Input id = "submit" name = "submit" type = "submit" value = "submit name" /> & Lt; / P & gt; & Lt; / Form & gt; // - Creating END form -------- | By & lt;? If ((isset ($ lname)) || (isset ($ fname)) {// Set the first name or last name, continue - & gt; $ Data = new stud class (); $ Data- & gt; Id = null; $ Data- & gt; First name = $ fname; $ Data- & gt; Last name = $ lname; $ Db = JFTC :: Milan (DDBO); $ Db- & gt; InsertObject ('#__ name', $ data, id); } Else {echo & lt; H4 & gt; A field is required! & Lt; / H4 & gt; '; }? & Gt;
Should do this if you are writing a traditional Joomla module, this should be your helpful file.
Note: Include "die" script only once in Joomla document .. (defined by '_JEXEC') ..
JURI :: current () Automatically current page URL reads if you have juri :: current (); on a page with the URL, it will display the same link, then it will display the same link.
It is important that the action = "" indicates the exact URL You will publish this module.
In addition, it is bad for posting data to 'SELF' Behavior is considered, but you are limited with modules, so long as you do not make any components or plugins, you should post your form with this form as 'SELF'. (Zurich :: Current ( );)
When Joomla is in the framework, it is not necessary to declare your database name, username or password as Joomla is already "logged in". Therefore, databasename
.com to query jos__tablename
Yes, you can change the query with: #__ tablename
. In fact it is best practice while working with DB queries and Joomla because users do not have to use the default jos_ prefix , Joomla automatically replaces "# " with the prefix in my case "# " par "jos"
Note when asking SQL to create a table .. Make sure you get the databasename
to be the real name Change your database with
should do this
If for some reason you are unable to post the data: 1) Make sure that the form does not redirect you to a different page When you click submit. If this happens, change the form action "" to the full URL where this page is published .. Then go from there.
2) Sometimes the $ data = new method does not work. It depends on how you install your modules, functions and classes. Here's an option:
$ db = & amp; JFactory :: getDBO (); Enter $ Query = "` #__Name` (`fname`,` lname`) VALUES ($ fname, $ lname); "; $ Db- & gt; Setclin ($ query); $ Db- & gt; Query ();
Comments
Post a Comment