WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-13-2008, 09:45 PM
jinchiruki jinchiruki is offline
WebProWorld New Member
 

Join Date: Feb 2008
Posts: 3
jinchiruki RepRank 0
Angry undefine variable

Code:
 
<table width="1000" height="95" border="1">
  <tr>
    <td bgcolor="#0000FF">&nbsp;</td>
  </tr>
</table>

<center>

<?
   include("connect.php"); 
      //this is your validation in the form,put it here....
    { if (empty($_POST['name']))
      { 
      $errors[] = 'Please enter a name';
      }
      if (empty($_POST['email']))
      {
      $errors[] = 'Please enter a valid e-mail address';
      }
      else if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", 
      $_POST['email']))
      {
      $errors[] = 'Please enter a valid e-mail address';
      }
      if (empty($_POST['contact']))
      {
      $errors[] = 'Please enter a valid contact with numeric value';
      }
      else if (!is_numeric($_POST['contact']))
      {
      $errors[] = 'Please enter a valid contact with a numeric value';
      }
      if (empty($_POST['person_attend']))
      {
      $errors[] = 'Please enter some word for person attend';
      }
      else if (strlen ($_POST['person_attend']) > 255)
      {
      $errors[] = 'person_attend';
   }
      if (empty($_POST['comment']))
      {
      $errors[] = 'Please enter some comment';
      }
      else if (strlen ($_POST['comment']) > 255)
      {
      $errors[] = 'comment '; 
 }
      if($errors)
{
    foreach($errors as $val)
    {
        echo "ERROR: $val <br/>"; 
    }
}
 
      //process form
 
      //this is your add query....
  
      $name = $_POST['name'];
 
      $email = $_POST['email'];
 
      $contact = $_POST['contact'];
 
      $person_attend = $_POST['person_attend'];
  
      $comment = $_POST['comment'];
 
      $query = "INSERT INTO rsvp (id, name, email, contact, person_attend, comment)
   
      VALUES ('', '$name', '$email', '$contact', '$person_attend', '$comment')";
      
      $results = mysql_query($query) or die
  
      ("Could not execute query : $query." . mysql_error());
    
      {
 
      echo "thanks you ";
 
      }
   
      mysql_close();
 }
      

?>
</center>
<br>
<a href="Index.php">View list attend</a>


Notice: Undefined variable: errors in c:\program files\easyphp1-8\www\rsvp\added.php on line 51
Reply With Quote
  #2 (permalink)  
Old 02-14-2008, 09:14 AM
kruser kruser is offline
WebProWorld Pro
 

Join Date: Aug 2007
Location: Southern Illinois USA
Posts: 141
kruser RepRank 0
Default Re: undefine variable

Try this:

foreach($errors[] as $val)
__________________
Randy
Rocking Chairs
Reply With Quote
  #3 (permalink)  
Old 02-14-2008, 09:40 AM
kruser kruser is offline
WebProWorld Pro
 

Join Date: Aug 2007
Location: Southern Illinois USA
Posts: 141
kruser RepRank 0
Default Re: undefine variable

Forget that last suggestion as it did not work for me.

Your script seems to work when a value has been assigned to $errors, but when there is no value set, it causes an error.
__________________
Randy
Rocking Chairs
Reply With Quote
  #4 (permalink)  
Old 02-14-2008, 09:58 AM
kgun's Avatar
kgun kgun is offline
WebProWorld 1,000+ Club
 

Join Date: May 2005
Location: Norway
Posts: 4,565
kgun RepRank 3kgun RepRank 3
Default Re: undefine variable

If that is correct, a solution is to:
  1. Connect a default value to $errors.
  2. Try / Catch block to catch the error and take the necessary action.
Assumption about 2 above:
The web server has a PHP version where exception handling is implemented, PHP 5+.

Last edited by kgun : 02-14-2008 at 10:01 AM.
Reply With Quote
  #5 (permalink)  
Old 02-14-2008, 10:00 AM
kruser kruser is offline
WebProWorld Pro
 

Join Date: Aug 2007
Location: Southern Illinois USA
Posts: 141
kruser RepRank 0
Default Re: undefine variable

AHH..

When I wrapped it to check for value, it seems to work.

Code:
if(!empty($errors))
{
  foreach ($errors as $val)
   {
       echo "ERROR: $val <br/>"; 
   }
}
__________________
Randy
Rocking Chairs
Reply With Quote
  #6 (permalink)  
