js向php发消息
php向js发消息可以通过函数传递
js向php发送消息需要用到ajax发送请求
用户填写客户端消息 --> ajax向php服务器发送信息 --> php服务器(页面)处理 --> php服务器向ajax回应消息 --> 客户端ajax收到消息
在整个过程中,始终停留在原始页面
这个代码中index负责显示页面,发送ajax请求
cncommand作为php服务器负责处理
cncommand.php
<?php $phone_num=$_GET['phone_num']; $hnb_ip=$_GET['hnb_ip']; $command=$_GET['command']; switch ($command) { case "call": exec("cmdcli 172.21.22.97 call_ue $phone_num"); break; case "disconnect": break; case "paging": break; case "send sms": exec("cmdcli 172.21.22.97 sms $phone_num unicode winter"); break; case "disregister": break; default: break; } ?>
index.php
<html> <body> <?php $hnb_num=0; $hnb_ip=array(); $hnb_number=array(); $ue_num=0; $ue_id=array(); $ue_number=array(); $ue_context_id=array(); $ue_cs_rua_status=array(); $ue_cs_ranap_status=array(); $ue_ps_rua_status=array(); $ue_ps_ranap_status=array(); exec("cmdcli 172.21.22.97 cn php", $output); $k=0; $line=$output[$k++]; $temp=split(" ",$line); $hnb_num=$temp[1]; for($iter=0;$iter<$hnb_num;$iter++){ $ue_id_t=array(); $ue_number_t=array(); $ue_context_id_t=array(); $ue_cs_rua_status_t=array(); $ue_cs_ranap_status_t=array(); $ue_ps_rua_status_t=array(); $ue_ps_ranap_status_t=array(); $line=$output[$k++]; $temp=split(" ",$line); $hnb_ip[$iter]=$temp[1]; $line=$output[$k++]; $temp=split(" ",$line); $hnb_number[$iter]=$temp[1]; $line=$output[$k++]; $temp=split(" ",$line); $ue_num=$temp[1]; for($i=0;$i<$ue_num;$i++){ $line=$output[$k++]; $temp=split(" ",$line); $ue_id_t[$i]=$temp[1]; $line=$output[$k++]; $temp=split(" ",$line); $ue_context_id_t[$i]=$temp[1]; $line=$output[$k++]; $temp=split(" ",$line); $ue_number_t[$i]=$temp[1]; $line=$output[$k++]; $temp=split(" ",$line); $ue_cs_rua_status_t[$i]=$temp[1]; $ue_cs_ranap_status_t[$i]=$temp[3]; $line=$output[$k++]; $temp=split(" ",$line); $ue_ps_rua_status_t[$i]=$temp[1]; $ue_ps_ranap_status_t[$i]=$temp[3]; } $ue_id[]=$ue_id_t; $ue_context_id[]=$ue_context_id_t; $ue_number[]=$ue_number_t; $ue_cs_rua_status[]=$ue_cs_rua_status_t; $ue_cs_ranap_status[]=$ue_cs_ranap_status_t; $ue_ps_rua_status[]=$ue_ps_rua_status_t; $ue_ps_ranap_status[]=$ue_ps_ranap_status_t; } ?> <?php echo "<form name='form1' method='post' action='index.php'>"; for($iter=0;$iter<$hnb_num;$iter++){ $ue_id_t=$ue_id[$iter]; $ue_number_t=$ue_number[$iter]; $ue_context_id_t=$ue_context_id[$iter]; $ue_cs_rua_status_t=$ue_cs_rua_status[$iter]; $ue_cs_ranap_status_t=$ue_cs_ranap_status[$iter]; $ue_ps_rua_status_t=$ue_ps_rua_status[$iter]; $ue_ps_ranap_status_t=$ue_ps_ranap_status[$iter]; echo "<p>"; echo "<span title='plmn:10587 ci:bc75000 rnc_id:c70b000 lac:d030 rac:0 sac:1'> $hnb_number[$iter] ip: [$hnb_ip[$iter]] </span>"; echo "</p>"; echo "<table border='1px' bordercolor='#000000' cellspacing='0px' style='border-collapse:collapse'>"; echo "<tr>"; echo "<th rowspan='2'> NUMBER </th>"; echo "<th rowspan='2'> ID </th>"; echo "<th rowspan='2'> Context ID </th>"; echo "<th colspan='2'> CS </th>"; echo "<th colspan='2'> PS </th>"; echo "<th rowspan='2'> Action </th>"; echo "</tr>"; echo "<tr>"; echo "<th> RUA status </th>"; echo "<th> RANAP status </th>"; echo "<th> RUA status </th>"; echo "<th> RANAP status </th>"; echo "</tr>"; for($i=0;$i<$ue_num;$i++){ echo "<tr>"; echo "<td>$ue_number_t[$i]</td>"; echo "<td>$ue_id_t[$i]</td>"; echo "<td>$ue_context_id_t[$i]</td>"; echo "<td>$ue_cs_rua_status_t[$i]</td>"; echo "<td>$ue_cs_ranap_status_t[$i]</td>"; echo "<td>$ue_ps_rua_status_t[$i]</td>"; echo "<td>$ue_ps_ranap_status_t[$i]</td>"; echo "<td>"; echo "<select id=\"action\"> <option value=\"call\">call</option> <option value=\"disconnect\">disconnect</option> <option value=\"paging\">paging</option> <option value=\"send sms\">send sms</option> <option value=\"disregister\">disregister</option> </select> <input type='button' value='go' onclick=\"do_ue_action('$hnb_number[$iter]',$ue_number_t[$i],'action');\"></input>"; echo "</select>"; echo "</td>"; echo "</tr>"; } echo "</table>"; } echo "</form>"; echo "</p>"; echo "<form name='form2' method='post' action='index.php'>"; echo "<input type='submit' value='reload'></input>"; echo "</form>"; ?> <script language="javascript"> var xmlHttp; function do_ue_action(hnb_ip,phone_num,name) { var select_action=document.getElementById(name); xmlHttp=new XMLHttpRequest(); if(xmlHttp==null) { alert("browser does not http"); } var url="cncommand.php"; url=url+"?phone_num="+phone_num; url=url+"&hnb_ip="+hnb_ip; url=url+"&command="+select_action.value; xmlHttp.open("GET",url,true); xmlHttp.onreadystatechange=updatePage; xmlHttp.send(null); } function GetXmlHttpObject() { var xmlHttp=null; try { xmlHttp=new XMLHttpRequest(); } catch (e) { try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } function updatePage() { if(xmlHttp.readyState==4) { alert("success!"); } } </script>