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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