SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with "If user submits form" display link to next page else "fill the form first"

    Hi,

    I'm trying to modify the code below to use. Basically, it's a simple form a user is required to fill-out. When the form is filled and submitted a button will appear, if the form is not filled or submitted a text reading "Fill the form" will show. I need help with the button showing part I was hoping someone here could help out. My knowledge of PHP is below basic.


    The HTML Form

    HTML Code:
    <form action="result.php" method="post" class="niceform">
    	<fieldset>
        	<legend>Pet's Information</legend>
            <dl>
            	<dt><label for="name">Name:</label></dt>
                <dd><input type="text" name="name" id="name" size="32" maxlength="128" /></dd>
            </dl>
            <dl>
            	<dt><label for="location">Location:</label></dt>
                <dd><input type="location" name="location" id="location" size="32" maxlength="32" /></dd>
            </dl>
        </fieldset>
        <fieldset class="action">
        	<input type="submit" name="submit" id="submit" value="Submit" />
        </fieldset>
    </form>



    The PHP Code

    PHP Code:

    <?php  
    if(isset($_POST['submit']))
    {   
      if(
    $_POST['name'] && $_POST['location'])
      {
        
    $file fopen("black_listed_words.txt"'r');
        if(
    $file)
        {
      
    $blacklisted_array explode(",",fgets($file));
      
    $input $_POST['name'].' '.$_POST['location'];
      
    $input_words explode(" "$input);
      
    $result array_intersect($blacklisted_array$input_words);
      if(
    $result)
      {
      echo 
    'you have entered a blacklisted word.';
      }
      else
      {
      
    fclose($file);
      
    $file2 fopen("input_info.txt"'a');
      
    $input_info "$input\r\n";
      
    fwrite($file2$input_info);
      
    fclose($file2);
      echo 
    'Login successful. Click here to continue <form action=""><input type="submit" value="Enter" /></form>';
      }
        }
        else
        {
      echo 
    'No such file exists.';
        }
      }
      else
      {
        echo 
    'Please fill in both fields before submitting the form.';
      }
    }
    ?>

    When a user successfully submits the form above a button will appear here linking to an external page. On the external page are some basic text with something basic like

    PHP Code:
    This is the result for <strong><?php echo $_GET["name"]; ?> located in <strong><?php echo $_GET["location"]; ?></strong>

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    The trick here is to post the form to the same page (let's call it form.php) instead of result.php. If the user has forgotten to fill some fields, tell them so, and if they've completed the form correctly, send them to a 'thank you' type page.

    Pseudo-code for how it should work

    form.php

    PHP Code:
    <?php

    $errors 
    = array();

    if(!empty(
    $_POST)) {
        
        if(empty(
    $_POST['name'])) {
            
    $errors['name'] = 'Name is a required field';
        }
        
        if(empty(
    $_POST['name'])) {
            
    $errors['location'] = 'Location is a required field';
        }
        
        if(empty(
    $errors)) {
            
    // process form!
            
    do_stuff();
            
            
    // send the user to a thank you page:
            
    header('Location: http://www.mydomain.com/success.php'); // redirect
            
    die();
        }
        
    }

    // display form
    include('form_template.php');

    ?>

    form_template.php:
    PHP Code:

    <?php
    if(!empty($errors)) {
        echo 
    '<ul class="errors">';
        foreach(
    $errors as $error) {
            echo 
    '<li>'.$error.'</li>';
        }
        echo 
    '</ul>';
    }
    ?>

    <form action="result.php" method="post" class="niceform">
        <fieldset>
            <legend>Pet's Information</legend>
            <dl>
                <dt><label for="name">Name:</label></dt>
                <dd><input type="text" name="name" id="name" size="32" maxlength="128" value="<?php echo !empty($_POST['name']) ? htmlentities($_POST['name']) : '' ?>" /></dd>
            </dl>
            <dl>
                <dt><label for="location">Location:</label></dt>
                <dd><input type="location" name="location" id="location" size="32" maxlength="32" value="<?php echo !empty($_POST['location']) ? htmlentities($_POST['location']) : '' ?>" /></dd>
            </dl>
        </fieldset>
        <fieldset class="action">
            <input type="submit" name="submit" id="submit" value="Submit" />
        </fieldset>
    </form>

    Does that make sense?

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Immerse View Post
    Does that make sense?
    Makes sense and I actually like the send to a thank you page idea. I've added the blacklisted snippet to line 28 of form.php


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
  •