Request validation via jQuery, Json

Asked

Viewed 99 times

1

I want to do a function that the customer type his order 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, someone would know how to do?

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 don’t know where, but I think I’m doing some wrong check on IF.

jQuery Ajax try

$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!");
            }
        },
    })
});

1 answer

1


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

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

Another is that you are forgetting to pick up the return of your request through the Success Function parameter:

 success: function (result) 

You can try using this example below, maybe the return is not exactly as I put, because as you did not put the full if is difficult to know how will be treated the resutlad of this request, but you can give a console.log(data) and access the information in the best way, I believe that so you can get what you need

In the example below I am 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>

Oh and I believe it would be better if you use a GET instead of POST, since you just want to get information from the bank, this post explains better about HTTP verbs and when to use them

Browser other questions tagged

You are not signed in. Login or sign up in order to post.