简体   繁体   English

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

[英]Store data on my XML file while redirecting it to my home page using PHP and Javascript

I have created a PHP page where there is a form which would store data on an XML file. 我创建了一个PHP页面,该页面中有一种表单会将数据存储在XML文件中。

I have written the code of PHP which could store the data but now I have to redirect it to my homepage also. 我已经写了可以存储数据的PHP代码,但是现在我也必须将其重定向到我的主页。 But the main error is when I write the Javascript code for redirecting the page, then the data is not stored. 但是主要的错误是当我编写用于重定向页面的Javascript代码时,则没有存储数据。

Now I have currently written the code of Javascript in the PHP code itself. 现在,我目前已经在PHP代码本身中编写了Javascript代码。

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>

Try this. 尝试这个。 You need to use header("Location:navbar.php"); 您需要使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在使用Jquery将动态php数据加载到文件中,但是无法将数据存储到Javascript变量中 - I am using Jquery to load dynamic php data into my file but cannot store the data into Javascript variables 如何使用 javascript 将数据存储在 xml 文件中? - How to store data in xml file using javascript? 如何在我的快速服务器上提供 php 文件作为我的主页 - How to serve php files on my express server as my home page 无法将数据从JavaScript传递到PHP文件 - Cannot pass data from my JavaScript to my PHP file 如何使用PHP从XML文件加载数据,以使用javascript在html页面中显示它 - How to load data from an XML file using php to show it in an html page with javascript 将我的XML文件链接到我的HTML页面 - link my XML file to my HTML page 重定向到单独的页面时如何验证表单? - How can I verify my form while redirecting to a separate page? 如何从存储在我 PC 上的文件中获取 XML 数据并使用 javascript 以 HTML 格式填充表格? - How can I fetch XML Data from a file stored on my PC and populate a table in HTML using javascript? 为什么我的javascript无法在我的php页面上运行? - why is my javascript not working on my php page? 在移动视图主页上,无法使用jQuery进行重定向 - On mobile view home page not redirecting using jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM