gpt4 book ai didi

php - 发布到自己的页面而不是将信息提交到文本文档

转载 作者:行者123 更新时间:2023-11-28 02:52:22 28 4
gpt4 key购买 nike

我正在为我的 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();
}
?>

最佳答案

您可以完成所有这一切,而无需尝试使功能正常工作的复杂性。它可以用函数来完成,但你需要在函数内添加全局变量或状态变量,即函数示例($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 '])) 命令以确保在按下提交按钮之前不会运行代码。

希望这对您有所帮助。

关于php - 发布到自己的页面而不是将信息提交到文本文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38885526/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com