Functions and conditional IFs

This is a discussion on "Functions and conditional IFs" within the PHP Forum section. This forum, and the thread "Functions and conditional IFs are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Sep 15th, 2007, 17:34
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Functions and conditional IFs

I am having a problem with the IF statements in my functions.

What I am trying to do is when the page first loads, the form is prefilled with values from the database. If the user makes a change to one of the fields and it doesn't validate, the field is rest to the value from the database.

I have that part working where the values are put in the form on page load and the errored field has the dtabase value put back in, but if all the other valid fields have their variables wiped and the fields are empty.

The problem I think is with these functions

PHP: Select all

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;
      }
     }
  } 
Here is the whole script with the validation
PHP: Select all

<?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))
 {
     
$get_first_name $row['first_name'];
     
$get_last_name $row['last_name'];
     
$get_username $row['username'];
     
$get_interests $row['interests'];
 }
 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;
      }
     }
  }
 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 <span><b>Username</b></span> 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;
         }
  
//These just test to make sure what variables are getting passed
  
echo $username."<br>";
  echo 
$first_name."<br>";
  echo 
$last_name."<br>";
  echo 
$interests."<br>";
  echo 
$get_first_name."<br>";
  echo 
$get_last_name."<br>";
  echo 
$get_username."<br>";
  echo 
$get_interests."<br>";
  echo 
$get_username;
     } 
 }
 function 
show_form() { 
     global 
$_POST$print_again$error$get_interests;
     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 error_username($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 error_firstname($error); ?>" /></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 error_lastname($error); ?>" /></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 $get_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">&nbsp;</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(); 
    }
}
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Sep 16th, 2007, 14:37
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Functions and conditional IFs

I think the problem you are having is because you are only setting the index for each field if there is an error.

eg. $error['last_name'] will only exist if there is a problem with it.

When you're checking to see if there has been an error you can try

if (!empty($error['last_name']))
{
//etc
}

Depending of your PHP version, this may be throwing a notice that is messing the rest up?

If that's not it, what does the function check_input() do?

Cheers,
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Sep 16th, 2007, 14:59
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Functions and conditional IFs

check_input() is a function that cleans the user input such as mysql_real_escape_string

I will check that suggestion of yours becuase it is the best thing that I haven't tried
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Sep 16th, 2007, 20:47
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Functions and conditional IFs

I have checked the logs and I am getting these notices

Quote:
[error] PHP Notice: Undefined variable: message in d:\\Apache\\htdocs\\jack\\edit_profile.php on line 104
[Sun Sep 16 21:36:22 2007] [error] PHP Notice: Undefined index: last_name in d:\\Apache\\htdocs\\jack\\includes\\functions.php on line 140
[Sun Sep 16 21:36:22 2007] [error] PHP Notice: Undefined index: last_name in d:\\Apache\\htdocs\\jack\\edit_profile.php on line 48
[Sun Sep 16 21:40:43 2007] [error] PHP Notice: Undefined variable: message in d:\\Apache\\htdocs\\jack\\edit_profile.php on line 104
[Sun Sep 16 21:40:43 2007] [error] PHP Notice: Undefined index: last_name in d:\\Apache\\htdocs\\jack\\includes\\functions.php on line 140
[Sun Sep 16 21:40:43 2007] [error] PHP Notice: Undefined index: last_name in d:\\Apache\\htdocs\\jack\\edit_profile.php on line 48
[Sun Sep 16 21:41:07 2007] [error] PHP Notice: Undefined index: username in d:\\Apache\\htdocs\\jack\\includes\\functions.php on line 140
[Sun Sep 16 21:41:07 2007] [error] PHP Notice: Undefined index: username in d:\\Apache\\htdocs\\jack\\edit_profile.php on line 17
[Sun Sep 16 21:41:16 2007] [error] PHP Notice: Undefined index: username in d:\\Apache\\htdocs\\jack\\includes\\functions.php on line 140
[Sun Sep 16 21:41:16 2007] [error] PHP Notice: Undefined index: username in d:\\Apache\\htdocs\\jack\\edit_profile.php on line 17
[Sun Sep 16 21:41:16 2007] [error] PHP Notice: Undefined index: last_name in d:\\Apache\\htdocs\\jack\\includes\\functions.php on line 140
[Sun Sep 16 21:41:16 2007] [error] PHP Notice: Undefined index: last_name in d:\\Apache\\htdocs\\jack\\edit_profile.php on line 48
[Sun Sep 16 21:41:21 2007] [error] PHP Notice: Undefined variable: message in d:\\Apache\\htdocs\\jack\\edit_profile.php on line 93
[Sun Sep 16 21:41:21 2007] [error] PHP Notice: Undefined index: username in d:\\Apache\\htdocs\\jack\\includes\\functions.php on line 140
[Sun Sep 16 21:41:21 2007] [error] PHP Notice: Undefined index: username in d:\\Apache\\htdocs\\jack\\edit_profile.php on line 17
[Sun Sep 16 21:41:33 2007] [error] PHP Notice: Undefined index: username in d:\\Apache\\htdocs\\jack\\includes\\functions.php on line 140
[Sun Sep 16 21:41:33 2007] [error] PHP Notice: Undefined index: username in d:\\Apache\\htdocs\\jack\\edit_profile.php on line 17
This is the function it's kicking up a fuss about in the includes folder
PHP: Select all

function error_bool($error$field) { 
    if(
$error[$field]) { 
 print(
"style=\"background-color:#FBEC87\""); 
    } 
    else {
 print(
"style=\"background-color:white\"");
    }

What the other functions are supposed to do is check if there is an error with i.e. Username. If that is Ok, check to see if the username is different and not empty. If that passes, and the print_again variable is false there is nothing left for it to do becuase it can either be the original username or a new username. the problem is, it is exiting the IFs without selecting one.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Sep 17th, 2007, 08:18
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Functions and conditional IFs

Hi Adrock,

You would be getting this error for the same reason as before -- the error field won't actually be there unless the error occured.

You should probably check for the error's existence using

if (!empty($error[$field]))
{
// etc..

Unless you know for certain the arraay key will be there.

Cheers
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Type Input in Conditional Statement affirmallchance Flash & Multimedia Forum 3 Jan 9th, 2008 11:30
[SOLVED] conditional statement firefox saltedm8 Web Page Design 9 Nov 25th, 2007 18:13
[SOLVED] How do I write IE conditional comment Lchad Web Page Design 23 Oct 17th, 2007 01:29
Conditional IF in external stylesheet AdRock Web Page Design 6 Sep 23rd, 2007 22:45
ASP stylesheet switcher with conditional comments Shelly Classic ASP 1 Apr 19th, 2006 12:44


All times are GMT. The time now is 15:55.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42