PHPRad Classic Initial 2.7.3 Forum

PHPRad Forum => Questions => Topic started by: phpdad on July 08, 2020, 05:25:05 AM

Title: Reference fields dynamically based on previous entry.
Post by: phpdad on July 08, 2020, 05:25:05 AM
When I use LookupDependentField, and LookupDependentMatchField on the page I want-  I am able to see "Select A Value" Placeholder drop down box with the correct answer present..

Is it possible to skip the "Select a Value" and see a read only version of that answer displayed without additional clicking/selecting?

When I attempt to do Default Selected Value I think I may be doing it incorrectly if that's the solution...

"SELECT  DISTINCT Goal_1 AS value,Goal_1 AS label FROM student"

I have 3 tables-
Student      - this table has Student_Name, Goal_1, Goal_2, Teacher_Name
Teacher      - Teacher_Name
Chart         - Teacher_Name, Student_Name, Goal_1, Goal_1_value, Goal_2, Goal_2_value, etc...

I want Chart/Add to:

Select a Student and show a read only copy of Goal_1 so that I can assess and assign a  value to Goal_1

Please let me know if I can explain in greater detail. Thank you!

Title: Re: Reference fields dynamically based on previous entry.
Post by: willvin on July 08, 2020, 12:08:13 PM
@phpdad‍ please set Read_Only to True in Add / Edit Page Field Properties to set a field to read-only.
Title: Re: Reference fields dynamically based on previous entry.
Post by: phpdad on July 09, 2020, 03:53:28 AM
Thank you. I did see that option in the other page fields, but not in Select. I might be looking in the wrong area.

I tried to upload pics of examples, but "the upload directory is full."

https://photos.app.goo.gl/Ue3ycq8Paf34j9gz7

That's the area I was poking around in, and I also included a snap of the page to better explain what I mean.

On my page I have to click the drop down to display a value, but I'd like for the value to show automatically. I'm using LookupDependentField to match the goal to the student because Goal_1 is a unique value for each student. On the snip example Goal_2 and Goal_3 are displaying how I'd like, but display incorrect info because they don't reference the Student_Name.  If you're able to guide me in a different way to yield the same result I'm open to that also. I'm quite new at this, but trying to invest the time to improve.

Thanks for your Youtube videos.

Title: Re: Reference fields dynamically based on previous entry.
Post by: willvin on July 09, 2020, 08:18:48 PM
@phpdad‍ the Read_Only configuration for the select is not there but you can use the Disabled configuration, to disable the select input field.
Title: Re: Reference fields dynamically based on previous entry.
Post by: phpdad on July 10, 2020, 07:00:29 PM
Thank you @willvin I greatly appreciate your reply.

I gave it a shot, but might still be missing or overlooking something. When I disable the field entry I see the placeholder text, but not the value.

Disabled Set to True: https://photos.app.goo.gl/ZBf1a9Ag8dL4W1Zd8

Disabled Set to False: https://photos.app.goo.gl/onvjUBnFWXf6CmUS8

In this case I'm trying to get "Dance Better" to show where my placeholder text is showing.

Thank you again for your assistance.
Title: Re: Reference fields dynamically based on previous entry.
Post by: willvin on July 10, 2020, 07:33:50 PM
@phpdad I think the issue is with your MySql query for the default value. Please note that the value your query should return should be a single value and not a list. And from what I see on your query, you just copied the query generated for you from the select field and paste in in the Selected Default value, which is wrong.

Your query should look like this
Code: [Select]
SELECT Goal_1 FROM students WHERE (Goal_1 = 'Dance Better')
Title: Re: Reference fields dynamically based on previous entry.
Post by: phpdad on July 11, 2020, 03:53:50 AM
@phpdad I think the issue is with your MySql query for the default value. Please note that the value your query should return should be a single value and not a list. And from what I see on your query, you just copied the query generated for you from the select field and paste in in the Selected Default value, which is wrong.

Your query should look like this
Code: [Select]
SELECT Goal_1 FROM students WHERE (Goal_1 = 'Dance Better')


Thank you. I think I may be trying to do something that's not supported, or possibly need another trick to get me there.
https://photos.app.goo.gl/rMpvR25MnY6tF25DA  <- Video

When I select the student I want Goal_1 to show the personalized individual goal that was set, like "Get Better at coding" in this example.

