Jump to content

Recommended Posts

I have a function which decides what to display as the value in an HTML form

 

The problem is I can get the new username to stay in the text box but the original username is not set to start with.  it's just an empty field.

I think it has something to do with this line becuase the value is true so it is assigning $username to the text field and because it is empty it displays nothing

elseif($get_username != $username) {
    echo $username;	
}

 

function error_username($error) {
    global $get_username, $print_again, $username;

    //if the new username has got an error, display the original username 
    if($error['username']) { 
        echo $get_username; 
    }
    elseif($get_username != $username) {
	echo $username;	
    }
    else
    {
                if(!$print_again) {
            echo $get_username;
    		}
    }
	}

 

On page first load I want this variable set and if there is an error use this variable again

$get_username = $row['username'];

 

If the field validates I want this variable

$username = check_input($_POST['username']);

 

Have you tried printing out the values of all the variables you are testing?  Something like this:

 

print "<br>About to test error[username]: {$error['username']}";
if ($error['username']) {
  print "<br>error[username'] evaluated to true";
  # stuff
}

I have got it replacing the field with the original variable on failed validation and if it validates it keeps the new value but what it does now is wipe the values from the other 2 fields

 

 

	$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'];
}

the functions

	function error_username($error) {
    global $get_username, $print_again, $username;

    //if the new username has got an error, display the original username 
    if($error['username']) { 
        echo $get_username; 
    }
    elseif(($get_username != $username) && ($username != null)) {
	echo $username;	
    }
    else
    {
                if(!$print_again) {
            echo $get_username;
    		}
    }
	}

function error_firstname($error) {
    global $get_first_name, $print_again, $first_name;

    //if the new username has got an error, display the original username 
    if($error['first_name']) { 
        echo $get_first_name; 
    }
    elseif(($get_first_name != $first_name) && ($first_name != null)) {
	echo $first_name;	
    }
    else
    {
                if(!$print_again) {
            echo $get_first_name;
    		}
    }
}

function error_lastname($error) {
    global $get_last_name, $last_name, $print_again;

    //if the new username has got an error, display the original username 
    if($error['last_name']) { 
        echo $get_last_name; 
    }
    elseif(($get_last_name != $last_name) && ($last_name != null)) {
	echo $last_name;	
    }
    else
    {
                if(!$print_again) {
            echo $get_last_name;
    		}
    }
	}

 

The validation

	function check_form() { 
    global $_POST, $error, $print_again, $get_username, $get_first_name, $get_last_name, $get_interests;
    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;
	 $oldvalue = $get_last_name; 
        $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>";
    }

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.