Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert into database from Joomla form fields

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?

like image 971
jehan Avatar asked May 16 '10 07:05

jehan


1 Answers

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();   
like image 181
Kevin Florida Avatar answered Oct 21 '22 05:10

Kevin Florida