Jump to content

[SOLVED] Refill failed form validation with original values


AdRock

Recommended Posts

I have a form which takes values out of a database and fills the fields.

 

What I would like to do is, if the form fails it's validation i.e. field was left blank, it's original values are placed back in the text boxes.

 

I have got it to sort of work but I can't get it quite right

 

This is the new function i made but it need some tweaking

function error_field($error) { 
    if($error['first_name']) { 
print("value=\"<?php echo $get_first_name; ?>\""); 
    } 
    else {
print("value=\"<?php echo $first_name; ?>\"");
    }
}

 

and here is the rest of the form

$result = mysql_query("SELECT `first_name`, `last_name`, `username`, `interests` FROM `users` WHERE `username`='".$_SESSION['username']."'");
while($row = mysql_fetch_assoc($result))
{
    $get_first_name = $row['first_name'];
    $get_last_name = $row['last_name'];
    $get_username = $row['username'];
    $get_interests = $row['interests'];
}

function error_field($error) { 
    if($error['first_name']) { 
print("value=\"<?php echo $get_first_name; ?>\""); 
    } 
    else {
print("value=\"<?php echo $first_name; ?>\"");
    }
}



function check_form() { 
    global $_POST, $error, $print_again;
    global $first_name, $last_name, $username, $newsletter; 

    $first_name = check_input($_POST['first_name']);
    $last_name = check_input($_POST['last_name']);
    $interests = check_input($_POST['interests']);
    $username = check_input($_POST['username']);
    $newsletter = $_POST['newsletter'];

    $error['first_name'] = false;

    if(empty($first_name)) { 
        $error['first_name'] = true; 
         $print_again = true; 
        $message="<li>The <span><b>Forename</b></span> field is empty</li>"; 
    }
    else if(!ereg("^[A-Za-z]{2,30}$",$first_name)) {
	$error['first_name'] = true; 
         $print_again = true; 
        $message="<li><span><b>Forename</b></span> must contain letters only</li>";
    }

    if(empty($last_name)) { 
        $error['last_name'] = true; 
         $print_again = true; 
        $message.="<li>The <span><b>Surname</b></span> field is empty</li>"; 
    }
    else if(!ereg("^[A-Za-z\-]{2,30}$",$last_name)) {
	$error['last_name'] = true; 
         $print_again = true; 
        $message.="<li><span><b>Surname</b></span> must contain letters only</li>";
    }

    if(empty($username)) { 
        $error['username'] = true; 
         $print_again = true; 
        $message.="<li>The <span><b>Username</b></span> field is empty</li>"; 
    }
    else if( mysql_num_rows(mysql_query("SELECT username FROM users WHERE username = '$username'")) ) {
	       $error['username'] = true; 
         $print_again = true; 
        $message.="<li>The username you have selected has already been used by another member in our database. Please choose a different Username!</li>";
    }
    else if(!ereg("^[A-Za-z0-9 \-]{4,30}$",$username)) {
	$error['username'] = true; 
         $print_again = true; 
        $message.="<li><span><b>Username</b></span> must contain letters and numbers only</li>";
    }  

    if($print_again)
    {
   	echo "<h2 class=\"errorhead\">There has been an error:</h2><p>You forgot to enter the following field(s)</p>
   	    <ul id=\"validation\">$message</ul>";
   	show_form(); 
    }
    else
    {

	// Stop the form being used from an external URL
    	// Get the referring URL
    	$referer = $_SERVER['HTTP_REFERER'];
    	// Get the URL of this page
    	$this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
    	// If the referring URL and the URL of this page don't match then
    	// display a message and don't send the email.
    	if ($referer != $this_url)
 	{
            echo "You do not have permission to use this script from another URL.<br />";
	    echo "If you are behind a firewall please check your referrer settings.";
            exit;
        }
	echo $username."<br>";
	echo $first_name."<br>";
	echo $last_name."<br>";
	echo $interests."<br>";
    } 
}

