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?
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>