0

I tried to update so much rows with PHP that I had to make it with ajax (becouse execution time in server is too short for execute all rows). So I made a code which should call a .php file with the code inside, which updates sended ROW id in mysql table.

I have a code

<html>
<head>
    <title>Cron job!</title>
    <meta charset="UTF-8" />
    <script src="jquery.js"></script>
</head>

<html>
<body>

<div id="body">

</div>

<script>
    $(document).ready(function(){
        var array_items = <?=$table_info?>;
        var updated_items = 0;

        for(var i=0; i<array_items.length; i++){
            // pradedam kreipimąsi į kiekvieną rową jo updeitui su ajax
            $.ajax({
                url: "ajax_cron.php",
                type: 'POST',
                dataType: 'json',
                data: {update_id : array_items[i]['ids']},
                success: function (data) {
                    // kreipimąsis pavyko 
                    if(data.updated_id){
                        updated_items += 1;
                    }
                    $("#body").html(updated_items + " iš " +array_items.length + " updeitinta sėkmingai");                   
                }, error: function(e){
                    console.log(e.message);
                }
            });            
        }
    });
</script>

</body>
</html>

and it works when I open url with browser but when I make a call to this script with CRON JOB - ajax doesn't work.. So how to make it work?

2
  • I don't see that posting the data via ajax will solve the problem, the server still has to execute and return a response within the max execution time. I would suggest increasing the time as required in the php ini
    – andrew
    Jan 19, 2014 at 18:37
  • I send via ajax only one request and do it many times with loop. Jan 19, 2014 at 18:47

1 Answer 1

0

Ajax is a client side technology, so it requires client (a browser) to be able to execute the script and most importantly the DOM elements which could be read by your Javascript.

Cron Job on the other hand runs on CLI which does not know anything about the DOM when a script runs there.

So in other words what you are trying to do is not possible with the current web technology that we have. However you can always run a server side script on cronjob but client side script, no you can not.

2
  • is it possible to install web browser to VPS (ex. mozilla) and do cron to /path/to/browser http://mysite/mywebpage.php ? Jan 19, 2014 at 18:48
  • Yes, if you run browser executable and the call the URL, the code will run within the browser environment. But its always better to execute PHP code or server side code with cronjob. If you know what PHP script is doing when you load the browser why not add the PHP code to cronjab. Jan 19, 2014 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.