0

I've created a page to insert order into a mysql database. Under the order the user is able to add up to 9 itens in any quantity specified. Because of this I created a string concatenation loop. At first I used just a commom string concatenation(dados += ''), then I tried to use an array.push to see if it would solve the allocation memory problem but it didn't do as expected(loading time very slow and get caught in memory allocation issue again)

Using the first approach the error message was: "Allocation Size Overload" Now the current error message is: "uncaught exception: out of memory" Please take a look at my code and see what you think:)

function insertOrder(){
var counter = 0;
var lenz  = new Array();
var data;
var id      = 0;

var patientName  = $('#patientName').attr("value");
var phone1       = $('#phone1').attr("value");
var phone2       = $('#phone2').attr("value");
var email        = $('#email').attr("value");
var status       = $('#status').attr("value");
var atendantName = $('#atendantName').attr("value");
var referee      = $('#referee').attr("value");

$("select[name='lenz']").each(function(){
    if (counter == 0){
        lenz.push('lenzId='+$(this).val());
        counter++;
    }else{
        lenz.push('&lenzId'+counter+'='+$(this).val());
    }
    counter++;
}); 

for (counter < 9; counter++;){
    lenz.push('&lenzId'+counter+'=0');
}


counter = 0;

$("input[name='quant']").each(function(){

    if (counter == 0){
        lenz.push('quantity='+$(this).val());
        counter++;
    }else{
        lenz.push('&quantity'+counter+'='+$(this).val());
    }
    counter++;

});

for (counter < 9; counter++;){
    lenz.push('&quantityId'+counter+'=0');
}


var paymentMethod = $('#paymentMethod').attr("value");
var trancheNumber = $('#trancheNumber').attr("value");
var discount      = $('#discount').attr("value");
var totalAmount   = $('#totalAmount').attr("value");
var trancheAmount = $('#trancheAmount').attr("value");
var remarks       = $('#remarks').attr("value");

if(patientName == ''){
    alert('Patient Name is missing');
    return false;
}

if(confirm("Are you sure you want to include this order?")){
    data += lenz.join();
    alert(data);
    data += '&id='+id+'&patientName='+patientName+'&phone1='+phone1+'&phone2='+phone2+'&email='+email+'&status=';
    data += status+'&atendantName='+atendantName+'&referee='+referee+'&paymentMethod='+paymentMethod+'&trancheNumber='+trancheNumber+'&discount='+discount;
    data += '&totalAmount='+totalAmount+'&trancheAmount='+trancheAmount+'&remarks='+remarks;



    alert(data);

    $.ajax({
        type: "POST",
        url: caminho+"/view/includeOrder.php?acao=salvar",
        timeout: 20000,
        data: data,
        success: 
            function(data){
                if(jQuery.trim(data) == 'ok'){
                    alert('Order sucessfully included!');
                    if (id == 0){
                        $("#includeOrderform")[0].reset();
                        $("#select-patient").html('Paciente: <button class="btn btn-theme btn-search margintop10 pull-left" type="button" onCLick="popupCenter(\'selecionaPaciente.php\', \'selecionaPaciente\', 750, 500);" >Pesquisar</button>');
                    }else{
                        mostrarTela('maintainOrder');
                        $('html, body').animate({ scrollTop: 0 }, 'slow');
                    }

                }
                else{
                    alert('Error saving Order!');
                }
            }
    });
}
}
4
  • English is a common language in programming. Commented May 25, 2016 at 19:17
  • @linusg I think almost everything I wrote is in english(just the function name and front-end messages weren't translated... Commented May 25, 2016 at 19:22
  • No, it's ok. But Você tem certeza que deseja salvar este Pedido? is not english IMO, I think whith writing in english, most people here can understand your code. Commented May 25, 2016 at 19:24
  • @linusg My focus is only in the string concatenation memory issue not in the functionality understanding. But ok, changed everything to english standart to make it easier in all cases. Commented May 25, 2016 at 19:36

1 Answer 1

1

Basically, all your for loops are wrong:

for (counter < 9; counter++;)

it should be

for (; counter < 9 ; counter++)

The first ; is necessary here since otherwise counter > 9 becomes the assignment expression / initialization block

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, initially I had that doubt about add ";" but as I just tested the functionality days ahead I didn't notice it could be the cause of this issue.

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.