发布到自己的页面不向文本文档提交信息

时间:2016-08-11 00:26:45

标签: php html

我正在为我的11年级作业创建一个网站,我们打算发布到一个单独的页面“订阅时事通讯”并将名称和电子邮件发送到文本文档,但我决定接受它进一步将其发布到它自己的页面。但是,除非另有说明,否则即使它为'TRUE'也不会设置'$ ok = TRUE'。按下提交按钮时,它将始终显示“发生错误。您尚未订阅。”

我已将所有if语句放在函数中,就好像我没有显示所有可能的错误代码一样,因为输入字段默认为空。

这是我的代码:

  <div class="form">
    <form action="" method="post">
        <p>Name: <input type="text" name="name" size="20"></p>

        <p>Email: <input type="text" name="email" size="20"></p>

        <p>Confirm email: <input type="text" name="email2" size="20"></p>

        <input type="submit" name="submit" value="Submit">
    </form>
</div>

<?php

    //Converts data into string
    $name = $_POST['name'];
    $email = $_POST['email'];
    $email2 = $_POST['email2'];
    $ok = TRUE; 

    function fp() {
        if($fp = fopen('nameemail.txt', 'a+')) {
            $ok = TRUE;
            print '<p>fp true</p>';
        }else{
            $ok = FALSE;
            print '<p>fp false</p>';
        };
    }

    function emptyname() {
        if (empty($_POST['name'])){
            print'<font color="red"><p>You did not enter your name</p></font>';
            $ok = FALSE;
        };
    }

    function emptyemail() {
        if (empty($_POST['email'])){
            print'<font color="red"><p>You did not enter an email</p></font>'; 
            $ok = FALSE;
        };
    }

    function emptyemail2() {
        if (empty($_POST['email2'])){
            print'<font color="red"><p>You did not enter a confirm email</p></font>'; 
            $ok = FALSE;
        };
    }

    function emailemail2() {
        if ($_POST['email'] != $_POST['email2']){
            print'<font color="red"><p>Your emails did not match</p></font>';  
            $ok = FALSE;
        };
    }

    function ifok() {
        if($ok == TRUE) {
            fwrite($fp, "{$_POST['name']} - {$_POST['email']}\r\n");
            fclose($fp);
            print "<p><strong>You have been sucessfully subscribed</strong></p>";
            print "<p>Name: $name</p>";
            print "<p>Email: $email</p>";
        }elseif($ok == FALSE){
            print '<p>An error occurred. You have not been subscribed.</p>';
        };
    }

    function okfalse() {
        if($ok == FALSE){
            '<p>Ok is false</p>'; 
        }else{
            '<p>Ok is true</p>';
        };
    }

    if(isset($_POST['submit'])){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $email2 = $_POST['email2'];
        $ok = TRUE;
        fp();
        emptyname();
        emptyemail();
        emptyemail2();
        ifok();
        okfalse();
    }   
?> 

3 个答案:

答案 0 :(得分:0)

您需要参考有关变量范围的内容。如果您想更改<form method="post"> 的值,请使用$ok作为参数,或将其放在课堂中,请&$ok使用。

答案 1 :(得分:0)

使用$ok更改每个函数内的$GLOBALS['ok'] ...您无法直接在函数中引用全局变量。

实施例

function ifok() {
    if($GLOBALS['ok'] == TRUE) {

        fwrite($fp, "{$_POST['name']} - {$_POST['email']}\r\n");
        fclose($fp);

        print "<p><strong>You have been sucessfully subscribed</strong></p>";
        print "<p>Name: $name</p>";
        print "<p>Email: $email</p>";

    }elseif($GLOBALS['ok'] == FALSE){
        print '<p>An error occurred. You have not been subscribed.</p>';
    };
}

另外,只需在函数开头添加global $ok就可以节省大量时间

function ifok() {
    global $ok;
    if($ok == TRUE) {

        fwrite($fp, "{$_POST['name']} - {$_POST['email']}\r\n");
        fclose($fp);

        print "<p><strong>You have been sucessfully subscribed</strong></p>";
        print "<p>Name: $name</p>";
        print "<p>Email: $email</p>";

    }elseif($ok == FALSE){
        print '<p>An error occurred. You have not been subscribed.</p>';
    };
}

答案 2 :(得分:0)

您可以完成所有这些操作,而无需尝试使功能正常工作。它可以用函数完成,但你需要在函数中添加全局变量或状态变量,即函数示例($ ok)等。此外,使用这些函数的布局比它需要的更复杂,并且发布的布局将不断调用isok()函数是否所有字段都已填写。要执行您要查找的内容,可以将代码更改为以下内容:

<?php
if(isset($_POST['submit'])){
    $filename='nameemail.txt';
if (file_exists($filename)) {
    $ok = TRUE;
}else{
    $ok = FALSE;
}
if (empty($_POST['name'])){
    print'<font color="red"><p>You did not enter your name</p></font>';
    $ok = FALSE;
}
if (empty($_POST['email'])){
    print'<font color="red"><p>You did not enter an email</p></font>'; 
    $ok = FALSE;
}
if (empty($_POST['email2'])){
    print'<font color="red"><p>You did not enter a confirm email</p></font>'; 
    $ok = FALSE;
}
if ($_POST['email'] != $_POST['email2']){
    print'<font color="red"><p>Your emails did not match</p></font>';  
    $ok = FALSE;
}
    $name = $_POST['name'];
    $email = $_POST['email'];
    $email2 = $_POST['email2'];

if($ok == TRUE) {
$filename='./nameemail.txt';
$fp = fopen($filename, 'a+');
fwrite($fp, "{$_POST['name']} - {$_POST['email']}\r\n");
fclose($fp);

print "<p><strong>You have been sucessfully subscribed</strong></p>";
print "<p>Name: $name</p>";
print "<p>Email: $email</p>";

}elseif($ok == FALSE){
print '<p>An error occurred. You have not been subscribed.</p>';
} 
}
?>

<div class="form">
<form action="" method="post">
<p>Name: <input type="text" name="name" size="20"></p>
<p>Email: <input type="text" name="email" size="20"></p>
<p>Confirm email: <input type="text" name="email2" size="20"></p>
<input type="submit" name="submit" value="Submit">
</form>
</div>

我已选择向您展示此方法,因为如果您未将请求添加到函数中,则会显示错误,这是因为您需要将该批包装在if(isset($ _ POST ['submit]中')))命令确保在按下提交按钮之前不会运行代码。

希望这有帮助。