2

I am calling Api : $url = 'https://plapi.ecomexpress.in/track_me/api/mawbd/?awb=awbnumber&order=' . $orderrecords[$k]["order_id"] . '&username=admin&password=admin123'; and fetching Status results of all Order IDS & displaying in php page when we refresh php page.

But I dont want to Call APi everytime when we refresh page. But When i select Order IDs through checkbox, than when i click on button "Show Status" , than only i want to Call Api & update the Selected Order IDs status in web page.

Url Output

enter image description here

enter image description here

PHP

<?php

           $tabindex=1;
          function checkecomstatus($orderid)
          {
            $data['username']='admin';
            $data['password']='ouhk78epe34csmed46d';
            $data['awb']=$orderid;

            $url = 'https://plapi.ecomexpress.in/track_me/api/mawbd/?awb=awbnumber&order='.$orderid.'&username=admin&password=ouhk78epe34csmed46d';
            $ch = curl_init();                    
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_POST, true);  
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);         

            $output = curl_exec ($ch); 
            curl_close($ch);
            $res = explode("\n",$output);

            if ( ! isset($res[13])) 
            {
               $res[13] = null;
            }
            $status = str_replace('</field>.','',$res[13]);
            $statusfinal = str_replace('<field type="CharField" name="status">','',$status);
            if($statusfinal!='') 
            {
            $sqlecom = "UPDATE do_order set in_transit='".$statusfinal.".',tconformed_by='Ecom' where order_id=".$orderid;
            $db_handleecom = new DBController();
            $resultecom = $db_handleecom->executeUpdate($sqlecom);      
            }

            return $statusfinal;
          }

?>                  
         <p><button type= "button" >Show Status</button></p>
          <table class="tbl-qa" border="1">
           <thead>
            <tr>            
              <th class="table-header">ID</th>
              <th class="table-header">ORDERID</th>            
              <th class="table-header">Status</th>         
            </tr>
          </thead>
          <tbody id="table-body">
          <?php

          if(!empty($orderrecords)) 
          {
            foreach($orderrecords as $k=>$v) 
            {?>
              <tr class="table-row" id="table-row-<?php echo $orderrecords[$k]["id"]; ?>" tabindex="<?php echo $tabindex;?>">
                <td><input type="checkbox" onclick="assignorderids('<?php echo $orderrecords[$k]["order_id"]; ?>')" name="assigneeid" id="assigneeid-<?php echo $orderrecords[$k]["order_id"]; ?>" value="<?php echo $orderrecords[$k]["order_id"]; ?>"></td>

                <td><?php echo $orderrecords[$k]["order_id"]; ?></td>

               <td><?php echo checkecomstatus($orderrecords[$k]["order_id"]);?></td>           

            </tr>
            <?php 
            $tabindex++;
            }
          }?>
          </tbody>
        </table> 
        <input type="hidden" name="ordercheckallIDs" id="ordercheckallIDs" value="<?php echo $ordercheckall;?>"/> 

Javascript

function assignallorderids()
  {
   var checkstatus=$("#checkall").is(":checked");

    if(checkstatus==true)
    {
      var id=document.getElementById("ordercheckallIDs").value;  
      document.getElementById("orderids").value=id;
      $("input:checkbox[name='checkassigneeid']").prop('checked',true); 
    } 
    else
    {
      $("input:checkbox[name='checkassigneeid']").prop('checked',false);  
      document.getElementById("orderids").value='';
    }    
  }

    function assignorderids(oid)
    {
       var checkstatus=$("#assigneeid-"+oid).is(":checked");       
        var morderId =document.getElementById("orderids").value;
        if(checkstatus==false)
        {
            var arrayorder = JSON.parse("[" + morderId + "]");
            document.getElementById("orderids").value='';
            for (var i = 0; i < arrayorder.length; i++) {           
                var orderstatusValue=arrayorder[i];
                if(orderstatusValue!=oid){
                    if (document.getElementById("orderids").value=='')
                    {
                        document.getElementById("orderids").value=orderstatusValue; 
                    }
                    else
                    {
                        var newvalue=document.getElementById("orderids").value;
                        document.getElementById("orderids").value=newvalue+","+orderstatusValue;                        
                    }
                }                       
            }
        }
        else
        {
            if(morderId=='')
            {
                document.getElementById("orderids").value=oid;
            }
            else
            {
                document.getElementById("orderids").value=morderId+","+oid;
            }
        }
    }

1 Answers1

1

This seems like you don't know about jQuery basics. If you can grab data from API but is firing on refresh then put it in trigger fire approach. something like if you have fetchData() function which fetches data from API as per the checkbox then don't fire it directly instead do something like $('#fetch-button-id').click(function() { fetchData(); });

and if you have just put fetch data from api code directly into document then add those code into function then call it.

Suson Waiba
  • 156
  • 1
  • 2
  • 9
  • so is i need to call something like here : https://stackoverflow.com/questions/5578899/calling-php-function-from-jquery –  Feb 23 '18 at 06:05
  • yop, thats what I was talking about. – Suson Waiba Feb 23 '18 at 06:08
  • i tried something like https://pastebin.com/CTq2gD2G , but its not displaying table now.... –  Feb 23 '18 at 06:21
  • do you have any other idea ? –  Feb 23 '18 at 11:18
  • Basically, I would do something like create API response file to be separate. then I would not fetch data from API if I don't want data to be shown before anything happens. then when I would click some action then fire ajax call to API file then call, do SQL thing then return a response as JSON then change the current view as per required. does it make any sense? its just basic ajax. I don't see any complex thing going on here. – Suson Waiba Feb 24 '18 at 08:33