0

I'm trying to create a module that creates a JSON collection of orders from an email entered into a form.

When am email is entered into a field within my form, the on blur event of the email input should trigger orders to be populated in a select box via the JSON received from a controller using AJAX. This is to help the user select a related order.

However upon visiting the URL of the controller null is displayed. How can i fix this?

IndexController.php

public function emailAction()
{
    $customer_email = $_POST["email"];
    $orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_email', $customer_email) ; 

    foreach($orders as $order) {
        $neworderjson = array('orderid'=>$order->getIncrementId(), 'email'=>$order->getCustomerEmail(), 'total'=>$order->getBaseGrandTotal(), 'created'=>$order->getCreatedAt());
        $orderjson[] = $neworderjson;
    }
    return $orderjson;
}

public function IndexAction()
{
    $this->loadLayout();
    $this->renderLayout();
    $this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
    $this->getResponse()->setBody(json_encode($this->emailAction()));
}

form.xml

<layout version="0.1.0">
    <form_index_index translate="label">
        <label>Contact Us Form</label>
        <reference name="head">
            <action method="addItem"><type>js</type><name>jquery/jquery-3.2.1.min.js</name></action>
            <action method="setTitle" translate="title" module="form"><title>Contact Us</title></action>
        </reference>
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-left.phtml</template></action>
            <action method="setHeaderTitle" translate="title" module="form"><title>Fale Conosco</title></action>
        </reference>
        <reference name="content">
            <!--<block type="core/template" name="form" template="form/form.phtml"/>-->
            <block type="form/index" name="pedido" template="form/form.phtml"/>
        </reference>
    </form_index_index>
</layout>

form.phtml

<form action="<?php echo $this->getUrl("contato/index/email")?>" id="contato" method="post">
<p>
<script src="http://code.jquery.com/jquery-3.2.1.js" type="text/javascript"></script>
</p>
<p>Email:<input id="email" name="email" type="text" value="" /></p>
<p><label class="required" for="f_f_compra"><em>*</em>About an order?</label></p>
<div class="select"><select class=" required-entry required-entry select" id="f_f_compra" name="f_f_compra">
<option selected="selected" value="">Select</option>
<option value="Sim">Yes</option>
<option value="Não">No</option>
</select></div>
<ul>
<li id="pedido">
<div class="wide"><label class="required" for="pedido"><em>*</em>Order</label><select id="orders"></select></div>
</li>
</ul>
<p>

<script type="text/javascript">
$j = jQuery.noConflict( true );
$j("#email").focus(function() {}).blur(function() {    
    email = $j("#email").val();    
    $j.ajax({url: "/index.php/form", type: "post", data: { email: email }, dataType: "json",success: function(result){
    $j("#orders" ).empty();
    if (result == null) {
      $j('#orders').append($j("").attr("value","N/A").text("No orders found for this email"));
     } else {      $j.each(result, function (index, data) {
        orderstring = result[index]["orderid"] + " - " + result[index]["total"] + " - " + result[index]["created"];
        $j('#orders').append($j("").attr("value",orderstring).text(orderstring));
           })    }
 }});
});
</script>
</p>

But I only get the value "null" when entering the page

enter image description here

1
  • In indexController.php change public function IndexAction() to public function indexAction()
    – PЯINCƎ
    Commented Jul 31, 2017 at 20:42

1 Answer 1

1

The page requires the $_POST["email"] variable to be populated so the controller can use that to generate the collection. Either visit with a form or get data using ajax methods (which is what is being done) using POST to see the collection.

To quickly test you can change $_POST["email"] to $_GET["email"] and visit the url form/[email protected] (But an email with an order associated with it) and the page should show some JSON data.

Visit the page with your form.phtml instead this has the email field which can be populated and then the order data will be grabbed via jquery ajax method using post.

To get this working with your form and json being generated from same URL you could replace your IndexAction() function with below maybe:

public function IndexAction() {
  $this->loadLayout();
  $this->renderLayout();
  if ($_POST["json"] == "y") {
      $this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
      $this->getResponse()->setBody(json_encode($this->emailAction()));
  }
}

Your ajax request will need to change also so that the controller knows when to return json:

data: { email: email, json: "y" }

This line is within your .phtml where the ajax request is made for data.

12
  • Yes, that way it works, it shows me the values, but I'd like to get the values ​​when I give a "blur" after filling in the email, but it looks like the form is performing the action before receiving the POST, then it returns null Commented Jul 31, 2017 at 17:53
  • So your orders select gets populated with nothing? Are there any console errors?
    – harri
    Commented Jul 31, 2017 at 17:56
  • I do not even fill in, as soon as I enter the page it already gives me the value "null", the form does not appear. Commented Jul 31, 2017 at 17:59
  • You will need to visit the page with that form.phtml that page is just a json endpoint to get data via ajax.
    – harri
    Commented Jul 31, 2017 at 18:02
  • I'm in form.phtml, it returns that "null", if I comment the lines $this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true); $this->getResponse()->setBody(json_encode($this->emailAction())); in the controller it loads the form but does not perform the action Commented Jul 31, 2017 at 18:06

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.