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条)

报告相同问题?