そのため、PHP スクリプトで入力を .txt ファイルに保存しましたが、html ページで何かが機能しません。HTML ページの .txt ファイルと div の両方にテキストを出力したいと考えています。
個人的には、スクリプト自体が別のページにあり、ページを更新しているため、表示される機会が決してないという事実に関係していると思います. PHPスクリプトがすべきことは、代わりに.txtファイルからプルすることだと思います。しかし、よくわかりません。
PHP を同じページに配置しようaction=""
としましたが、.txt ファイルに書き込まれませんでした。それが.txtファイルに書き込まれた唯一の方法は、私がそうするときですaction="[php file]"
.
何か案は?
HTML:
参照:
<?php require_once('storeText.php');?>
フォーム:
<form id="post" name="post" action="storeText.php" method="post">
<textarea name="msg" rows="5"></textarea>
<input class="button" name="submit" type="submit" value="SQUAWK!"/>
</form>
ページ上で入力を行う場所:
<div class="menuItem" >
<?php echo $msg; ?>
</div>
PHP:
storeText.php
<?php
if ( isset( $_POST['submit'] ) ) {
$filename = 'posts.txt';
$msg = (isset($_POST['msg']) ? $_POST['msg'] : null);
if ( is_writable( $filename ) ) {
if ( $handle = fopen( $filename, 'a') ) {
fwrite( $handle, $msg );
echo "Success, wrote ( $msg ) to file ( $filename )";
fclose( $handle );
} else {
echo "Could not open file.";
}
} else {
echo "File not writable.";
}
header("Location: profile.html");
}
?>
参考までに-そこにいるすべての嫌いな人のために別のスクリプトを使用することにしfwrite()
ました...同じ取引... htmlページに書き込みません。
代替 PHP:
<?php
if(isset($_POST['submit']))
{
$file = 'posts.txt';
// Open the file to get existing content
$msg = file_get_contents($file);
// Append new content to the file
$msg .= (isset($_POST['msg']) ? $_POST['msg'] : null);
// Write the contents back to the file
file_put_contents($file, $msg);
header("Location: profile.html");
}
?>