Then I can have a table of all the relevant info I'd need for reports.
Title: Re: Reference fields dynamically based on previous entry.
Post by: willvin on July 11, 2020, 07:48:18 AM
@phpdad‍ do you have TeamViewer or AnyDesk installed on your computer?
Title: Re: Reference fields dynamically based on previous entry.
Post by: Catalin on July 12, 2020, 08:36:41 AM
@phpdad‍ do you have TeamViewer or AnyDesk installed on your computer?
Any solution found to that problem ? ...if yes please post it.
Title: Re: Reference fields dynamically based on previous entry.
Post by: phpdad on July 12, 2020, 05:13:14 PM
@phpdad‍ do you have TeamViewer or AnyDesk installed on your computer?

I have installed TeamViewer

I see a couple videos on your patreon that I hadn't seen yet. I'll try to check those out soon too. Thanks again.
Title: Re: Reference fields dynamically based on previous entry.
Post by: willvin on July 13, 2020, 10:12:08 AM
@phpdad‍ when you are ready to connect, you can send me your TeamViewer id and password on Patreon.

@Catalin‍ if I do resolve it for him, I will post that here.
Title: Re: Reference fields dynamically based on previous entry.
Post by: Catalin on July 16, 2020, 06:34:51 PM
Quote
@Catalin‍ if I do resolve it for him, I will post that here.
Many thanks, look forward for a solution, it's quite a basic and very useful feature, to be able to autocomplete a field based on another field value, e.g. at on-change event ! Thanks again...
Title: Re: Reference fields dynamically based on previous entry.
Post by: phpdad on July 19, 2020, 03:44:25 PM
Willvin fixed me up!

https://www.patreon.com/willvinofficial  :D
Title: Re: Reference fields dynamically based on previous entry.
Post by: willvin on July 19, 2020, 03:51:02 PM
@Catalin‍ as promised, here is the solution to his issue.

This👇 was added to his student Select field ClientEvents.
Code: [Select]
$('#ctrl-Student_Name').on('change', function(){ 
    let student_name = $(this).val();
    sendData(student_name);
});

function sendData(student_name){
    var data = {// We are trying to organize our data for the server
        student: student_name
    }
   
    //We now make an ajax post to the server
    var request = $.ajax({
        type: 'POST', // the type of request we are making.
        url: siteAddr+'api/getGoals/?csrf_token='+csrfToken, //our api link/url
        data: data, // the data we are sending to the server
        cache: false, // we dont want the request to be cached.
    });
   
    // Now we check for response that will be gotten from the server.
    // Callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        $('#ctrl-Goal_1').val(response.data.Goal_1);
        $('#ctrl-Goal_2').val(response.data.Goal_2);
        $('#ctrl-Goal_3').val(response.data.Goal_3);
        $('#ctrl-Goal_4').val(response.data.Goal_4);
    });
   
    // Now we check for errors we get when trying to make our post request.
    request.fail(function (jqXHR, textStatus, errorThrown){
    });
}

and this👇 was added to his ApiController.php.
Code: [Select]
	function getGoals(){
    if(!empty($_POST)){ // we check if the post variable is not empty.
$student = $_POST['student']; // pass the student to our variable $student

$db = $this->GetModel();
$db->where("student_name", $student);
$done = $db->getOne("student"); // get our data from database

if($done) { // checks if it was successful or not
$res['success'] = true;
$res['msg'] = "Action Successfully.";
$res['data'] = $done;
} else{
$res['success'] = false;
$res['msg'] = "Error getting student goal.";
}

render_json($res); // we return result of the update query in JSON format as expected.
} else {
            $res['success'] = false;
            $res['msg'] = "No post data detected.";
           
            render_json($res); // here we return access denied if Csrf fails
        }
}

What the codes above do is, get the value from the selected field, which is student, which then sends the value to an API link called getGoals. getGoals then queries the student table to get the student's goals which are then returned to the JS code, which the JS code assigns them to their fields.
Title: Re: Reference fields dynamically based on previous entry.
Post by: Catalin on July 19, 2020, 04:41:25 PM
Many thanks Willvin ! ...Somehow this should be part of the framework, easy to be accesses from the interface in the future. It is a heavy used functionality to populate a field based on another field value, on-change.
Respect & thanks ! ...Catalin
Title: Re: Reference fields dynamically based on previous entry.
Post by: Catalin on July 27, 2020, 09:20:40 PM
@Catalin‍ as promised, here is the solution to his issue.

This👇 was added to his student Select field ClientEvents.
Code: [Select]
$('#ctrl-Student_Name').on('change', function(){ 
    let student_name = $(this).val();
    sendData(student_name);
});

