且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我提交表单时创建一个用户

更新时间:2023-02-25 11:41:56

试试这个,

假设你的提交页面不是joomla的一部分,即外部joomla框架的工作。



第1步:

 包含Joomla框架工作

define('_JEXEC',1);
define('JPATH_BASE',dirname(__ FILE__)); //这是当我们在根
define('DS',DIRECTORY_SEPARATOR);

require_once(JPATH_BASE .DS.'includes'.DS.'defines.php');
require_once(JPATH_BASE .DS.'includes'.DS.'framework.php');

$ mainframe = JFactory :: getApplication('site');
$ mainframe-> initialise();
$ db = JFactory :: getDBO();

第二步:

 收集您的表单字段数据。 
//类似于$ user_email,$ password等

第3步:
检查用户名是否已存在于数据库上。

  $ sql =SELECT * FROM #__users WHERE username ='$ username' ; 
$ db-> setQuery($ sql);
$ db-> query();
if($ db-> getNumRows()> 0){

$ mainframe-> redirect(url,error msg,error);
}
else {
jimport('joomla.user.helper');
$ salt = JUserHelper :: genRandomPassword(32);
$ crypt = JUserHelper :: getCryptedPassword($ password,$ salt);
$ password = $ crypt。':'。$ salt;
//数据登录表
$ sql =INSERT INTO #__ users(username,email,lastvisitDate,registerDate,block,sendEmail,password,name,params,)values('$ user_email',' $ USER_EMAIL, '$现在', '$现在',0,0, '$密码', '$ FULL_NAME', '{}');
$ db-> setQuery($ sql);
$ db-> query();
$ last_inserted_id = $ db-> insertid();
//用户组tabe通常用于注册用户的group id是2其他明智的你也必须检查
$ sql =INSERT INTO #__ user_usergroup_map(user_id,group_id)values('$ last_inserted_id',2 );
$ db-> setQuery($ sql);
$ db-> query();

//最后根据需要发送邮件给用户。
JUtility :: sendMail(mailfrom,fromname,$ user_email,$ emailSubject,$ email_body,true);
}

希望它有道理..


This question unfortunately is fairly specific in that I may/may not need a Joomla! specialist to help me with this (hopefully I don't and someone with good php/mysql knowledge can help).

http://pastebin.com/JRhNB4EP - I've had to put my code in pastebin because the file is pretty damn large and I didn't want to flood this page with code.

So let me explain what the form does: it allows users to create a listing under a selected business type, but what I need implementing is that once the form is submitted, it creates a user under the ACL 'Registered' and randomly generates a password form them. The username would be the email that they provided. This info would then also get sent to the email provided.

I've looked around for help for a couple days now (usually a couple hours here and there within the day) but as the question is so specific, I haven't come across anything that's clear cut.

Any help/pointers/references that can point me in the right direction will be greatly appreciated! Thanks in advance.

Try this,

Assume that your submission page is not part of joomla ie, outside joomla frame work.

Step 1:

  Include Joomla Frame work

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
$db = JFactory::getDBO();

Step 2:

Collect your Form fields data.
//Something like $user_email ,$password etc.

Step 3: Check user name already exists on DB.

             $sql ="SELECT * FROM #__users WHERE username ='$username'"; 
             $db->setQuery($sql);
     $db->query();
     if($db->getNumRows()>0){

             $mainframe->redirect("url","error msg","error");
             }
            else{
                     jimport('joomla.user.helper');
         $salt = JUserHelper::genRandomPassword(32);
         $crypt = JUserHelper::getCryptedPassword($password, $salt);
         $password = $crypt.':'.$salt;
         //Data to login table
         $sql= "INSERT INTO #__users(username,email,lastvisitDate,registerDate,block,sendEmail,password,name,params,) values('$user_email','$user_email','$now','$now',0,0,'$password','$full_name','{}')";
         $db->setQuery($sql);
         $db->query();
         $last_inserted_id = $db->insertid();
                     //User group tabe normally group id for registered user is 2 other wise you have to check that too
         $sql= "INSERT INTO #__user_usergroup_map(user_id,group_id) values('$last_inserted_id',2)";
         $db->setQuery($sql);
         $db->query();

              //Finally send a mail to user if required.
             JUtility::sendMail(mailfrom, fromname, $user_email, $emailSubject, $email_body,true);
}

Hope it make sense..