Order validation via jQuery, Json

1

I want to make a function that the customer type his request it is checked if it exists in the bank or if it is an invalid request. And I want to return this via Ajax, but I never messed with Ajax, would anyone know how to do it?

Function

    public function verificaAction() {

            #Recebe o Pedido Postado
            $increment_id = $this->getRequest()->getParam('ordertxt');

            #Conecta banco de dados 
            $ordertexto = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('increment_id', $increment_id); 

            #Se o retorno for maior do que zero, envia o form, se for menor da o error
            if ($ordertexto[0]['increment_id'] == $increment_id) { 
            } else {
                echo json_encode(array('increment_id' => 'Número invalido' ));
    }
}

I do not know where, but I think I'm doing some erroneous verification in the IF.

jQuery Ajax attempt

$j("#ordertxt").focus(function() {}).blur(function() { 
    ordertxt = $j("#ordertxt").val(); 
         $j.ajax({ 
            url: '<?php echo Mage::getUrl('contato/index/verifica') ?>', 
            type: 'POST', 
            dataType: "json",
            success: function () {
            if (ordertexto == 1) {
                $j("#msg_pedido").html("Esse número de pedido não existe!");
            } else if (ordertexto == 0) {
                $j("#msg_pedido").html("Esse existe!");
            }
        },
    })
});
    
asked by anonymous 21.08.2017 / 22:59

1 answer

1

One of the errors you are making in your AJAX call is that you are not passing the value that was taken into the input $j("#ordertxt") through the ajax date

$.ajax({
 method: "POST",
 url: "some.php",
 **data: { name: "John", location: "Boston" }**
 dataType: 'json'
})

Another is that you are forgetting to get the return of your request through the parameter of success function:

 success: function (result) 

You can try to use this example below, maybe the return is not exactly as I put it, because you did not put the complete if it is difficult to know how to handle the request, but you can give console.log(data) and access the information in the best way, I believe that you will be able to get what you need

  

In the example below I'm using a shorthand for Jquery AJAX

$j("#ordertxt").focus(function() {}).blur(function() { 
    ordertxt = $j("#ordertxt").val(); 
    
 // Enviando a data pelo post
var posting = $.post('<?= Mage::getUrl('contato/index/verifica') ?>', {'ordertxt': orderTxt }); 
  // verificando resultado
  // o parâmetro da função é o resultado retornado pela sua action
  posting.done(function( data ) {
      if(parseInt(data) === 1){
        $j("#msg_pedido").html("Esse número de pedido não existe!");
      } else if (parseInt(data) === 0) {
          $j("#msg_pedido").html("Esse existe!");
      }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

OhandIbelieveyou'dbetteruseaGETinsteadofPOST,sinceyoujustwanttogetsomeinfofromthebank,that post explains better about HTTP verbs and when to use them

    
22.08.2017 / 01:22