开发者

Posting data with ajax to php

开发者 https://www.devze.com 2023-03-17 17:06 出处:网络
Im trying to send some parameters from a JS to php to return some xml dependant on the id im sending however for some reason it is not catching the params I am sending.

Im trying to send some parameters from a JS to php to return some xml dependant on the id im sending however for some reason it is not catching the params I am sending.

Paramater variables

      site = "http://localhost/开发者_StackOverflow社区playerdata.php";
      var id =  document.getElementById('playerId').innerHTML;
      query = "?id="
      params = query + id;
      ajax(site, params);

So Iam calling the function

      function ajax(site, params){
      var xmlhttp;
      var i;
      if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp=new XMLHttpRequest();
         }
       else
     {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
           }
      xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
      xmlDoc=xmlhttp.responseXML;   
       }
        } 

     xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')  
     xmlhttp.setRequestHeader("Content-length", params.length);
     xmlhttp.setRequestHeader("Connection", "close");


      xmlhttp.open("POST", site, false);
      xmlhttp.send(params);
        } 

My php code in playerdata.php should grab the params but alas no love!

   <?php  header('Content-Type: text/xml');
    echo "<player>";?>

    <?
    $id = $_POST['id'];

    echo $id."</player>";

    ?>

All this in theory should work however i cant for the life of me figure out where I went wrong.

Can someone point me in the right direction? Is it possible to POST data this way?


It should not be query = "?id="

It should be query = "id="

If it doesn't work, pl. look at the following solutions.

In the playerdata.php, in the second block, u have missed the php.

It should be like

<?php
 $id = ...

?>

Also, make sure the parameters are correct by adding alert before the line

xmlhttp.send(params);

Do like,

alert(params);
xmlhttp.send(params);


lets try:

var site = "http://localhost/playerdata.php",
   id =  document.getElementById('playerId').innerHTML,
   params = "id="+ id,
   xmlDoc = null;


function ajax(url, params) {
    var req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
        try {
            req = new XMLHttpRequest();
        } catch(e) {
            req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                req = false;
            }
        }
    }
    if(req) {
        req.onreadystatechange = processReqChange;
        req.open("POST", url, true);
        req.send(params);
    }
}

and process request:

function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
           xmlDoc=req.responseXML;
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

source form http://developer.apple.com/internet/webcontent/xmlhttpreq.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号