phpmagento-1.4

Call php function onclick button in same page


Once I select check boxes & OnClick Insert Button I am calling php function present in api.php page to display results under status column. But its not fetching any results .

enter image description here

below is api url page results :

enter image description here

table.php - php code

         <p><button type= "button" id="show_status" >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" name="assigneeid" class="assigneeid-order" value="<?php echo $orderrecords[$k]["order_id"]; ?>"> 
</td>

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

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

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

table.php - javascript code

 $('#show_status').click(function(){ 
var selected = []; 
$('.assigneeid-order input:checked').each(function() { 
selected.push($(this).val()); 
}); 

var jsonString = JSON.stringify(selected); 
$.ajax({ 
type: "POST", 
url: "api.php", 
data: {data : jsonString}, 
success: function(response){ 
$.each(response, function(index, val) { 

$("#"+index+"").html(val); 
}); 
} 
}); 

});


    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;
                    }
                }
            }

api.php

 <?php

$curl_data['username']='outthinking781346'; 
$curl_data['password']='ouhk78epe34csmed46d'; 


$data = json_decode(stripslashes($_POST['data'])); 


foreach($data as $id){ 

$orderid = $id; 
$curl_data['awb']=$orderid; 
$response = array(); 

$url = 'plapi.ecomexpress.in/track_me/…'.$orderid.'&username=outthinking781346&password=ouhk78epe34csmed46d'; 
$ch = curl_init(); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_data); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 

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

// store the responce in array 

$response[$orderid] = $statusfinal; 

return $response; 
}

?>

Solution

  • table.php:

    <td>
    <input type="checkbox" name="assigneeid" class="assigneeid-order" value="<?php echo $orderrecords[$k]["order_id"]; ?>">
    </td>
    <td>
    <?php echo $orderrecords[$k]["order_id"]; ?>
    </td>
    <td id="<?php echo $orderrecords[$k]["order_id"];?>">
    </td>   
    

    in ajax call:

    $('#show_status').click(function(){
    var selected = [];
    $('.assigneeid-order:checked').each(function() {
    selected.push($(this).val());
    });
    var jsonString = JSON.stringify(selected);
    $.ajax({
    type: "POST",
    url: "api.php",
    data: {data : jsonString},
    success: function(response){
    
    response = $.parseJSON(response);
    
    $.each(response, function(index, val) {
    
    $("#"+index+"").html(val);
    });
    }
    });
    

    api.php:

    require_once("dbcontroller.php");
    $db_handle = new DBController();
    
    $curl_data['username']='outthinking781346'; 
    $curl_data['password']='ouhk78epe34csmed46d'; 
    
    
    $data = json_decode(stripslashes($_POST['data'])); 
    $response = array(); 
    
    foreach($data as $id){ 
    
    $orderid = $id; 
    $curl_data['awb']=$orderid; 
    
    
     $url = 'https://plapi.ecomexpress.in/track_me/api/mawbd/?awb=awbnumber&order='.$orderid.'&username=outthinking781346&password=ouhk78epe34csmed46d'; 
    $ch = curl_init(); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_data); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    
    $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); 
    } 
    
    // store the responce in array 
    
    $response[$orderid] = $statusfinal; 
    
    
    }
    echo json_encode($response);
    
    //return $response;