0

I have a php script that is insert large result on my mysql dbb. I did jquery script to waiting the end of the php script. But I have a timeout error. Do you have an idea to change the timeout to let the script do his job ?

Thank you

Jquery script :

  <script type="text/javascript">
    var dataReturn='';

    function makeRequest(url){
        $('#preloader').show();
        $.get(url, function(data) {
            timeout: timeoutvalue
            $('#resultDiv').html(data);
            $('#preloader').hide();
        }).fail(function() {
            alert( 'Error occured during requesting the page' );
            $('#preloader').hide();
        });
    }

    function makePostRequest(url, params){
        $('#preloader').show();
        $.post(url,params, function(data){
            $('#resultDiv').html(data);
            $('#preloader').hide();
        }).fail(function() {
            alert( 'Error occured during requesting the page' );
            $('#preloader').hide();
        });
    }

    </script>
    <style>
        #preloader{
            display:none;
        }
    </style>

Button to start de script :

<input type="submit" onClick="makeRequest(\'inserttodbb.php?fonction=import_csv\')" value="" style="width:59px; height:17px; background:url(images/button-importstart.png) repeat-x left center;border:0;margin-left:0px; float:center;" />

Php page inserttodbb.php :

if(isset($_GET['fonction']) and $_GET['fonction']=="import_csv_valid")
{


$data=$_SESSION['listeok'];
    $count=count($data);

foreach($data as $numero)
{

    $req=("INSERT INTO contact VALUES('".$idclient."','', '".$numero."','','','','','','','0')");   
    $doins = mysql_query($req) or die(mysql_error()); 
}
echo 'Import OK for '.$count.' number !';


}

I have not the problem of time out when I load the php page. But when I try to use the ajax loader I have a problem with the message "Error occured during requesting the page". I tryed to put the timeout to 900000 but the problem continue. An idea ?

function makeRequest(url){
    $('#preloader').show();
    $.ajax({
        type: "GET",
        url: url,            
        timeout: 9000000,  //10 seconds, define this value in milliseconds
        success: function(data){
            $('#resultDiv').html(data);
            $('#preloader').hide();
        },
        error: function() {
            alert( 'Error occured during requesting the pagesssssss' );
            $('#preloader').hide();
        }
    });
    }

3 Answers 3

1

If the timeout is in the ajax call (not the PHP script) you can use this:

function makeRequest(url){
        $('#preloader').show();
        $.ajax({
            type: "GET",
            url: url,            
            timeout: 10000,  //10 seconds, define this value in milliseconds
            success: function(data){
                $('#resultDiv').html(data);
                $('#preloader').hide();
            },
            error: function() {
                alert( 'Error occured during requesting the page' );
                $('#preloader').hide();
            }
        });
}

EDIT:

Another option is to define a global timeout value. Put this in your code BEFORE calling makeRequest().

$.ajaxSetup({
    timeout:10000 // 10 seconds
});
Sign up to request clarification or add additional context in comments.

Comments

1

What you can do is to put set_time_limit(0);at the top of your script. This will make sure your script will not time out.

However, this might not be the best solution, you should also investigate why your script is timing out and if you can reduce the amount of work it needs to do in one go.

1 Comment

unfortunately I have to manage data[] with around 10 000/20 000 result. So, to insert into db it need some minimum time.
0

Within a PHP script

You can use set_time_limit() at the top of your PHP script to prevent timeouts.

set_time_limit(0); //no timeout

Within php.ini

If your host supports it, you can also set max_execution_time in php.ini:

max_execution_time 0

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.