Donnovan -3 Newbie Poster

hi everyone,

i never worked with php or any script language before i am more of a java, c++, C programmer, but for a school project i need to program a character converter in php, i have some problem recieving response from my php file. can someone help me

this is the convert.php

<?php

$key = $_GET['key'];

header('Content-type: text/xml');

$xml_output  = "<?xml version=\"1.0\"encoding=\"UTF-8\"?>\n";

$xml_output .= "\r\n<converted-values>";

$xml_output .= "\r\n<decimal>" .(string)$key. "</decimal>";
$xml_output .= "\r\n<hexadecimal>0x" .dechex((string)$key). "</hexadecimal>";
$xml_output .= "\r\n<octal>0" .decoct((string)$key). "</octal>";
$xml_output .= "\r\n<hyper>&amp;0x" .dechex((string)keyInt). ";</hyper>";
$xml_output .= "\r\n<binary>" .decbin((string)$key). "B</binary>";

$xml_output .= "\r\n</converted-values>";

echo $xml_output;
?>

this is the

var req; 
  
  function convertToXML()
  { 
    var key = document.getElementById("key"); 
    var keypressed = document.getElementById("keypressed"); 
    keypressed.value = key.value; 
     
    var url = ""+escape(key.value);
    if (window.XMLHttpRequest){ 
      req = new XMLHttpRequest(); 
    } 
    else if (window.ActiveXObject){ 
      req = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    req.open("Get",url,true); 
    req.onreadystatechange = callback; 
    req.send(null);
  } 

  function nonMSPopulate()
  {
    var resp = req.responseText;
    var parser = new DOMParser();
    var dom = parser.parseFromString(resp,"text/xml");
                     
    decVal = dom.getElementsByTagName("decimal");
    var decimal = document.getElementById('decimal');
    decimal.value=decVal[0].childNodes[0].nodeValue;
   
    hexVal = dom.getElementsByTagName("hexadecimal");
    var hexadecimal = document.getElementById('hexadecimal');    
    hexadecimal.value=hexVal[0].childNodes[0].nodeValue;
     
    octVal = dom.getElementsByTagName("octal");
    var octal = document.getElementById('octal');    
    octal.value=octVal[0].childNodes[0].nodeValue;
           
    hyperVal = dom.getElementsByTagName("hyper");
    var hyper = document.getElementById('hyper');    
    hyper.value=hyperVal[0].childNodes[0].nodeValue;
        
    binaryVal = dom.getElementsByTagName("binary");
    var bin = document.getElementById('bin');    
    bin.value=binaryVal[0].childNodes[0].nodeValue;
  }

  function msPopulate()
  {
    var resp = req.responseText;
    var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.loadXML(resp);
                  
    dec = xmlDoc.getElementsByTagName('decimal');
    var decimal = document.getElementById('decimal');
    decimal.value = dec[0].firstChild.data;
    
    hexi = xmlDoc.getElementsByTagName('hexadecimal');
    var hexadecimal = document.getElementById('hexadecimal');
    hexadecimal.value=hexi[0].firstChild.data;
                  
    oct = xmlDoc.getElementsByTagName('octal');
    var octal = document.getElementById('octal');
    octal.value=oct[0].firstChild.data;
                  
    bin = xmlDoc.getElementsByTagName('binary');
    var binary = document.getElementById('bin');
    binary.value=bin[0].firstChild.data;    
        
    hypertextml = xmlDoc.getElementsByTagName('hyper');
    var hyper = document.getElementById('hyper');
    hyper.value=hypertextml[0].firstChild.data;     
  }
  
  function callback()
  { 
    if (req.readyState==4){ 
      if (req.status == 200){ 
     
        if (window.XMLHttpRequest){
             nonMSPopulate(); 
              } 
            else if (window.ActiveXObject){ 
          msPopulate();
        } 
      }
      clear();
    } 
  } 

  function clear()
  { 
    var key = document.getElementById("key"); 
    key.value=""; 
  }
  
  function focusIn()
  { 
    document.getElementById("key").focus();
  }

Dani AI

Generated

— focused checklist and fixes for the PHP/AJAX converter you posted.

First, validate and sanitize the incoming GET value on the PHP side (reject or coerce non-numeric input). Using PHP input filters avoids surprises and is safer than trusting raw query data (filter_input documentation). Also check for any undefined variable usage in the string building: a single undefined name will break the XML you send.

Second, produce well-formed XML and the correct headers. Make sure the XML prolog is syntactically correct, there is no BOM or stray whitespace before the prolog, and send an explicit Content-Type with charset. Building XML by hand is brittle; using DOMDocument or SimpleXML will handle escaping and the prolog for you (DOMDocument docs, header docs).

Third, on the client side confirm the request URL and same-origin rules. Use a relative path to avoid typos in the host, and URL-encode the key with modern functions rather than deprecated ones (encodeURIComponent reference). Prefer reading the parsed XML from the XHR object (responseXML) when available, and detect parse errors when using a DOM parser (XMLHttpRequest.responseXML, DOMParser).

Quick debugging steps: check the browser Network panel for the raw reply and HTTP status, view the JS console for parse errors, and inspect server error logs if PHP returns nothing. If the XML parser fails, the Network tab will show the exact response that caused it and point to the offending character or malformed prolog (Network monitor guide).

This targets the most common failure points in the flow you shared: input handling, malformed XML, request URL/encoding, and parser choice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.