CF6 - compare two strings

compare two strings in ChronoForms.

Overview

The issue occurs when using direct variable placeholders in PHP code instead of the proper CF method to retrieve form data.
Use the get method to access form variables and then compare the strings with standard PHP functions.

Answered
ChronoForms v6
ri riccardoc 25 May, 2020
Hello, I want to compare two strings and show a message if they are different or not, I'm using the strcmp function (in a custom HTML box) but it seems not to work, here is the code:

<?php
$str1 = "{var:read_data.tblx.FirtField}";
$str2 = "{var:read_data.tlby.SecondField}";
echo strcmp($str1, $str2); // if it work in here I'll add the IF THEN condition
?>

The result is always -10 wherever the two fields are identical or not, I've tried to echo both $str1 and $str2 just to be sure they are read correctly and they are, if I change to:

<?php
$str1 = "a";
$str2 = "a";
echo strcmp($str1, $str2);
?>

I got 0 as result (and any other result if I change the strings), where I'm wrong?
Max_admin Max_admin 25 May, 2020
Answer
you better use the if conditions directly and use if == or !=

if you want to use PHP then you need to call the variables like this:
$this->get("read_data.tblx.FirstField")
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
ri riccardoc 26 May, 2020
1 Likes
Thanks Max, the PHP variable call works fine. I post my code just in case it could be useful to someone.

<?php
$str1 = $this->get("read_data.tblone.FirstField");
$str2 = $this->get("read_data.tbltwo.SecondField");
if (strcmp($str1, $str2)== "0") {
echo "<i class=\"thumbs up icon green big\"></i>";
} else {
echo "<i class=\"hand paper icon red big\"></i>";
}
?>
This topic is locked and no more replies can be posted.