Build Multilingual Websites with Gridbox

Build Multilingual Websites With Joomla Website Builder Gridbox
Mon. - Fri. 9 am - 5 pm Ask a Question
Remember me

some basic php after form submit

1 week 19 hours ago #1

  • BiLe's Avatar
  • BiLe
  • Posts: 232
Hello,

I am working on a custom login and next register form with balbooa forms + custom php after form submit. Login will be my fist try, based on this code.

I wanted to check on my field variables, but echo won't work.

Could you give me a hint on how to handle questions below.
error_reporting(E_ALL);
ini_set('display_errors', 1);

$username = '[Field ID=1]';
$password = '[Field ID=3]';
$reminder = '[Field ID=5]';

echo "username: " . $username;
echo "</br>";
echo "password: " . $password;
echo "</br>";
echo "reminder: " . $reminder;

// how can i abort the submission

// how can i throw an exception or error message with form in frontpage?

// how can I echo or check variables or form field variables?

// how can I include a file from joomla root directory, if I am not sure about the correct current path?

1 week 12 hours ago #2

  • Vyacheslav's Avatar
  • Vyacheslav
  • Posts: 21317
Hello,
Thank you for contacting us

1.) There is no such possibility, the script is executed after adding a submission to the database, it is started by the eval function

2.) This is not possible, the form is sent in the background via Ajax.

3.) Before submitting the form, open the browser console and write formsApp.debug = true; and your echo will show up in the browser console

4.) Use the joomla constant JPATH_ROOT - The path to the installed Joomla site.
Example:
JPATH_ROOT.'file.php ';

Regards,
Vyacheslav, Balbooa.com

1 week 11 hours ago #3

  • BiLe's Avatar
  • BiLe
  • Posts: 232
1) What if I do not save the record to database, instead write my own INSERT with php code after form submission. Could I redirect to the form and use a custom "Thank you" message in case of an error? But I could make a case with my redirect and just redirect to 2 different pages 1. success page 2. error page.

Your field validation works really well. Tested it against multiple other form builders.

In case of a login script, I would want to display either "Logged in" or "Logged out", instead of "Thank you".

3) Not sure how to do this. I need to just add it with my example code and watch browser console?
I have tried, but console says: ReferenceError: formApp is not defined.

4) See example code. Don't I need to define JPATH_BASE first?


error_reporting(E_ALL);
ini_set('display_errors', 1);

formsApp.debug = true;

require_once JPATH_BASE . '/includes/framework.php';

$username = '[Field ID=1]';
$password = '[Field ID=3]';
$reminder = '[Field ID=5]';

echo "username: " . $username;
echo "</br>";
echo "password: " . $password;
echo "</br>";
echo "reminder: " . $reminder;

6 days 13 hours ago #4

  • Vyacheslav's Avatar
  • Vyacheslav
  • Posts: 21317
1. There is no way to export custom messages or make different custom redirects based on your code, but you can redirect to a custom Gridbox page and collect 2 different messages on it and set in the settings a group of users who will see them (registered or guests)

3. You must go to the page with the form and after loading the page, simply write in the browser console formsApp.debug = true;
and then after submitting the form, the text of your echo
or errors from your custom php script added in the button settings will be displayed in the console

4. No, you do not need to define the JPATH_BASE constant in Joomla before using it,
because it, like all Joomla constants, are defined by Joomla itself

Regards,
Vyacheslav, Balbooa.com

6 days 4 hours ago #5

  • BiLe's Avatar
  • BiLe
  • Posts: 232
Thank you. It helped and I was able to see some debug. I can echo my database result with below script.

However, I am afraid I can't come up with a working authentification script. Tried different solutions for 2 days now. How can I make use of Joomla Auth.?

I somehow think I am not initiating the needed Joomla classes correct.

Any help appreciated.

I would love to use Balbooa Forms for login and register. It just works so smooth.
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once JPATH_ROOT . '/configuration.php';
require_once JPATH_ROOT . '/includes/defines.php';
require_once JPATH_ROOT . '/includes/framework.php';
//require_once JPATH_ROOT . '/assets/class.php';

$credentials = new JConfig();

$database_host 			= $credentials->host;
$database_user 			= $credentials->user;
$database_password 	= $credentials->password;
$database_name 		= $credentials->db;	

//$database_host 		= 'localhost';
//$database_name 		= '';
//$database_user 		= '';
//$database_password 	= '';

$result = new mysqli($database_host, $database_user, $database_password, $database_name);

