douzhang8033 2013-04-10 06:07
浏览 37
已采纳

获取通过php页面上的ajax传递的值

i would like to send 3 parameters organisation name,location name and building name as the parameters to ajax i have coded it as follows i would like to get the values passed from the ajax to see in the page that i need to execute .

 //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>

here on change function of building i am supposed to call ajax to execute a php page called addmachine.php with parameters as selected organisation,location and building ,now i am afraid why i am not getting response from the ajax can any one say me where i am doing wrong the php to be executed on onchange of the building is here

<?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."
");
    $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('
'."Server response : 
 
".$response);
    curl_close($ch);
      ?>
  • 写回答

3条回答 默认 最新

  • douba1067 2013-04-10 06:20
    关注

    If you normally post your form by clicking submit, you need to change your php code to

    $org=$_POST['category_id'];
    $loc=$_POST['category_id1'];
    $bui=$_POST['category_id2'];
    

    If you use your ajax function then it should be

    $org=$_GET['org'];
    $loc=$_GET['bui'];
    $bui=$_GET['loc'];
    

    cause your using GET variables in your querystring (remove the extra ampersand before org)

    req.open("POST", "addmachine.php?org="+org+"&loc="+loc+"&bui="+bui+"", true);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 delphi indy cookie 有效期
  • ¥15 labelme打不开怎么办
  • ¥35 按照图片上的两个任务要求,用keil5写出运行代码,并在proteus上仿真成功,🙏
  • ¥15 免费的电脑视频剪辑类软件如何盈利
  • ¥30 MPI读入tif文件并将文件路径分配给各进程时遇到问题
  • ¥15 pycharm中导入模块出错
  • ¥20 Ros2 moveit2 Windows环境配置,有偿,价格可商议。
  • ¥15 有关“完美的代价”问题的代码漏洞
  • ¥15 请帮我看一下这个简易化学配平器的逻辑有什么问题吗?
  • ¥15 暴力法无法解出,可能要使用dp和数学知识