0

整个周末我一直在处理网页,而我只是遇到了一些 PHP 的实际问题。这是我使用 PHP 的第一个网页,所以现在一切都非常新鲜和模糊。

我的页面显示正确,但我现在的问题是我的 PHP 在提交页面时似乎不想调用我的 processForm() 函数。事实上,当我单击提交时,我收到 403 Forbidden 错误。我正在使用 WAMP,我尝试将 httpd.conf 更改为 Allow for All,但没有成功。

此外,除了 processForm() 显然没有发生之外,我还想获取我的表单数据(姓名、电子邮件和复选框选项)并将信息放入文件中。每次有人访问该页面时,我都想附加到文件中。另外,我必须使用独占文件锁。我为此编写了一个函数,但显然该文件甚至没有被创建。

如果您不介意查看我的处理函数及其调用,以及我的 writeFile() 函数,我将不胜感激。这对我来说都是新事物,我真的很想弄清楚这一点。提前感谢您提供的任何帮助/知识!

我只是向您展示我的 index.php 文件,因为我的所有其他文件只是格式化特定和 Javascript 验证,我知道这是有效的。

<?PHP
require("header.php");

require("content.php");

if (isset($_POST['_submit_check']))  {
echo processForm();
}
else  {
echo displayForm();
}

// ------------------------------------------------
function processForm()  {    
if (isset($_POST['option']))  {
    $_POST['option'] = implode(', ', $_POST['option']);
}

echo "Your Name:  {$_POST['name']}<br />";
echo "Email Address:  {$_POST['email']}<br />";
echo "Area(s) of Study:  {$_POST['option']}<br />"; 
}

function displayForm()  {
$htmlblock = '
    <form name = "contact" method = "POST" action="<?php echo $_SERVER[PHP_SELF]?>">                
    <div class = "appearance">
    *All fields required.*<br /><br />
    Name: *  <input title="Name required!" type = "text" id = "name" name = "name"        required autofocus onchange = "validName();" size = 30>
    <br />
    Email: * <input title="Email address of form username@domain required!" type =   "email" placeholder = "me@example.com" id = "email" name = "email" 
    required onchange = "validAddress();" size = 30>
    <br /><br /><br />
    Areas of Study <br />
    Check the program(s) in which you are interested in. (*Check AT LEAST one.*) <br />
    <input type = "checkbox" id = "cs" name = "option[]">Computer Science<br />
    <input type = "checkbox" id = "cis" name = "option[]">Computer Information Systems<br     />
    <input type = "checkbox" id = "ddep" name = "option[]">Dual Degree Engineering    Program<br /><br /><br />
    <div class = "center">
        <input type="submit" value="Submit" onClick = "return validOption()" onSubmit =     "writeFile()" onSubmit = "processForm()">
        <input type="reset" value="Clear">
    </div>
    <input type = "hidden" name="_submit_check" value="1"/>
    </div>
    <br />
    <br />
    </form>'; 
    return $htmlblock;
}

function writeFile()  {
$file = 'PreviewDayInfo.txt';
$name = $_POST['name'];
$email = $_POST['email'];

if (isset($_POST['option']))  {
    $_POST['option'] = implode(', ', $_POST['option']);
    $options = $_POST['option'];
}

$write = $name + ", " + $email + ", " + options + "\n";
file_put_contents($file, $write, FILE_APPEND | LOCK_EX);
}


require("footer.php");
?>
4

1 回答 1

1

您已将 PHP 代码放入普通字符串中。该部分<?php echo $_SERVER[PHP_SELF]?>没有被处理,而是按原样返回到浏览器。$htmlblock 的前几行需要更改为:

$htmlblock = '<form name = "contact" method = "POST" action="'. $_SERVER[PHP_SELF] .'">                
    <div class = "appearance">

此外,属性中的等号前后不应有空格。例如。像这样写:name="contact"而不是name = "contact".

于 2013-10-27T19:48:56.907 回答