function show_form() { 
    global $_POST, $print_again, $error;
    global $first_name, $last_name, $interests, $username, $newsletter; 

    ?> 
    <form method="post" id="login" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    	    	<fieldset>
    		    <legend>Username</legend>
	    <p class="hint">Please enter a username for your user account. Note that username should be between 4 and 30 characters.</p>
    		    <p><label for="username">Username:</label>
    		    <input type="text" <?php error_bool($error, "username"); ?> title="Please enter a username" id="username" name="username" size="30" <?php error_field($error); ?> /></p>
    	    	</fieldset>
    	    	<fieldset>
	    <legend>About You</legend>
    		    <p><label for="first_name">Forename:</label>
    		    <input type="text" <?php error_bool($error, "first_name"); ?> title="Please enter your first name" id="first_name" name="first_name" size="30" value="<?php echo $first_name; ?>" /></p>

    		    <p><label for="last_name">Surname:</label>
    		    <input type="text" <?php error_bool($error, "last_name"); ?> title="Please enter your last name" id="last_name" name="last_name" size="30" value="<?php echo $last_name; ?>" /></p>

	    <p class="hint">Please enter any interests/hobbies you have (optional).</p>
    		    <p><label for="interests">Your Interests:</label>
    		    <input type="text" title="Please enter any interests/hobbies you have" id="interests" name="interests" size="30" value="<?php echo $interests; ?>" /></p>
    	    	</fieldset>
    	    	<fieldset>
    		    <legend>Newsletter</legend>
	    <p class="hint">Sign up for jackgodfrey.org.uk newsletter. This newsletter includes what forthcoming events we are arranging, the latest Honeylands news and our latest news. You can Opt-out at any time....so sign up and give it a try!!</p>
    		    <p><label for="newsletter">Newsletter:</label>
    		    <input style="border:none" type="radio" value="yes" id="newsletter" checked="checked" name="newsletter" />Opt-in <input style="border:none" type="radio" value="no" name="newsletter" />Opt-out</p>
    	    	</fieldset>
    		    <p><label for="Submit" style="width: 20px"> </label>
    		    <input type="submit" name="submit" value="Edit Profile" class="sendbutton" />
    		    <input type="reset" value="Reset Fields" class="sendbutton" /></p>
    </form>
    <?php
} 
    if(isset($submit)) { 
    	check_form(); 
    } else { 
    	show_form(); 
    }
}
?> 

 

What it does at the minute is just echo this

<?php echo ; ?>

even if I enter some valid data in there

Link to comment
Share on other sites

I am unclear by what you mean by "original values are placed back in the text boxes". Do you mean you want the original values that you had pulled from the database or the original values as posted by the user. In either case, I think you are making this way too complicated.

 

Link to comment
Share on other sites

Yeah the original values that were pulled from the database

 

I have on field called last_name for example and if they try entering some garbage in that field and the validation picks up the error, i want the value pulled from the database back in the text box

Link to comment
Share on other sites

Ok, easy enough. Here would be the basic process I would take:

 

<?php

    //Create a validation flag
    $validation = false;

    if(isset($submit)) {
        //Modify the function check_form() to return back true if validation
        //passes, otherwise it returns back the error message(s)
        $validation = check_form();

        if ($validation === true) {
            //Process the form and redirect to confirmation or subsequent page
        }
    }

    //modify the show_form function so that if the passed value does not
    //equal false then it displays the passed value as error text
    //the function should also be where the query is done to get the values
    //to populate the fields
    $how_form($validation); 

?>

Link to comment
Share on other sites

Ok, after a re-read of your post I see that my response may not be exactly what you want. I will post back momentarily with another option.

 

I am little confused though, because it appears ytour current script will put the error messages into the text fields. If you will want to populate the fields with the db values, where do you want the error messages?

Link to comment
Share on other sites

I have got it to work a bit better but now I can't get the original value stored in the database to be displayed in the text boxes.

 

I can now some rubbish in the text boxes and it says original value which means it should display what is in the database but how do i get what's in the database in the form in the first place?

 

here is the new function

	function error_field($error, $field) { 
    if($error[$field]) { 
	print("value=\"old value\""); 
    } 
    else {
	print("value=\"new value\"");
    }
}

 

here is the whole script (again) with changes

 

