1084

Does / Should trongate module routing work if called within a javascript function?

Comments for “Does / Should trongate module routing work if called within a javascript...”
 

Posted by Dom on Saturday 27th January 2024 at 09:10 GMT

I have a javascript function, currently within the code of a view but the long term plan is to move it to its own file in the assets folder.

From that function I want to call a php file to process the info I'm pulling from the view so I tried the following;

<script>
function postData() {
    var message = document.getElementById("message").value;

    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                document.getElementById("result").innerHTML = "Response: " + xhr.responseText;
            } else {
                document.getElementById("result").innerHTML = "Error: " + xhr.status;
            }
        }
    };

    xhr.open("POST", "vtl_data_module/php/process.php", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("message=" + encodeURIComponent(message));
}
</script>


That produces the following error

Fatal error: Uncaught Exception: View 'C:/xampp/htdocs/data_generator/modules//views/test.php' does not exist in C:\xampp\htdocs\data_generator\engine\Trongate.php:175 Stack trace: #0 C:\xampp\htdocs\data_generator\engine\Trongate.php(135): Trongate->_get_view_path('test', NULL) #1 C:\xampp\htdocs\data_generator\modules\vtl_data\controllers\Vtl_data.php(12): Trongate->view('test') #2 C:\xampp\htdocs\data_generator\engine\Core.php(298): Vtl_data->test('') #3 C:\xampp\htdocs\data_generator\engine\Core.php(253): Core->invoke_controller_method() #4 C:\xampp\htdocs\data_generator\engine\Core.php(22): Core->serve_controller() #5 C:\xampp\htdocs\data_generator\public\index.php(5): Core->__construct() #6 {main} thrown in C:\xampp\htdocs\data_generator\engine\Trongate.php on line 175


I'm guessing that this simply shouldn't be done in a view, but I'd be grateful for clarification unless it can but I'm going about it the wrong way.
Level One Member

Dom

User Level: Level One Member

Date Joined: 12/01/2024

Posted by DaFa on Saturday 27th January 2024 at 12:05 GMT

Hi Dom,

You are missing the controller name in the path.

Also, you are overthinking your XMLHttpRequest(), which can be a lot simpler.

Have you played with the Trongate API Explorer? this task you are trying to perform would be well suited to it.

I've had a quick look at your GitHub commit for data_creator (https://github.com/domsinclair/data_creator). It's a great start! a few tips, you should just commit the module and other files you have changed and .gitignore your config, engine and other non-essentials to your project. Have you thought about using AI to populate data columns? I'll be checking in on your progress and may contribute if I get time.
Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

Posted by Dom on Saturday 27th January 2024 at 12:33 GMT

Hi Simon

Thanks for your reply, I'd thought that as the module name was vtl_data having vtl_data_module was including but I mat well be wrong.
You're quite correct in that it's probably being 'overthought' at the moment. That's just plain infamiliarity with php and javascript on my part and misinterpreting the good Dr. Google's suggestions!
You've obviously seen what I've been playing at so you'll have worked out that I have both the tables and more importantly table field details being returned and that I can select whichever fields one wants to generate data for.
The javescript function is out putting that I just need my php to read it and then invoke the Fakerphp.
I have considered using the inbuilt api explorer , it's there and it works so why reinvent the wheel. I'm also a bit of a pedant for using good old fashioned sql and talking directly to the database. Either should do the trick.
AI is indeed one possible option for populating the tables, Isuspect however that it will soon turn into a paid option and not everyone is keen on paying. I feel therefore that making a free option work, at least at first is preferable. If it then turns out that one could add AI and it does a better job, albeit at a price, then that can be added as an option and the end user can make an informed choice as to which route they wish to follow.

Once again thanks for your thoughts.
Level One Member

Dom

User Level: Level One Member

Date Joined: 12/01/2024

Posted by DaFa on Saturday 27th January 2024 at 22:23 GMT

Hi Dom,

I had another look for you, this time I cloned your repo instead of browsing it on GitHub.

You just made a simple error in your test method, by not passing through $data to the view:
function test() {
    $data['view_module'] = 'vtl_data';
    $this->view('test', $data);
}
Also, in 'vtl.js', you should avoid hard coding paths and use relative paths
instead or construct them using APPPATH and use forward slashes (Windows works fine with them) as things will break if another user is on Mac or Linux
<script>
    var processPath = <?= '"' . APPPATH . 'modules/vtl_data/assets/js/php/process.php"' ?>
</script>
If you are a visual person, I recommend watching as many of David's Trongate videos as possible. Try not to overthink things and if you haven't already done so, study the docs.

Otherwise, thanks for taking the time to learn Trongate and look forward to how your app evolves.

This comment was edited by DaFa on Saturday 27th January 2024 at 22:33 GMT

Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

×