简体   繁体   English

基本的PHP形式不起作用

[英]basic php form doesnt work

I am new in PHP and trying to save input to a text file, but POST action does nothing: 我是PHP的新手,尝试将输入保存到文本文件,但是POST操作不执行任何操作:

<form method="POST" action="<?php $_SERVER["PHP_SELF"]; ?>">
Name: <input type="text" name="usersname"/><br/>
<input type="submit" value="Write" name="submitwrite"/>
</form>

and in the same file on the top: 并在顶部的同一文件中:

<?php
    // Check if the user submitted this form
if (isset($_POST["submitwrite"])) {
    // Open the file in write mode
    $handle = fopen("writetest.txt","a+"); 

    // If successful
    if ($handle) {
        // Write to that handle the username submitted in the form and the date
        fwrite($handle,$_POST["usersname"] . " - " . date("Y-m-d"));

        // Close the file
        fclose($handle);
    }
}
?>

I click button and it doesnt POST to itself.... how to fix it?? 我单击按钮,它没有开机自检...。如何解决?

UPDATE: Thank you for your answers.. A problem was with file permissions... server does not allow me to set 777, only 755. There was nothing wrong with this code. 更新:谢谢您的回答。文件权限存在问题...服务器不允许我将777设置为755。此代码没有错。 I am glad you helped me to spot this !! 我很高兴你帮助我发现了这个!

What does <?php $_SERVER["PHP_SELF"]; ?> <?php $_SERVER["PHP_SELF"]; ?>是什么<?php $_SERVER["PHP_SELF"]; ?> <?php $_SERVER["PHP_SELF"]; ?> do? <?php $_SERVER["PHP_SELF"]; ?> It doesn't print anything, so your form doesn't have a place to submit to. 它不会打印任何内容,因此您的表单没有要提交的地方。

Try using <?php echo $_SERVER["PHP_SELF"]; ?> 尝试使用<?php echo $_SERVER["PHP_SELF"]; ?> <?php echo $_SERVER["PHP_SELF"]; ?> or <?php print($_SERVER["PHP_SELF"]); ?> <?php echo $_SERVER["PHP_SELF"]; ?><?php print($_SERVER["PHP_SELF"]); ?> <?php print($_SERVER["PHP_SELF"]); ?> instead. <?php print($_SERVER["PHP_SELF"]); ?>代替。


Also, if that won't help, maybe changing fopen("writetest.txt","a+"); 另外,如果这样做没有帮助,则可以更改fopen("writetest.txt","a+"); to fopen("writetest.txt","w"); fopen("writetest.txt","w"); might help? 可能有帮助?

You forgot to echo the action. 你忘了回应。 But it doesn't matter, since you should omit the attribute completely if you want to POST to the same URL. 但这并不重要,因为如果要发布到同一URL,则应完全省略该属性。

For me your code works fine, but, I think that you doesn't work because the directory haven't permission for write. 对我来说,您的代码可以正常工作,但是,我认为您无法工作,因为目录没有写权限。

run chmod 0777 diretory_name 运行chmod 0777 diretory_name

I suspect <?php $_SERVER["PHP_SELF"]; ?> 我怀疑<?php $_SERVER["PHP_SELF"]; ?> <?php $_SERVER["PHP_SELF"]; ?> isn't doing what you expect it to. <?php $_SERVER["PHP_SELF"]; ?>没有按照您的期望做。

If I recall correctly if you can avoid the problem by removing the action attribute, then the form will default to posting to "itself" (the same URL). 如果可以通过删除action属性避免出现问题,如果我没记错的话,该表单将默认为发布到“自身”(相同的URL)。

You have to set your permissions to read/write for both User and IUSR on "writetest.txt" if you're testing locally. 如果要在本地进行测试,则必须在“ writetest.txt”上设置用户和IUSR的读/写权限。

Steps to Fix it: 解决它的步骤:

  • Right click the file "writetest.txt" 右键单击文件“ writetest.txt”
  • Go to Properties. 转到属性。
  • Click Security. 单击安全性。
  • Click Edit. 单击编辑。 Select IUSR and USER 选择IUSR和USER
  • Check the "Write" box on both users. 选中两个用户上的“写入”框。
  • Save the settings. 保存设置。

You are missing an echo in the action php statement. 您在动作php语句中缺少回显。 However the form should still post to itself. 但是,表格仍应自行发布。

Try: 尝试:

if (isset($_POST)) {
    var_dump($_POST);
}

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

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