繁体   English   中英

使用 php 将内容提交到 .txt 并停留在同一页面上,而不是转到 php 页面

[英]submitting content to a .txt using php and staying on the same page and not go to the php page

我想将内容提交/写入 .txt 文件,我必须使用 php 来完成,但我不想打开 php 页面并想留在同一页面上。

我怎样才能做到这一点?

索引.html

<form action="writer.php" method="POST">
    <input name="field1" type="text" />
    <input name="field2" type="text" />
    <input type="submit" name="submit" value="Save Data">
</form>

编写器.php

<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
    $data = $_POST['field1'] . '-' . $_POST['field2'] . "|| \n";
    $ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo " written to file";
    }
}
else {
   die('no post data to process');
}
?>`

您可以简单地使用 iframes,更容易替代 AJAX。

<iframe name="panel" style="display:none;"></iframe>
<form action="writer.php" method="POST" target="panel">
    <input name="field1" type="text" />
    <input name="field2" type="text" />
    <input type="submit" name="submit" value="Save Data">
</form>

...正如这里的每个人都在大喊大叫,考虑学习AJAX

创建一个包含以下内容的 php 文件。

<?php    
 if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
file_put_contents('mydata.txt', $input, FILE_APPEND | LOCK_EX);
$message = "Success! You entered: ".$input;
}    
  ?>

  <html>
  <body>    
  <form action="" method="post">
<?php if(isset($message)) echo $message; ?>
 <input type="text" name="inputText"/>
 <input type="submit" name="SubmitButton"/>
</form>    
</body>
</html>

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM