insert into database from Joomla form fields

后端 未结 3 567
盖世英雄少女心
盖世英雄少女心 2021-02-05 13:24

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 ta

3条回答
  •  萌比男神i
    2021-02-05 13:59

    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:

    
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    //--END POST YOUR FORM DATA---|
    //--build the form------------>
    ?>
    

    //--END BUILD THE FORM--------| $data =new stdClass(); $data->id = NULL; $data->firstname = $fname; $data->lastname = $lname; $db = JFactory::getDBO(); $db->insertObject('#__names', $data, id); } else { echo '

    One Field Is Required!

    '; } ?>

    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();   
    

提交回复
热议问题