skip to Main Content

I have just migrated an intranet server from Windows Server 2008 and IIS8 to Server 2016 and IIS10 — everything is good except for one little PHP function– the rest of the xml manipulation pages are all done in ASP and work just fine– I have checked and rechecked the PHP install and even tried cloning the php 5.6 folder from the old server (which also has the php config file) to no avail. No errors are generated in the PHP log file! I think I prefer to just rewrite the code in ASP, but I think it is much more complicated than the PHP code. Any help would be greatly appreciated, as my time is limited and I have lots of other tasks occupying me. Already spent way too much time on this one single function. The basic idea is to look at a particular cid (row) and just toggle the Status from In to Out or vice-versa. Have a feeling that ASP code may be the faster route than poking around with the PHP installation."

Hoping this is an easy question for the right person.

Calling the function like so:

  function change(stat,row) {
 
$.ajax({
    type: "POST",
    url: "js/update.php",
    data: {status: stat, statrow: row},
    success: function() {
        location.reload();
    }
});

The php function:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php 
    
    $status = $_POST['status'];
    $row = $_POST['statrow'];
    $rownum = (int)$row;
    // echo $status," ",$rownum;
    
    // load the document
    // the root node is <Crew_List/> so we load it into $crewlist
    $crewlist = simplexml_load_file('../xml/crew-list.xml');
    //$currstatus = $crewlist->item[$rownum]->Status;
    
    // update
    if ($status == "In") {
        $crewlist->item[$rownum]->Status = "Out";
    } else {
        $crewlist->item[$rownum]->Status = "In";
    }
    
    // save the updated document
    $crewlist->asXML('../xml/crew-list.xml');
    
    // echo "done";
    ?>
</body>
</html>

XML file:

<?xml version="1.0" standalone="true"?>
-<Crew_List>

-<item>
<cid>1</cid>
<Status>Out</Status>
<Name>Crew1</Name>
<Title>Purser</Title>
<Phone_Number>-</Phone_Number>
<EMail/>
</item>

-<item>
<cid>2</cid>
<Status>In</Status>
<Name>Crew2</Name>
<Title>Captain</Title>
<Phone_Number>-</Phone_Number>
<EMail/>
</item>
</Crew_List>

Tried lots of stuff with PHP Manager and could not get php function to work. Works fine on the other server with IIS8 and PHP 5.6

2

Answers


  1. Chosen as BEST ANSWER

    Well, to answer my own question, in case anyone needs the same:

    I ended up finding a good code conversion tool and it gave me the following asp.net code that works just fine:

    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Xml" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.HttpMethod == "POST")
            {
                string status = Request.Form["status"];
                int rownum = Convert.ToInt32(Request.Form["statrow"]);
    
                // load the document
                XmlDocument crewlist = new XmlDocument();
                crewlist.Load(Server.MapPath("../xml/crew-list.xml"));
    
                // update
                XmlNodeList items = crewlist.GetElementsByTagName("item");
                XmlNode item = items[rownum];
                if (status == "In")
                {
                    item["Status"].InnerText = "Out";
                }
                else
                {
                    item["Status"].InnerText = "In";
                }
    
                // save the updated document
                crewlist.Save(Server.MapPath("../xml/crew-list.xml"));
            }
        }
    </script>


  2. Typically a standard, clean XML is like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
    </note>
    

    Hence, for your XML, please

    1. remove the - from the following :
    -<Crew_List>
    ....
    -<item> 
    ....
    -<item> 
    
    1. remove the <?xml version="1.0" standalone="true"?> line (or change to <?xml version="1.0" encoding="UTF-8"?>)

    2. For sure you need to set the XML file to be writable otherwise the PHP will not be able to update and save the file.

    So the following works (tested – 2nd record Status updated from In to Out):

    XML

    <Crew_List>
    
    <item>
    <cid>1</cid>
    <Status>Out</Status>
    <Name>Crew1</Name>
    <Title>Purser</Title>
    <Phone_Number>-</Phone_Number>
    <EMail/>
    </item>
    
    <item>
    <cid>2</cid>
    <Status>In</Status>
    <Name>Crew2</Name>
    <Title>Captain</Title>
    <Phone_Number>-</Phone_Number>
    <EMail/>
    </item>
    </Crew_List>
    

    PHP (as a test I hardcoded the variables to be set by POST)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <?php 
    
    /*    
        $status = $_POST['status'];
        $row = $_POST['statrow'];
    */
    
        $status = "In";
        $row = "1"; 
    
        $rownum = (int)$row;
        
        $crewlist = simplexml_load_file('../xml/crew-list.xml');
    
         
        if ($status == "In") {
            $crewlist->item[$rownum]->Status = "Out";
        } else {
            $crewlist->item[$rownum]->Status = "In";
        }
      
        // save the updated document
       $crewlist->asXML('../xml/crew-list.xml');
        
        ?>
    </body>
    </html>
    
    

    Last but not least, one seldom uses location.reload(); on success from a ajax call (just a note)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search