Search code examples
phpformsuser-input

PHP based form not sending content? Looking for a fresh set of eyes


I'm writing a simple PHP subscription form and when it runs it's not doing anything. The PHP looks like this:

<form class='subscribe' method='post' action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>'>
                Subscribe for occasional updates and reminders!
                Email: <input type='text' class='input' name='email' id='email' >
                <!--There will be some type of CAPTCHA here-->
                    <label for='Subscribe'>
                        <input value='Subscribe' type='submit' id='subscribebutton' class='input' name='subscribebutton'>
                    </label>
                <span class='error'><?php echo $inError ?> </span>
                <?php
                      $email = '';
                      $inError = '';
                      if($_SERVER['REQUEST_METHOD'] == 'POST') {
                        if(empty($_POST['email']))
                            $inError = "You must provide an email to register!";
                        elseif(stripos($_POST['email'],'@') !== TRUE || stripos($_POST['email'],'.') !== TRUE)
                            $inError = "Please provide a properly formatted email to register.";
                        else
                        {
                            $email = sanatizeInput($_POST['email']);
                            $emailFile = fopen('emails.txt','a') or die ('Sorry. Subscriptions are disabled for the time being.');
                            fwrite($emailFile, $email);
                        }
                      }
                      function sanatizeInput($data) {
                          $data = trim($data);
                          $data = stripslashes($data);
                          $data = htmlspecialchars($data);
                          return $data;
                      }

                ?>

It's not throwing errors if I submit the form with no content or improperly formatted content, it's not writing a file, it appears that it's doing precisely nothing when I press the submit button (although it does clear the field and appears to submit the form).
It's probably something simple, but my eyes are totally missing it.
Any help would be appreciated.
Thanks in advance, Zach


Solution

  • Close the form tag properly then move your php code top of the page. Something like this, it should work.

    <?php
    $email = '';
    $inError = '';
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if (empty($_POST['email']))
            $inError = "You must provide an email to register!";
        elseif (stripos($_POST['email'], '@') !== TRUE || stripos($_POST['email'], '.') !== TRUE)
            $inError = "Please provide a properly formatted email to register.";
        else {
            $email = sanatizeInput($_POST['email']);
            $emailFile = fopen('emails.txt', 'a') or die('Sorry. Subscriptions are disabled for the time being.');
            fwrite($emailFile, $email);
        }
    }
    
    function sanatizeInput($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    ?>
    <form class='subscribe' method='post' action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>'>
        Subscribe for occasional updates and reminders!
        Email: <input type='text' class='input' name='email' id='email' >
        <!--There will be some type of CAPTCHA here-->
        <label for='Subscribe'>
            <input value='Subscribe' type='submit' id='subscribebutton' class='input' name='subscribebutton'>
        </label>
        <?php if(!empty($inError)){ ?><span class='error'><?php echo $inError; ?> </span> <?php } ?>
    </form>