问题描述
我是Joomla的初学者!
I am beginner to Joomla! development and have created a very simple module.
如何创建一个包含3个文本字段的表单,然后将输入的值保存到数据库表中?
How do I create a form with 3 text fields and then save the entered values into a database table?
推荐答案
试试这个例子:
我们会将使用者的姓氏表。
We will Post a user's first and last name to a table.
在数据库中创建一个表。注意它应该有前缀jos _
create a table in your database. Note it should have the prefix "jos_"
我们将这个形式称为names。因此,我们将命名表jos_names
We will call this form, "names". So we will name our table "jos_names"
在PHPMyAdmin(或您使用的任何工具)的SQL行,执行此查询以创建一个新表: / p>
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>'; } ?>
如果你正在编写一个传统的Joomla模块,这应该是你的helper.php文件。
That should do it. If you are writing a traditional Joomla module, this should be your helper.php file.
注意:
只在joomla文档中包含一次die脚本..(defined('_JEXEC')..
NOTES:Only include the "die" script once in a joomla document.. (defined( '_JEXEC' )..
JURI :: current()自动读取当前页面的URL,如果你在一个页面上调用echo JURI :: current(); url ,那么它将显示相同的链接。
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.
重要的是,action =指向要发布此模块的确切网址。
It is important that the action="" points to the exact Url where you will publish this module.
此外,将数据发布到 SELF',但你是有限的一个模块,所以除非你构建一个组件或插件,你应该发布你的形式为'SELF'像这个例子。(JURI :: current();)
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();)
在Joomla框架中,没有必要声明您的数据库名称,用户名或密码,因为Joomla已经登录。因此,而不是查询 databasename 。 jos__tablename 在joomla中,您可以用以下命令替换查询: #__ tablename 。事实上,这是处理数据库查询和Joomla的最佳做法,因为用户不必使用默认的jos_前缀,joomla自动使用任何前缀替换#。在我的情况下,#equalsjos
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"
记住在查询sql创建表时,请确保替换 databasename 与您的数据库的实际名称。
Take note when querying the sql to create the table.. make sure you replace databasename with the actual name of your database..
这应该做。
如果由于某种原因无法发布数据:
1)确保在您单击提交时表单不会重定向到其他页面。如果是,请将表单操作更改为发布此页面的绝对网址。然后从那里继续。
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)有时$ data = new方法不工作。这取决于你如何设置你的模块,函数和类。
这是另一种选择:
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();
这篇关于从Joomla表单字段插入到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!