Java 获取要执行的php页面上通过ajax传递的值

Java 获取要执行的php页面上通过ajax传递的值,java,php,ajax,web-services,Java,Php,Ajax,Web Services,我希望将3个参数组织名称、位置名称和建筑名称作为参数发送到ajax。我已将其编码如下。我希望获得从ajax传递的值,以便在我需要执行的页面中查看 //ajax calling function ajax() { var req; var org=document.getElementById('category_id').value; var loc=document.getElementById('category_id1').value; var bui=document.ge

我希望将3个参数组织名称、位置名称和建筑名称作为参数发送到ajax。我已将其编码如下。我希望获得从ajax传递的值,以便在我需要执行的页面中查看

 //ajax calling
  function ajax()
  {
 var req;
var org=document.getElementById('category_id').value;
var loc=document.getElementById('category_id1').value; 
var bui=document.getElementById('category_id2').value;  
 if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
   req=new XMLHttpRequest();
 }
 else
 {// code for IE6, IE5
   req=new ActiveXObject("Microsoft.XMLHTTP");
 }
 req.open("POST", "addmachine.php?org="+org+"&loc="+loc+"&bui="+bui+"", true);
 req.send();
  req.onreadystatechange=function(){
   if(req.readyState==4&&req.status==200){
       $(".error").hide();
       result=req.responseText

 }

}

         <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" onchange="OrganisationName(this);">
                    <option value="">Select</option>
                     <option value="1">1</option>
                     <option value="2">2</option>
                                      </select>

                    <p>
        <label name="location">Location</label>

                     <select style="width: 305px;" name="category_id1" id="category_id1" onchange="LocationName(this);" >
                     <option value="">Select</option>
                     <option value="1">1</option>
                     <option value="2">2</option>

                     </select>
                    </p>
                    <p>
        <label for="building">Building</label>

                    <select style="width: 305px" name="category_id2" id="category_id2" onchange="BuildingName(this);" onchange="ajax(this);">
                    <option value="">Select</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    </select>
                    </p>
                    <label for="entr/exi">Entrance/Exit</label>
                    <input type="text" name="ent" id="ent" placeholder="enter entrance/exit"/>
                    <p>
                    <label for="type">Type</label>
                    <input type="text" name="type" placeholder="enter your work station"/>

          <label for="name">Name</label>
          <input type="text" id="workstnname" name="workstnname" placeholder="enter your work station" onblur="return name();" 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>
</div>
//ajax调用
函数ajax()
{
var-req;
var org=document.getElementById('category_id')。值;
var loc=document.getElementById('category_id1')。值;
var bui=document.getElementById('category_id2')。值;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
req=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
req=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
请求打开(“POST”,“addmachine.php?org=“+org+”&loc=“+loc+”&bui=“+bui+”,true);
请求发送();
req.onreadystatechange=函数(){
如果(req.readyState==4&&req.status==200){
$(“.error”).hide();
结果=请求响应文本
}
}
机构名称
挑选
1.
2.

位置
挑选
1.
2.

建筑 挑选 1. 2.

入口/出口 类型 名称 描述 机器类型 挑选 小亭 工作站

在这里,关于构建的更改功能,我应该调用ajax来执行一个名为addmachine.php的php页面,参数包括所选的组织、位置和构建,现在我担心为什么我没有从ajax得到响应 有人能告诉我哪里做错了吗?一旦建筑发生变化,要执行的php就在这里

<?php
session_start();
$org=$_GET['org'];
$loc=$_GET['bui'];
$bui=$_GET['loc'];
$addmachine=array(
'org_name'=>$org,
'loc_name'=>$loc,
'building_name'=>$bui
 );
   echo json_encode($addmachine);
  $url='web service 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);
      ?>

Put

在你的内心

function ajax()

然后再试一次。

如果您通常通过单击提交发布表单,则需要将php代码更改为

$org=$_POST['category_id'];
$loc=$_POST['category_id1'];
$bui=$_POST['category_id2'];
如果您使用ajax函数,那么它应该是

$org=$_GET['org'];
$loc=$_GET['bui'];
$bui=$_GET['loc'];
使您在查询字符串中使用GET变量(在组织之前删除额外的符号)


?&org

req.open("POST", "addmachine.php?&org="+org+"&loc="+loc+"&bui="+bui+"", true);

在您的php代码中,您是否获得了值&org、$loc和$buiwell我的问题是如何在php代码中获得这些值先生,我已经按照您的建议进行了修改,您可以看一下我编辑的代码吗,这些修改无法带来任何更改,谢谢您更改请求打开(“POST”,“addmachine.php?&org=“+org+”&loc=“+loc+“&bui=“+bui+”,true);对于请求打开(“POST”,“addmachine.php?org=“+org+”&loc=“+loc+”&bui=“+bui+”,true);先生,我已经做了一些修改,请您检查一下,并告诉我我做错了什么更改请求打开(“POST”),好吗addmachine.php?org并删除&我已经更改了它,但是没有运气服务器收到请求了吗?哈哈,我得到了它,我一直在寻找不发出警报的响应,我在ajax函数的末尾添加了警报(结果),它来了。感谢所有帮助chandresh_cool、Amir、Ruben-J的人
req.open("POST", "addmachine.php?org="+org+"&loc="+loc+"&bui="+bui+"", true);
req.open("POST", "addmachine.php?&org="+org+"&loc="+loc+"&bui="+bui+"", true);