Jump to content

Recommended Posts

I have a script i started in which I am trying to validate required fields before passing onto the next page/process.  It is for what will become a multistep form.

 

Here is the code:

 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Required Form Fields</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
        <?php
        # Script 4.1 - register.php
        
        /*	This page creates a registration form
         *	which is then validated using various functions.
         */
        
        if (isset($_POST['submitted'])) { // Handle the form.
            
            // Store errors in an array:
            $errors = array();
            
            // Check for non-empty name:
            if (!isset($_POST['name']) OR empty($_POST['name'])) {
                $errors[] = 'name';
            }
            
            // Validate the email address using eregi():
            if (!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', $_POST['email'])) {
                $errors[] = 'email address';
            }
            
            // Check for non-empty comments:
            if (!isset($_POST['comments']) OR empty($_POST['comments'])) {
                $errors[] = 'comments';
            }
            
            if ( empty($errors)) { // Success!
                
                // Print a message and quit the script:
                /*echo '<p>You have successfully registered (but not really).</p></body></html>';*/
                $action = "post_req_form.php";
                exit();
            
            } else { // Report the errors.
                
                echo '<p>Problems exist with the following field(s):<ul>';
                
                foreach ($errors as $error) {
                    echo "<li>$error</li>\n";
                }
                
                echo '</ul></p>';
            
            }
        
        } // End of $_POST['submitted'] IF.
        
        // Show the form.
        
        ?>
        <form action="<?php echo $action;?>" method="post">
            <fieldset>
                <legend>
                    Registration Form
                </legend>
                <p>
                    <STRONG>Name:</STRONG>
                    <input type="text" name="name" size="50" maxlength="50" value="">
                </p>
                <p>
                    <STRONG>E-Mail:</STRONG>
                    <input type="text" name="email" size="50" maxlength="200">
                </p>
                <p>
                    <STRONG>Comments:</STRONG>
                    <br/>
                    <textarea name="comments" rows="5" cols="40">
                    </textarea>
                </p>
                <input type="hidden" name="submitted" value="true" /><input type="submit" name="submit" value="Submit" />
            </fieldset>
        </form>
    </body>
    </head>

 

 

The hang up is when I try to use and echo statement into the action field of the form. it takes me to

 

file/$action

 

rather then

 

file/process_req_form.php

 

and any help would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/172476-assistance-needed-on-a-form/
Share on other sites

When your page loads for the first time:

 

<form action="<?php echo $action;?>" method="post">

 

Will output:

 

<form action="" method="post">

 

The user fills out the form and submits, the output is now:

 

<form action="post_req_form.php" method="post">

 

---

 

Change:

 

<form action="<?php echo $action;?>" method="post">

 

To:

 

<form action="" method="post">

 

And:

 

$action = "post_req_form.php";

 

To:

 

include('post_req_form.php');

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.