SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form Validation Script - Works with <input> tag but not <textarea> tag

    I'm new to PHP, but I'm trying to get a simple form validation script to work for a contact page. The "required" fields should show an error if they are not filled out. For some reason it won't work for the <textarea> element (message field). Since I've found out this element doesn't allow for a value="" attribute I put the script snippet between the textarea tags which doesn't seem to work - unless I'm missing something somewhere else. I don't know where else I'd put the statement. Here is a link to the page to give you a visual of what I'm talking about: http://www.sitekitchen.us/form

    Here's the script and the form:


    Code PHP:
    <?php
    // define variables and initialize with empty values
    $nameErr = $emailErr = $feedbackErr = "";
    $name = $email = $feedback = "";
     
     
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["name"])) {
            $nameErr = "Missing";
        }
        else {
            $name = $_POST["name"];
        }
     
        if (empty($_POST["email"]))  {
            $emailErr = "Missing";
        }
        else {
            $email = $_POST["email"];
        }
     
        if (!isset($_POST["feedback"])) {
            $feedbackErr = "Missing";
        }
        else {
            $feedback = $_POST["feedback"];
        }
     
    }
     
     
    ?>


    Code HTML4Strict:
    <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    		<fieldset>
    		<div>
     
    		<label>
    		<span class="error"><?php echo $nameErr;?></span>
    		<span>Your name</span> <span class="require">(required)</span><input type="text" name="name" value="<?php echo htmlspecialchars($name);?>" />
     
    		</label>
    		<label>
    		<span class="error"><?php echo $emailErr;?></span>
    		<span>Email Address</span> <span class="require">(required)</span><input type="text" name="email" value="<?php echo htmlspecialchars($email);?>" />
     
    		</label>
    		<label>
    		<span>Website</span><input type="text" name="website" id="website" />
    		</label>
    		<label>
    		<span>Subject</span><input type="text" name="subject"  id="subject" />
    		</label>
    		<label>
    		<span class="error"><?php echo $feedbackErr;?></span>
    <span>Message</span> <span class="require">(required)</span><textarea name="feedback" id="feedback"><?php echo htmlspecialchars($feedback);?></textarea>
    		</label>
    		<label>
    		<input type="submit" name="submit" id="submit" class="button" value="Submit" />
    		</label>					
    		</div>
    		</fieldset>
    		<div>
     
    		</div>
    		</form>

  2. #2
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    500
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    The POST key "feedback" will always "be set" so change your error check to empty like the others.

  3. #3
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That was it! Thanks a million Drummin. I learned a little something there. Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •