首页 > 解决方案 > 调用 api url 后显示进度消息

问题描述

我们选择复选框和 onclick 按钮“显示状态”,我正在调用外部 Web 服务 api url 并更新数据库中的“状态”列 [下图中的第 4 个] 值.....

一旦我们得到成功响应,我们就会显示警报消息....

在此处输入图像描述

要求

但现在我想显示进度,如“1% 完成,2% 完成”,依此类推......:

在此处输入图像描述

状态页面

<button type= "button" id="show_status" >Show Status</button>

脚本

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

var jsonString = JSON.stringify(selected); 

$.ajax
  ({ 
type: "POST", 
url: "api.php", 
data: {data : jsonString}, 
     success: function(response)
       { 
          response = $.parseJSON(response);
          alert("completed");
          $.each(response, function(index, val)
             { 

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

             }); 
        } 
  }); 

});

api.php

 <?php

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

foreach($data as $id){ 

$post_data['username']='a';
$url = 'https://plapi.ecomexpress.in/track_me/api/mawbd/'; 
$ch = curl_init(); 
curl_close($ch); 

$orderResults=$xml=simplexml_load_string($output);
//print_r($orderResults); die;
    foreach($orderResults->object as $child)
    {
      $status=(string)$child->field[10];         
      break;
     }

$statusfinal = str_replace('<field type="CharField" name="status">','',$status); 

if($statusfinal!='') 
{ 
$sqlecom = "UPDATE do_order set in_transit='".$status."' where tracking_id=".$orderid;
//echo $sqlecom;

$db_handleecom = new DBController(); 
$resultecom = $db_handleecom->executeUpdate($sqlecom); 
} 
$response[$orderid] = [ 'status' => $status ];
}
echo json_encode($response);

?>

标签: url-routing

解决方案


请试试这个脚本和代码

$('#show_status').click(function()
 { 
    var selected = []; 
    $('.assigneeid-order:checked').each(function(){ 
        selected.push($(this).val()); 
        $('.assigneeid-order').prop('checked', false);
    }); 
    var count = selected.length
    var perPlush = 100/count;
    var per = 0;
    for (var i = 0; i <= count; i++) {
        $.ajax
        ({ 
            type: "POST", 
            url: "api.php", 
            data: {id : selected[i]},               
            success: function(response)
            { 
                response = $.parseJSON(response);
                $.each(response, function(index, val)
                { 
                    $("#"+index+"").html(val);
                    $("#"+index+"").html(val.status); 
                });
                per = per + perPlush;
                console.log(Math.round(per) + " % Complated");
                $("#progress").html(Math.round(per) + " % Complated"); 
            } 
        });     
    }
});

你的 api.php 文件

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX);

require_once("dbcontroller.php");
$db_handle = new DBController();




$id = $_POST['id']; //960856092
$response = array(); 

$orderid = $id; 
$hide = '';
$post_data['awb']=$orderid;  
$url = 'https://plapi.ecomexpress.in/track_me/api/mawbd/'; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, "http://plapi.ecomexpress.in/track_me/api/mawbd/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
$output = curl_exec ($ch); 
curl_close($ch); 

$orderResults=$xml=simplexml_load_string($output);
if($orderResults){
    foreach($orderResults->object as $child)
    {
        $status=(string)$child->field[10];       
        break;
    }

    $statusfinal = str_replace('<field type="CharField" name="status">','',$status); 

    if($statusfinal!='') 
    { 
    $sqlecom = "UPDATE do_order set in_transit='".$status."' where tracking_id=".$orderid;
    //echo $sqlecom;

    $db_handleecom = new DBController(); 
    $resultecom = $db_handleecom->executeUpdate($sqlecom); 
    } 
    $response[$orderid] = [ 'status' => $status ];  
}
else{
    $response[$orderid] = ['status' => 'Order already placed'];
}
echo json_encode($response);
?>

推荐阅读