Create a user when I submit a form [duplicate]

2019-09-09 21:43发布

问题:

This question already has an answer here:

  • How can I create a new Joomla user account from within a script? 11 answers

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.

回答1:

Something similar.. But you get the idea :

HTML Code :

<html>
<head>
<title>Registration </title>
</head>
<body>
<form name="reg_form" id="reg_form" method="post" action="confirm.php">
<table>
<tr><td colspan="2"><?php echo isset($_GET["msg"])?$_GET["msg"]:"";?> </td></tr>
<tr><td>Username</td><td><input type="text" name="username" id="username" /> </td></tr>
<tr><td>Password</td><td><input type="password" name="password" id="password" /> </td></tr>
<tr><td>confirm</td><td><input type="password" name="confirm" id="confirm" /> </td></tr>

<tr><td></td><td><input type="submit" name="btnsubmit" id="btnsubmit" /></td></tr>
</table>
</form>
</body>
</html>

PHP Code to be executed on form submit

<?php

$username=isset($_POST["username"])?$_POST["username"]:"";

$password=isset($_POST["password"])?$_POST["password"]:"";

$confirm=isset($_POST["confirm"])?$_POST["confirm"]:"";

if(!empty($username)&&!empty($password)){

if($password!=$confirm)
header("location:registration.php?msg=Password does not be match.");

$host="localhost";

$user="mysql_user_name";

$pass="mysql_password";

///open connection
$link=mysql_connect($host,$user,$pass) or die(mysql_error());

mysql_select_db("databse_name",$link);

$query="SELECT * FROM users WHERE username='".mysql_escape_string($username)."'";

$result=mysql_query($query);

//count no of rows
$count=mysql_num_rows($result);

if($count==1){
header("location:registration.php?msg=username already exists");
}else{

$qry="INSERT INTO users(username,password)VALUES('".mysql_escape_string($username)."'
,'".mysql_escape_string($password)."')";

mysql_query($qry);

//Sending mail part goes here

echo "You are successfully registered.";
}

mysql_close($link);
}else{
header("location:registration.php?msg=Username or password cannot be blank.");
}


回答2:

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