dongyuan9149 2014-07-16 07:27
浏览 73
已采纳

提交不应该重定向到HTML表单中的另一个页面

I have a HTML form which reads some data and saves in the text file with help of PHP. The form's HTML code looks like below.

What is happening now:
1. once i click on submit button it redirects to the 'myprocessing.php'
2. successfully saving in data.txt

The help i required on
1. when i click on submit it shouldn't redirect me to the php page, it should stay on the HTML form page
2. it should show the php file's output on the same HTML page
In simple words, I want to stay on the HTML page itself. Since I'm pretty new to HTML and PHP, struggling much to do these. Thanks in advance :-)

<html>
<head>
<title></title>
</head>
<body>
     <form action="myprocessing.php" method="POST">
     <input name="field1" type="text" />
     <input name="field2" type="text" />
     <input type="submit" name="submit" value="Save Data">
     </form>
<a href='data.txt'>Show data</a>
</body>
</html>

This is my data prcoessing PHP file.

<?php
echo "starting...";
if(isset($_POST['myTextBox']) && isset($_POST['field2'])) {
    $data = $_POST['field1'] . '-' . $_POST['field2'] . "
";
    $ret = file_put_contents('data.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
    die('There was an error writing this file');
}
else {
    echo "$ret bytes written to file";
}
echo "Ended...";
}
else {
die('no post data to process');
}
?>
  • 写回答

3条回答 默认 最新

  • dongliu1883 2014-07-16 07:30
    关注

    Write php code in same html file and use isset() function to check for $_POST data

    Try this

    <?php
    if(isset($_POST['field1']) && isset($_POST['field2']))
    {
    echo "starting...";
    if(isset($_POST['myTextBox']) && isset($_POST['field2'])) {
        $data = $_POST['field1'] . '-' . $_POST['field2'] . "
    ";
        $ret = file_put_contents('data.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file";
    }
    echo "Ended...";
    }
    else {
    die('no post data to process');
    }
    }
    ?>
    
    <html>
    <head>
    <title></title>
    </head>
    <body>
         <form action="current_page.php" method="POST">
         <input name="field1" type="text" />
         <input name="field2" type="text" />
         <input type="submit" name="submit" value="Save Data">
         </form>
    <a href='data.txt'>Show data</a>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起