Old 02-14-2008, 10:04 AM
wige's Avatar
wige wige is offline
Moderator
WebProWorld Moderator
 

Join Date: Jun 2006
Location: United States
Posts: 1,629
wige RepRank 4wige RepRank 4wige RepRank 4
Default Re: undefine variable

$errors is only created if there is an actual error. if($errors) tests if the $errors variable contains a value other than false, but if no errors were generated, $errors is never created. There are two solutions.
  1. Make the first line of the script $errors=false; This will cause the script to work, because the if condition will fail if no errors are generated.
  2. Replace the if($errors) test with if(isset($errors)) which checks if the variable exists. Since the variable currently only gets created if there is an error, if $errors has not been created the test will fail.
Note, these are two ways of accomplishing the same task. Do not do both.
__________________
The best way to learn anything, is to question everything.
Interestingly Average Security Blog
Reply With Quote
  #7 (permalink)  
Old 02-20-2008, 01:44 AM
jinchiruki jinchiruki is offline
WebProWorld New Member
 

Join Date: Feb 2008
Posts: 3
jinchiruki RepRank 0
Default Re: undefine variable

ok guy,pass...change few code..but now,facing 1 problem...how can i stop query to databse when theres no data fill in the form if i click submit?


here my latest code

Code:
 
<table width="1000" height="95" border="1">
  <tr>
    <td bgcolor="#0000FF">&nbsp;</td>
  </tr>
</table>

<center>

<?
   include("connect.php"); 
      //this is your validation in the form,put it here....
 { 
 
 if (empty($_POST['name']))
 { 
      $errors[] = 'Please enter a name';
  }
      if (empty($_POST['email']))
  {
      $errors[] = 'Please enter a valid e-mail address';
  }
      else if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", 
      $_POST['email']))
  {
      $errors[] = 'Please enter a valid e-mail address';
  }
      if (empty($_POST['contact']))
  {
      $errors[] = 'Please enter a valid contact with numeric value';
  }
      else if (!is_numeric($_POST['contact']))
  {
      $errors[] = 'Please enter a valid contact with a numeric value';
  }
      
      
      if (empty($_POST['person_attend']))
   {
      $errors[] = 'Please enter some word for person attend';
   }
      
      
      else if (strlen ($_POST['person_attend']) > 255)
  {
      $errors[] = 'person attend';
   }
   
   
   
      if (empty($_POST['comment']))
      {
      $errors[] = 'Please enter some comment';
      }
      else if (strlen ($_POST['comment']) > 255)
      {
      $errors[] = 'comment ';
  
 }
 
      if(isset($errors))
{

    foreach($errors as $val)
    {
    
        echo "Error: $val <br/>"; 
    }
}
 
 
      if
      
      ($name="" && $email="" && $contact="" && $person_attend="" && $comment="" )
      
      {
 
      //insert statements
 
      } else 
      
      {
 
      //error message
 
      }
 
      //process form
 
      //this is your add query....
  
      $name = $_POST['name'];
 
      $email = $_POST['email'];
 
      $contact = $_POST['contact'];
 
      $person_attend = $_POST['person_attend'];
  
      $comment = $_POST['comment'];
 
      $query = "INSERT INTO rsvp (id, name, email, contact, person_attend, comment)
   
      VALUES ('', '$name', '$email', '$contact', '$person_attend', '$comment')";
      
      $results = mysql_query($query) or die
  
      ("Could not execute query : $query." . mysql_error());
    
      {
 
      echo "thanks you ";
 
      }
   
      mysql_close();
 }
      

?>
</center>
<center> 
<br>
<a href="Index.php">View list attend</a><br>
<a href="register.html">Clik here to register</a>
</center>
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum


Thread Tools
Display Modes

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

vB 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
Can I get a variable name as a string? montyauto Web Programming Discussion Forum 12 01-18-2007 05:20 PM
Passing a variable through the url netman4ttm Web Programming Discussion Forum 2 06-15-2005 10:50 AM
PHP Variable NOT NULL richkoi Database Discussion Forum 5 06-07-2005 03:42 PM
Link variable (PHP) L Plate web designer Web Programming Discussion Forum 12 10-08-2004 10:31 AM
Variable Stylesheets MrLeN Graphics & Design Discussion Forum 9 11-30-2003 01:39 PM


Search Engine Friendly URLs by vBSEO 3.0.0