This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

sorentruelsen339
sorentruelsen339

Hi

I am trying to run a php-script many times (50-100) to update a mysql database parallel with page-rendering. I am doing it this way, since the mysql-update requires a call to dropbox and by using ajax I hope not to delay the intial page-loading.

So I wanted to make an ajax-call to a php-function. Without october I would do something like: $.post( "AjaxTest.php"). For the life of me I can not figure out where to put the "AjaxTest.php"-file. So, I assume there must be a better way.

In the october-documentation it says I can make ajax-calls from partials or similar directly to the php-function inside the component. However, there are only examples of forms. I wish to run the scripts as the page is rendering. I made a function inside the component's php-file: Public function ajaxtest () {update mysql}, but I can not figure out how to trigger that function during page rendering. I've gotten a

            $( 'body ').append( "data" );

to work, but not function calls.

So, there are three questions:

1) Where can I place AjaxTest.php? 2) How do I call a specific function/where should the function be defined? 3) Is there a better way?

that0n3guy
that0n3guy

There are several ways to do this, but you could add everything that is in ajaxtest.php to a class then use laravel routing to point at your class/method.

See: http://laravel.com/docs/controllers#basic-controllers . You basically want to do something like: Route::post('url/that/you/are/posting/to', 'Namespace\FooController@method'); Also see laravel docs on routing http://laravel.com/docs/routing (google for a video if you want).

Another idea is to use laravel queue features to run your script 100 times... rather than doing it the way you're doing it. See http://laravel.com/docs/queues . Setting up queues with amazon SQS is VERY easy (I was surprised since amazon can sometimes be a pain) and SQS is dirt cheap.

sorentruelsen339
sorentruelsen339

The goal is to run a lightbox/fancybox straight from dropbox. I have found that the bottleneck is getting the temp.links from Dropbox's SDK. Tried rewriting the dropbox sdk but I am not good enough at php to make cURL_multi work. So, this ajax is a workaround for running the cURL in parallel while the splash-page is loading.

Ill look into your suggestions, though I doubt I skilled enough to get it working. Really, I am considering just heading back to pure laravel, October just seems to require too much additional learning before it gets easy

that0n3guy
that0n3guy

Hmm... why not just install dropbox headless client on your server and have the files sync into an upload folder.... seems way easier.

I went back to pure laravel again for a while. I'm back again :P for my CMS projects. Docs are way better now and community seems to be growing. If I was building a completely custom app, that didn't require much of the CMS features. I would think about using pure laravel.... BUT you can technically use pure laravel right in a plugin. You have routes.php and can use the normal basecontroller and views if you want. It IS laravel after all :P.

Daniel81
Daniel81

@sorentruelsen339

Try this...

In your page (in the code section) place your PHP function:

function onAjaxTest()
{
    // update MySQL here
    echo "Called";
}

then in your JS file use:

$.request('onAjaxTest', {
    success: function(data) {
        alert(data);
    }
});

You should see the alert message with "Called" appear.

Hope that helps.

yozDev
yozDev

So, there are three questions:

1) Where can I place AjaxTest.php? 2) How do I call a specific function/where should the function be defined? 3) Is there a better way?

@sorentruelsen339 Same Problem. Did you find a solution?

Last updated

yozDev
yozDev

Really, I am considering just heading back to pure laravel, October just seems to require too much additional learning before it gets easy

I am staring to feel the same way :(

daslicht
daslicht

Same question here:

[viewBag]
==
<?php
function onProcess() {
    echo 'submit';
}
...
<form class="contactForm" role="form" onsubmit="$(this).request('onProcess'); return false;">

When I submit the form I get Ajax handler 'onProcess' not found

Last updated

Briddle
Briddle

The following calls a function "onProcess()" inside the PHP Code section of your page and updates the contents of a DIV on your page with the id "feedback" with the contents of a partial named "confirm.htm":

<form data-request="onProcess" data-request-update="confirm: '#feedback'">
    <div id="feedback"></div>
    <input type="text" name="something">
    <button type="submit">Test</button>
</form>

Note that using October's AJAX framework requires you to include:

{% framework extras %} 

at the bottom of your page (after including jQuery).

All of this is also described clearly in https://octobercms.com/docs/ajax/introduction

Last updated

1-9 of 9

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.