<?php

    if (!is_authed()) 
    {
print ('You need to login to view this page, <a href="index.php?page=login">click here</a> to login.');
    }
    else
    {
echo "<h2>Edit Profile - ".$_SESSION['username']."</h2><hr />";

if(isset($_POST['submit']))
{
             $submit = $_POST['submit'];
}

$result = mysql_query("SELECT `first_name`, `last_name`, `username`, `interests` FROM `users` WHERE `username`='".$_SESSION['username']."'");
while($row = mysql_fetch_assoc($result))
{
    $first_name = $row['first_name'];
    $last_name = $row['last_name'];
    $username = $row['username'];
    $interests = $row['interests'];
}

function error_field($error, $field) { 
    if($error[$field]) { 
	print("value=\"old value\""); 
    } 
    else {
	print("value=\"new value\"");
    }
}

function check_form() { 
    global $_POST, $error, $print_again;
    global $first_name, $last_name, $username, $newsletter; 

    $first_name = check_input($_POST['first_name']);
    $last_name = check_input($_POST['last_name']);
    $interests = check_input($_POST['interests']);
    $username = check_input($_POST['username']);
    $newsletter = $_POST['newsletter'];

    $error['first_name'] = false;

    if(empty($first_name)) { 
        $error['first_name'] = true; 
         $print_again = true;
        $message="<li>The <span><b>Forename</b></span> field is empty</li>"; 
    }
    else if(!ereg("^[A-Za-z]{2,30}$",$first_name)) {
	$error['first_name'] = true; 
         $print_again = true;
        $message="<li><span><b>Forename</b></span> must contain letters only</li>";
    }

    if(empty($last_name)) { 
        $error['last_name'] = true; 
         $print_again = true; 
        $message.="<li>The <span><b>Surname</b></span> field is empty</li>"; 
    }
    else if(!ereg("^[A-Za-z\-]{2,30}$",$last_name)) {
	$error['last_name'] = true; 
         $print_again = true; 
        $message.="<li><span><b>Surname</b></span> must contain letters only</li>";
    }

    if(empty($username)) { 
        $error['username'] = true; 
         $print_again = true; 
        $message.="<li>The <span><b>Username</b></span> field is empty</li>"; 
    }
    else if( mysql_num_rows(mysql_query("SELECT username FROM users WHERE username = '$username'")) ) {
	       $error['username'] = true; 
         $print_again = true; 
        $message.="<li>The username you have selected has already been used by another member in our database. Please choose a different Username!</li>";
    }
    else if(!ereg("^[A-Za-z0-9 \-]{4,30}$",$username)) {
	$error['username'] = true; 
         $print_again = true; 
        $message.="<li><span><b>Username</b></span> must contain letters and numbers only</li>";
    }  

    if($print_again)
    {
   	echo "<h2 class=\"errorhead\">There has been an error:</h2><p>You forgot to enter the following field(s)</p>
   	    <ul id=\"validation\">$message</ul>";
   	show_form(); 
    }
    else
    {

	// Stop the form being used from an external URL
    	// Get the referring URL
    	$referer = $_SERVER['HTTP_REFERER'];
    	// Get the URL of this page
    	$this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
    	// If the referring URL and the URL of this page don't match then
    	// display a message and don't send the email.
    	if ($referer != $this_url)
 	{
            echo "You do not have permission to use this script from another URL.<br />";
	    echo "If you are behind a firewall please check your referrer settings.";
            exit;
        }
	echo $username."<br>";
	echo $first_name."<br>";
	echo $last_name."<br>";
	echo $interests."<br>";
    } 
}

function show_form() { 
    global $_POST, $print_again, $error;
    global $first_name, $last_name, $interests, $username, $newsletter; 

    ?> 
    <form method="post" id="login" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    	    	<fieldset>
    		    <legend>Username</legend>
	    <p class="hint">Please enter a username for your user account. Note that username should be between 4 and 30 characters.</p>
    		    <p><label for="username">Username:</label>
    		    <input type="text" <?php error_bool($error, "username"); ?> title="Please enter a username" id="username" name="username" size="30" value="<?php echo $username; ?>" /></p>
    	    	</fieldset>
    	    	<fieldset>
	    <legend>About You</legend>
    		    <p><label for="first_name">Forename:</label>
    		    <input type="text" <?php error_bool($error, "first_name"); ?> title="Please enter your first name" id="first_name" name="first_name" size="30" <?php error_field($error, "first_name"); ?> /></p>

    		    <p><label for="last_name">Surname:</label>
    		    <input type="text" <?php error_bool($error, "last_name"); ?> title="Please enter your last name" id="last_name" name="last_name" size="30" value="<?php echo $last_name; ?>" /></p>

	    <p class="hint">Please enter any interests/hobbies you have (optional).</p>
    		    <p><label for="interests">Your Interests:</label>
    		    <input type="text" title="Please enter any interests/hobbies you have" id="interests" name="interests" size="30" value="<?php echo $interests; ?>" /></p>
    	    	</fieldset>
    	    	<fieldset>
    		    <legend>Newsletter</legend>
	    <p class="hint">Sign up for jackgodfrey.org.uk newsletter. This newsletter includes what forthcoming events we are arranging, the latest Honeylands news and our latest news. You can Opt-out at any time....so sign up and give it a try!!</p>
    		    <p><label for="newsletter">Newsletter:</label>
    		    <input style="border:none" type="radio" value="yes" id="newsletter" checked="checked" name="newsletter" />Opt-in <input style="border:none" type="radio" value="no" name="newsletter" />Opt-out</p>
    	    	</fieldset>
    		    <p><label for="Submit" style="width: 20px"> </label>
    		    <input type="submit" name="submit" value="Edit Profile" class="sendbutton" />
    		    <input type="reset" value="Reset Fields" class="sendbutton" /></p>
    </form>
    <?php
} 
    if(isset($submit)) { 
    	check_form(); 
    } else { 
    	show_form(); 
    }
}
?> 

