我在加载页面时调用web服务,即其下面的代码包含在addmachine.php的顶部
<?php
session_start();
$url='webservice url';
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($useridofuser));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$response= curl_exec($ch);
echo('\n'."Server response : \n \n".$response);
curl_close($ch);
//parsing the json response from server
$jsonde="$response";
$_SESSION['json']=$jsonde;
$org = array();
$loc = array();
$bui = array();
$items = json_decode($response);
foreach( $items as $each ){
$loc[]=$each->location[0]->name;
$bui[]=$each->location[0]->building[0];
$org[]=$each->name;
}
?>
Web服务的响应是json如下
[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"},{"location":[{"building":["test_loc1_building1","test_loc1_building2"],"name":"test location1"},{"building":["test_loc2_building2"],"name":"test location2"}],"name":"test Organization"}]
我已经解码了json,并在组织,位置和建筑物的下拉菜单中作为选项移动,然后在从下拉列表中选择组织,构建和位置后,需要使用ajax调用Web服务
<form name="theForm" method="post" action="addmachine.php" enctype="multipart/form-data" onSubmit="return validate();">
<label for="orgname">Organisation Name</label>
<select style="width: 305px;text-align:left ;" name="category_id" id="category_id">
<option value="">Select</option>
<?php foreach($org as $key=>$val){?>
<option value="<?php echo $val; ?>"><?php echo $val;?></option>
<?php
}
?> </select>
<p>
<label name="location">Location</label>
<select style="width: 305px;" name="category_id1" id="category_id1">
<option value="">Select</option>
<?php foreach($loc as $key=>$val){?>
<option value="<?php echo $val; ?>"><?php echo $val; ?></option>
<?php
}
?>
</select>
</p>
<p>
<label for="building">Building</label>
<select style="width: 305px" name="category_id2" id="category_id2">
<option value="">Select</option>
<?php foreach($bui as $key=>$val){?>
<option value="<?php echo $val; ?>"><?php echo $val; ?></option>
<?php
}
?>
</select>
</p>
<label for="entr/exi">Entrance/Exit</label>
<select style="width: 305px;text-align:left ;" name="building" id="category_id1" onkeypress="return ajax();">
<option value="">Select</option>
<?php foreach($org as $key=>$val){?>
<option value="<?php echo $val; ?>"><?php echo $val;?></option>
<?php
}
?> </select>
<p>
<label for="type">Type</label>
<input type="text" name="type" placeholder="enter your work station"/>
<label for="name">Name</label>
<input type="text" name="workstnname" placeholder="enter your work station" onkeypress="return onKeyPressBlockNumbers(event);">
<label for="description">Description</label>
<textarea name="description" style="height:150px;width:300px;"></textarea>
<label for="machinetype">Machine Type</label>
<select style="width: 305px;text-align:left;" name="machinetype">
<option value="">Select</option>
<option value="kiosk">kiosk</option>
<option value="workstation">workstation</option>
</select>
<p>
<input type="submit" name="submit" value="Submit">
</p>
</form>
因为我已经调用了函数ajax();关于字段入口/出口的按键。我已经在这里进行了ajax调用,我希望执行另一个名为ajaxweb.php的php页面我的ajax代码如下所示在页面的脚本标签中addmachine.php
$.ajax({
type: "GET", //GET or POST or PUT or DELETE verb
url:ajaxweb.php, // Location of the service
data: "", //Data sent to server
contentType: "json", // content type sent to server
dataType: "json", //Expected data format from server
processdata: true, //True or False
success: function (json) {//On Successful service call
var result = json.name;
$("#dvAjax").html(result);
},
error: ServiceFailed // When Service call fails
});
return false;
});
ajaxweb.php,其中选定的组织,位置和建筑物被提供给Web服务以在同一页面上返回,但它不起作用我也想知道如何将该响应发送到addmachine.php页面。
<?php
session_start();
$org=$_POST['category_id'];
$loc=$_POST['category_id1'];
$bui=$_POST['category_id2'];
$addmachine=array(
'org_name'=>$org,
'loc_name'=>$loc,
'building_name'=>$bui
);
echo json_encode($addmachine);
$url='webservice url';
$data="$addmachine";
echo("Input to Server : ".$data."\n");
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($addmachine));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$response= curl_exec($ch);
echo('\n'."Server response : \n \n".$response);
curl_close($ch);
?>
希望有人能提供帮助 谢谢