Do not worked for me... :( ...after many retries, I'm trying now to activate error log... to see what is happening..[/code]
Title: Re: Reference fields dynamically based on previous entry.
Post by: willvin on July 28, 2020, 10:22:58 AM
@Catalin‍ please make sure to replace the field id with your field id, as the example above contains the field id of the user the fix was made for.
Title: Re: Reference fields dynamically based on previous entry.
Post by: Catalin on July 29, 2020, 06:22:29 PM
@Catalin‍ please make sure to replace the field id with your field id, as the example above contains the field id of the user the fix was made for.

I did it but it's not working; I posted my code in the initial topic; it will help me if I can have errors captured in the log, but nothing in the log.
Title: Re: Reference fields dynamically based on previous entry.
Post by: willvin on July 30, 2020, 11:08:18 AM
@Catalin‍ please make sure to replace the field id with your field id, as the example above contains the field id of the user the fix was made for.
 
I did it but it's not working; I posted my code in the initial topic; it will help me if I can have errors captured in the log, but nothing in the log.
 To see javascript error, you have to view your browser console, phprad does not handle that. my answer o the other forum was for PHP code having errors.
Title: Re: Reference fields dynamically based on previous entry.
Post by: Catalin on August 26, 2020, 09:00:20 AM
Sorry to say but is a NASTY BUG related with this feature, as follow:
1) In the PAGE-SCRIPTS.JS in case an event is sent to server al the time is written in the JS file as:
$('#ctrl-Field_Name').on('change', function(){ ...}
but WITHOUT "Table_Name" and this will duplicate the functions in JS file, because SAME FIELD named "ctrl-Field_Name" may appear in several Tables with different functions...
2) In this regard, you receive random-values depending on which function is executed.
Is a nasty bug !
Correct call into the PAGE-SCRIPTS.JS for the function should be written as:
$('#ctrl-Table_name.Field_Name').on('change', function(){ ...}

This is a VERY USEFUL functionality, please do a work around ...and let me know :)
This error may make the ClientEvent useless because of impredictibility described, which is bad because is a cool and useful functionality.

To better understand I'm giving you bellow a sample code from the Application.

;$('#ctrl-Cod_Unic_Linie_Buget').on('change', function(){
    let Cod_Unic_Linie_Buget = $(this).val();
    sendCimLinieBugetProiect(Cod_Unic_Linie_Buget);
});
function sendCimLinieBugetProiect(Cod_Unic_Linie_Buget){
    var data = {// We are trying to organize our data for the server
        codliniebuget: Cod_Unic_Linie_Buget
    }
    //We now make an ajax post to the server
    var request = $.ajax({
        type: 'POST', // the type of request we are making.
        url: siteAddr+'api/getLinieBugetProiect/?csrf_token='+csrfToken, //our api link/url
        data: data, // the data we are sending to the server
        cache: false, // we dont want the request to be cached.
        //dataType: 'json' //the type of data we expect our api to return.
    });
    // Now we check for response that will be gotten from the server.
    // Callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        $('#ctrl-Categorie').val(response.data.Nume_cat_cheltuiala_eligibila);
        $('#ctrl-Subcategorie').val(response.data.Nume_subcat_cheltuiala_eligibila);
    });
    // Now we check for errors we get when trying to make our post request.
    // Callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // Log the error to the console
        console.error(
            "The following error occurred: "+
            textStatus, errorThrown
        );
    });
}
$('#ctrl-Cod_Unic_Linie_Buget').on('change', function(){
    let Cod_Unic_Linie_Buget = $(this).val();
    sendLinieBugetProiect(Cod_Unic_Linie_Buget);
});
function sendLinieBugetProiect(Cod_Unic_Linie_Buget){
    var data = {// We are trying to organize our data for the server
        codliniebuget: Cod_Unic_Linie_Buget
    }
    //We now make an ajax post to the server
    var request = $.ajax({
        type: 'POST', // the type of request we are making.
        url: siteAddr+'api/getLinieBugetProiect/?csrf_token='+csrfToken, //our api link/url
        data: data, // the data we are sending to the server
        cache: false, // we dont want the request to be cached.
        //dataType: 'json' //the type of data we expect our api to return.
    });
    // Now we check for response that will be gotten from the server.
    // Callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        $('#ctrl-Categorie').val("Something Else");
        $('#ctrl-Subcategorie').val("Random text");