Link to comment
Share on other sites

Ok, one thing I would highly suggset is to not put yur functions within the "logic" poprtion of your script. Put all you functions at the top of the page or in an included file. Then you can easily see the flow of the script. Also, the script is a little heavy on the use of global variables when they are not needed.

 

Here is a quick rewrite. I'm sure there are erros, but I can't test this obviously. I am curious what the error_bool() function is doing. I suspect your validation function could be simplified, but I didn't try to do anything since I didn't know what that function does.

 

<?php
function check_form() { 

    global $first_name, $last_name, $username, $newsletter; 

    $first_name = check_input($_POST['first_name']);
    $last_name = check_input($_POST['last_name']);
    $interests = check_input($_POST['interests']);
    $username = check_input($_POST['username']);
    $newsletter = $_POST['newsletter'];

    $valid = true;

    $error['first_name'] = false;

    if(empty($first_name)) { 
        $error['first_name'] = true; 
         $valid = false;
        $message="<li>The <span><b>Forename</b></span> field is empty</li>"; 
    }
    else if(!ereg("^[A-Za-z]{2,30}$",$first_name)) {
$error['first_name'] = true; 
         $valid = false;
        $message="<li><span><b>Forename</b></span> must contain letters only</li>";
    }

    if(empty($last_name)) { 
        $error['last_name'] = true; 
         $valid = false; 
        $message.="<li>The <span><b>Surname</b></span> field is empty</li>"; 
    }
    else if(!ereg("^[A-Za-z\-]{2,30}$",$last_name)) {
$error['last_name'] = true; 
         $valid = false; 
        $message.="<li><span><b>Surname</b></span> must contain letters only</li>";
    }

    if(empty($username)) { 
        $error['username'] = true; 
         $valid = false; 
        $message.="<li>The <span><b>Username</b></span> field is empty</li>"; 
    }
    else if( mysql_num_rows(mysql_query("SELECT username FROM users WHERE username = '$username'")) ) {
       $error['username'] = true; 
       $valid = false; 
       $message.="<li>The username you have selected has already been used by another member in our database. Please choose a different Username!</li>";
    }
    else if(!ereg("^[A-Za-z0-9 \-]{4,30}$",$username)) {
$error['username'] = true; 
         $valid = false; 
        $message.="<li><span><b>Username</b></span> must contain letters and numbers only</li>";
    }  


    if(!$valid)
    {
   	echo "<h2 class=\"errorhead\">There has been an error:</h2><p>You forgot to enter the following field(s)</p>
        <ul id=\"validation\">$message</ul>";
    }

    return $valid;
}


