English 中文(简体)
从文字箱到文字档案的书写数据
原标题:Write data from text box to a text file
  • 时间:2012-01-14 05:45:22
  •  标签:
  • php

I have developed a text box and I am trying to write this data to a text file. The PHP code is generating the file but the data is not being written. below is my code:

<html>
<body>
<form name="form" method="post">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit" />
<?
$a=@$_POST["text_box"];
$myFile = "t.txt";
$fh = fopen($myFile,  w ) or die("can t open file");
fwrite($fh,$a);
fclose($fh);
?>

最佳回答

提交行动的形式是“目标”,但根据您的《公共卫生和植物检疫法》,您的变数为_POST。 取而代之的是GET。

问题回答

你们的法典中看到的最大问题是,你是开立的,是打开的。 页: 1

<?

但它应当

<?php

then, the way you call $_POST and write file and stuff means it will be executed when you first load the form into the browser as well. the php engine makes no distinction between first run and consecutive run. this means that even if the user don t submit anything, there will still be an empty file, created from the run of the script where the form was displayed. it s a side effect. i ve modified your code just a little. here s my take on this:

<html>
    <body>
        <form name="form" method="post">
            <input type="text" name="text_box" size="50"/>
            <input type="submit" id="search-submit" value="submit" />
        </form>
    </body>
</html>
<?php
    if(isset($_POST[ text_box ])) { //only do file operations when appropriate
        $a = $_POST[ text_box ];
        $myFile = "t.txt";
        $fh = fopen($myFile,  w ) or die("can t open file");
        fwrite($fh, $a);
        fclose($fh);
    }
?>

表格发送了GET申请,但你试图查阅<代码>_POST[“text_ Box”]。 Try changing that to $_GET[text_ Box ], or using a form approach POST.

您可以仅用以下方式取代您的购买力平价代码:

if ($_REQUEST) {
    file_put_contents("t.txt", $_REQUEST["text_box"]);
}

这将确保档案只有在实际提交表格时,而不是在显示表格时,才会过期。

是否有数据? Try echo. 或,《<条码>印本/code> ,载于<条码>。

http://www.un.org。 您的表格方法为get,但您重新尝试使用$_POST。 使用<代码>_GET,或_$_REquestST/code>。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...