duankang5285 2019-01-22 08:39
浏览 63
已采纳

将数据存储在我的XML文件中,同时使用PHP和Javascript将其重定向到我的主页

I have created a PHP page where there is a form which would store data on an XML file.

I have written the code of PHP which could store the data but now I have to redirect it to my homepage also. But the main error is when I write the Javascript code for redirecting the page, then the data is not stored.

Now I have currently written the code of Javascript in the PHP code itself.

I will provide the code for my page below.

<html>
  <head>
    <title>Login</title>
  </head>
<body bgcolor="#f0f0f0">
<?php
if(isset($_REQUEST['ok']))
{
    $xml = new DOMDocument("1.0","UTF-8");
    $xml->load("a.xml");

    $rootTag = $xml->getElementsByTagName("document")->item(0);

    $dataTag = $xml->createElement("data");

    $aTag=$xml->createElement("a",$_REQUEST['a']);
    $bTag=$xml->createElement("b",$_REQUEST['b']);

    $dataTag->appendChild($aTag);
    $dataTag->appendChild($bTag);

    $rootTag->appendChild($dataTag);

    $xml->save("a.xml");
}
echo "<script type=\"text/javascript\">
function Home()
{
    window.location.href=\"navbar.php\";
}
</script>
"
?>
<form action="index.php" method="post">
  <table cellspacing="20px" style="margin-left:500px;" id="atable">
    <tr>
    <td>User Name </td>
    <td> <input type="text" name="a" width="75" /> </td>    <!--Inserting textbox in the page with justified width-->
</tr>

<tr>
<td> Password </td>
<td> <input type="password" name="b" width="75" /> </td>    <!--Inserting textbox in the page with justified width-->
</tr>
<tr>
<td>
<input type="button" name="ok" value="Login" onclick="Home()"  />
</td>
</tr>
</table>
</form>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dqb14659 2019-01-22 08:42
    关注

    Try this. You need to use header("Location:navbar.php");

    <?php
    if(isset($_REQUEST['ok'])){
        $xml = new DOMDocument("1.0","UTF-8");
        $xml->load("a.xml");
    
        $rootTag = $xml->getElementsByTagName("document")->item(0);
    
        $dataTag = $xml->createElement("data");
    
        $aTag=$xml->createElement("a",$_REQUEST['a']);
        $bTag=$xml->createElement("b",$_REQUEST['b']);
    
        $dataTag->appendChild($aTag);
        $dataTag->appendChild($bTag);
    
        $rootTag->appendChild($dataTag);
    
        $xml->save("a.xml");
        header("Location:navbar.php");
        exit;
    }
    ?>
    <html>
    <head>
    <title>Login</title>
    </head>
    <body bgcolor="#f0f0f0">
    
    <form action="index.php" method="post">
    <table cellspacing="20px" style="margin-left:500px;" id="atable">
    <tr>
    <td>
    User Name </td>
    <td> <input type="text" name="a" width="75" /> </td>    <!--Inserting textbox in the page with justified width-->
    </tr>
    
    <tr>
    <td> Password </td>
    <td> <input type="password" name="b" width="75" /> </td>    <!--Inserting textbox in the page with justified width-->
    </tr>
    <tr>
    <td>
    <input type="submit" name="ok" value="Login"  />
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?