Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Forming Help

  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    153
    Thanks
    53
    Thanked 0 Times in 0 Posts

    Forming Help

    Hello. I'm trying to make a really simple contact form.
    I have name and a content box and with a submit button. When the user presses submit, the form action goes to /page.php?done=1 and then the form gets sent. But I am getting a lot of empty fields. How can I make it so if the user doesn't fill one of the fields, it give them an error and they have to go back to fill it.
    Here is my code:

    PHP Code:
    <?php if($_GET['done'] == 1)
    {
    echo 
    "<center>Thank you for your message. We will have a new site shortly.</center>";}

    //BLAH BLAH SEND CODES
        
        
    else
        {
    ?>
    <center>

    <form id="contact" name="contact" method="post" action="?done=1">
    <br/>Name: <input type="text" name="name"/>
    <br/><br/>What would you like on the new site?
    <br/><textarea name="content" cols="75" rows="4"></textarea>
    <br/><input type="SUBMIT" value="Submit"/>
    </center>
    <? ?>

  • #2
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could use a

    PHP Code:
    if (empty($variable)) {
    die (
    "Please go back and enter a value in variable name"


  • #3
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    PHP Code:
    <?php if($_GET['done'] == 1)
    {
    extract($_POST);
    if (EMPTY(
    $name) || EMPTY($content)) {
    echo 
    'You left a field empty. Please fill it in.';
    }
    else {
    echo 
    "<center>Thank you for your message. We will have a new site shortly.</center>";}

    //BLAH BLAH SEND CODES
     
    }   
        else
        {
    ?>
    <center>

    <form id="contact" name="contact" method="post" action="?done=1">
    <br/>Name: <input type="text" name="name"/>
    <br/><br/>What would you like on the new site?
    <br/><textarea name="content" cols="75" rows="4"></textarea>
    <br/><input type="SUBMIT" value="Submit"/>
    </center>
    <? ?>
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Why the need for extract for two fields? Extract can dangerous some times. Also with the way it is setup now someone can just go to ?done=1 and attempt to process the form. I would check to see if the form was submitted through a post like so
    PHP Code:
    <?php
    if(isset($_POST['submit']))
    {
        
    $name $_POST['name'];
        
    $content $_POST['content'];
        if (
    trim($name) == '' || trim($content) == '')
        {
        echo 
    'You left a field empty. Please fill it in.';
        }
        else
        {
        echo 
    "<p style=\"text-align:center\" Thank you for your message. We will have a new site shortly.</p>";
        }
        
    //BLAH BLAH SEND CODES
    }   
    else
    {
    ?>

    <form id="contact" style="text-align:center" name="contact" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
    <br/>
    <label for="name">Name:</label>
    <input type="text" id="name" name="name"/>
    <br/>
    <br/>
    <label for="content">What would you like on the new site?</label> <br/>
    <textarea name="content" id="content" cols="75" rows="4"></textarea>
    <br/>
    <input type="submit" name="submit" value="Submit"/>
    <?php

    ?>
    I used htmlentities on the $_SERVER['PHP_SELF'] in order to help prevent xss injection. Also the <center> tag is deprecated. CSS should be used in its place.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

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