我提交表单时创建一个用户 [英] Create a user when I submit a form

查看:172
本文介绍了我提交表单时创建一个用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,这个问题相当具体,因为我可能/不需要Joomla!专家来帮助我(希望我不会和有良好的PHP / MySQL知识的人可以帮助)。


$ b

http://pastebin.com/JRhNB4EP - 我不得不把我的代码放在pastebin中,因为这个文件非常大,我不想淹没这个带有代码的页面。

因此,让我解释一下表单的作用:它允许用户在选定的业务类型下创建列表,但我需要实现的是,一旦表单被提交后,它会在ACL'Registered'下创建一个用户,并随机生成一个密码。用户名将是他们提供的电子邮件。这些信息也会发送到所提供的电子邮件中。



我现在已经找了几天寻求帮助(通常在一天之内几个小时),但由于问题是如此具体,我还没有遇到任何明确的切入点。



任何帮助/指针/引用可以指向正确的方向非常感谢!

解决方案

试试这个,

假设你的提交页面不是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..

这篇关于我提交表单时创建一个用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