0

我想将 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 来执行一个名为 addmachine.php 的 php 页面,其参数为选定的组织、位置和建筑,现在我担心为什么我没有从 ajax 得到响应,谁能告诉我在哪里我做错了要在建筑物的onchange上执行的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);
      ?>
4

3 回答 3

1

如果您通常通过单击提交来发布表单,则需要将您的 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);
于 2013-04-10T06:20:36.050 回答
0

你有额外&?&org

req.open("POST", "addmachine.php?&org="+org+"&loc="+loc+"&bui="+bui+"", true);
于 2013-04-10T06:20:40.047 回答
0

var org=document.getElementById('category_id').value;
var loc=document.getElementById('category_id1').value; 
var bui=document.getElementById('category_id2').value; 

在你的里面

function ajax()

然后尝试一次。

于 2013-04-10T06:10:21.077 回答