brandonb2000 brandonb2000 - 3 months ago 11
HTML Question

Posting to own page not submitting info to text document

I am working on creating a site for my Year 11 assignment, we are meant to post to a separate page to "subscribe to a newsletter" and sending the name and email into a text document, however I decided to take it a step further and post it to it's own page. However it is not setting '$ok = TRUE' even when it to be 'TRUE' unless stated otherwise. When the submit button is pressed it will always say "An error occurred. You have not been subscribed."

I have put all the if statements in functions as if I did not it would show all the possible error codes as the input fields are empty by default.

Here is my code:

<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();
}
?>

Answer

You can do all of this without the complexity of trying to make the functions work. It can be done with functions, but you need to add globals or state variables within the function ie function example($ok) etc. Also, the layout using such functions is more complicated than it needs to be and the layout of posting will constantly call upon the isok() function whether or not all fields have been filled in. To do what you're looking for you could change the code to the following:

<?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>

I've chosen to show you this method as you mentioned errors being shown if you didn't add the requests to the functions, this is because you need to wrap the lot in the if(isset($_POST['submit'])) command to ensure the code won't be run until the submit button has been pressed.

Hope this helps.