if(!$result) {
	$error = mysqli_error() . " / " . mysqli_connect_errno();
	echo $error;
	exit;
}
	
// fields
$username = '[Field ID=1]';
$password = '[Field ID=3]';
$reminder = '[Field ID=5]';

// fields
$username = 'Administrator';
$password = '';
$reminder = 'true';

/* 
 * https://phpdelusions.net/mysqli_examples/prepared_select
 * bind param i for integer
 * bind param d for double (float)
 * bind param s for string
 * bind param b for blobs
*/
$query = "SELECT id, password FROM joomla_users WHERE username = ?";
$stmt = $mysqli->prepare($query); 
$stmt->bind_param("s", $username);
$stmt->execute();

$result = $stmt->get_result();
$joomla_user = $result->fetch_assoc();

echo $joomla_user['password'];
echo $joomla_user['id'];

5 days 12 hours ago #6

  • Vyacheslav's Avatar
  • Vyacheslav
  • Posts: 21317
An example of the code that you need to use in order to log in a user
$options = ['remember' => false];
$credentials = ['username' => '[Field ID=7]', 'password' => '[Field ID=8]'];
JFactory::getApplication()->login($credentials, $options);

Regards,
Vyacheslav, Balbooa.com

5 days 7 hours ago #7

  • BiLe's Avatar
  • BiLe
  • Posts: 232
Thank you. I was confused, because some people online wrote that JFactory will no longer work. Factory would be the new class name. My solution for now...

Form with 3 fields:

1) username (input, text)
2) password (input, password)
3) reminder (checkbox with option text)

Form redirects to another page, where we place a "GUEST" message and a "REGISTERED" message for login failure and login success.

Just add this script with your submit button. Make sure to use the correct Field ID:
$form_username = '[Field ID=1]';
$form_password = '[Field ID=3]';
$form_reminder = '[Field ID=5]';

full script
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once JPATH_ROOT . '/configuration.php';
require_once JPATH_ROOT . '/includes/defines.php';
require_once JPATH_ROOT . '/includes/framework.php';

$credentials = new JConfig();

$database_host 			= $credentials->host;
$database_user 			= $credentials->user;
$database_password 	= $credentials->password;
$database_name 		= $credentials->db;	

$mysqli = new mysqli($database_host, $database_user, $database_password, $database_name);

if(!$mysqli) {
	$error = mysqli_error() . " / " . mysqli_connect_errno();
	echo $error;
	exit;
}
	
/* fields */
$form_username = '[Field ID=1]';
$form_password = '[Field ID=3]';
$form_reminder = '[Field ID=5]';

/* checkbox */
if($form_reminder <> '') {
	$form_reminder = true;
}
else {
	$form_reminder = false;
}

/* 
 * validation
 * https://phpdelusions.net/mysqli_examples/prepared_select
 * bind param i for integer
 * bind param d for double (float)
 * bind param s for string
 * bind param b for blobs
*/

$query = "SELECT id, password FROM joomla_users WHERE username = ?";
$stmt = $mysqli->prepare($query); 
$stmt->bind_param("s", $form_username);
$stmt->execute();

$result = $stmt->get_result();
$joomla_user = $result->fetch_assoc();

$joomla_user_id = $joomla_user['id'];
$joomla_user_password = $joomla_user['password'];

/* login */
$options = ['remember' => $form_reminder];
$credentials = ['username' => $form_username, 'password' => $form_password];
JFactory::getApplication()->login($credentials, $options);

5 days 6 hours ago #8

  • BiLe's Avatar
  • BiLe
  • Posts: 232
Well, still an error with this. In case username/password do not match a db user, I get redirected to a 404page and browser says something like: Notice</b>:%20 Trying to access array offset on value of type null in

Almost there :)

2 days 12 hours ago #9

  • Vyacheslav's Avatar
  • Vyacheslav
  • Posts: 21317
Remove your code and use this one (replace with your Field IDs)
$options = ['remember' => '[Field ID=9]' != ''];
$credentials = ['username' => '[Field ID=7]', 'password' => '[Field ID=8]'];
JFactory::getApplication()->login($credentials, $options);

Best Regards,
Vyacheslav, Balbooa Support Team

2 days 12 hours ago #10

  • BiLe's Avatar
  • BiLe
  • Posts: 232
Thank you very much.

2 days 11 hours ago #11

  • Vyacheslav's Avatar
  • Vyacheslav
  • Posts: 21317
You are welcome
Powered by Kunena Forum