function error_field($error, $field) { 
    if($error[$field]) { 
print("value=\"old value\""); 
    } 
    else {
print("value=\"new value\"");
    }
}

function show_form($first_name, $last_name, $interests, $username, $newsletter) { 
    global $error;


?> 
    <form method="post" id="login" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    	<fieldset>
    <legend>Username</legend>
    <p class="hint">Please enter a username for your user account. Note that username should be between 4 and 30 characters.</p>
    <p><label for="username">Username:</label>
    	    <input type="text" <?php error_bool($error, "username"); ?> title="Please enter a username" id="username" name="username" size="30" value="<?php echo $username; ?>" /></p>
    	</fieldset>
    	<fieldset>
    <legend>About You</legend>
    	    <p><label for="first_name">Forename:</label>
    	    <input type="text" <?php error_bool($error, "first_name"); ?> title="Please enter your first name" id="first_name" name="first_name" size="30" <?php error_field($error, "first_name"); ?> /></p>

    <p><label for="last_name">Surname:</label>
    <input type="text" <?php error_bool($error, "last_name"); ?> title="Please enter your last name" id="last_name" name="last_name" size="30" value="<?php echo $last_name; ?>" /></p>

    <p class="hint">Please enter any interests/hobbies you have (optional).</p>
    	    <p><label for="interests">Your Interests:</label>
    	    <input type="text" title="Please enter any interests/hobbies you have" id="interests" name="interests" size="30" value="<?php echo $interests; ?>" /></p>
    	</fieldset>
    	<fieldset>
    	    <legend>Newsletter</legend>
    <p class="hint">Sign up for jackgodfrey.org.uk newsletter. This newsletter includes what forthcoming events we are arranging, the latest Honeylands news and our latest news. You can Opt-out at any time....so sign up and give it a try!!</p>
    	    <p><label for="newsletter">Newsletter:</label>
    	    <input style="border:none" type="radio" value="yes" id="newsletter" checked="checked" name="newsletter" />Opt-in <input style="border:none" type="radio" value="no" name="newsletter" />Opt-out</p>
    	</fieldset>
    	    <p><label for="Submit" style="width: 20px"> </label>
    	    <input type="submit" name="submit" value="Edit Profile" class="sendbutton" />
    	    <input type="reset" value="Reset Fields" class="sendbutton" /></p>
    </form>
<?php
} 


//**********************//
//Page logic starts here//
//**********************//
if (!is_authed()) 
{
    print ('You need to login to view this page, <a href="index.php?page=login">click here</a> to login.');
}
else
{
    echo "<h2>Edit Profile - ".$_SESSION['username']."</h2><hr />";

    //Get the values from db
    $result = mysql_query("SELECT `first_name`, `last_name`, `username`, `interests` FROM `users` WHERE `username`='".$_SESSION['username']."'");
    $row = mysql_fetch_assoc($result);
    $first_name = $row['first_name'];
    $last_name = $row['last_name'];
    $username = $row['username'];
    $interests = $row['interests'];
    $newsletter = $_POST['newsletter'];

    $valid = false;
    if(isset($_POST['submit']))
    {
// Stop the form being used from an external URL
    	// Get the referring URL
    	$referer = $_SERVER['HTTP_REFERER'];
    	// Get the URL of this page
    	$this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
    	// If the referring URL and the URL of this page don't match then
    	// display a message and don't send the email.
    	if ($referer != $this_url)
	{
            echo "You do not have permission to use this script from another URL.<br />";
    echo "If you are behind a firewall please check your referrer settings.";
            exit;
        }

        //Validate the form
        $valid = check_form();
    }

    if($valid==true)
    {
        //Insert processing script and redirect here
        echo "Form was valid. Need to add processing script";
    }
    else
    {
        show_form($first_name, $last_name, $interests, $username, $newsletter); 
    }
}
?>

Link to comment
Share on other sites

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.